QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#864450 | #4272. Phone Plans | lfxxx | 0 | 14ms | 71268kb | C++14 | 3.2kb | 2025-01-20 16:50:23 | 2025-01-20 16:50:24 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) (x).begin(), (x).end()
bool be;
#define int ll
constexpr int N = 6e5 + 5, inf = 1e9 + 9;
int n, a, b, cnt, fa[N], siz[N], in[N], tot;
ll k, sum;
unordered_map<int, int>mp[N];
vector<int>v[N], e[N];
struct edge {
int u, v, w;
}e1[N], e2[N], tmp[N];
int find(int k)
{
return fa[k] == k ? k : fa[k] = find(fa[k]);
}
ll h(int n)
{
return (ll) n * (n - 1) / 2;
}
ll work(edge *e, int &m)
{
cnt = 0;
for (int i = 1; i <= n; ++i) fa[i] = i, siz[i] = 1;
sort(e + 1, e + 1 + m, [](edge a, edge b) {
return a.w < b.w;
});
for (int i = 1; i <= m; ++i) {
int fx = find(e[i].u), fy = find(e[i].v);
if (fx != fy) {
fa[fx] = fy;
siz[fy] += siz[fx];
tmp[++cnt] = e[i];
}
}
m = cnt;
copy(tmp + 1, tmp + 1 + m, e + 1);
ll ans = 0;
for (int i = 1; i <= n; ++i) {
if (find(i) == i) {
ans += h(siz[i]);
}
}
return ans;
}
int dfs(int u, int f, int c)
{
if (in[u]) {
sum += h(mp[find(u)][in[u]]) + h(mp[find(u)][c]);
--mp[find(u)][in[u]];
++mp[find(u)][c];
sum -= h(mp[find(u)][in[u]]) + h(mp[find(u)][c]);
} else {
++mp[find(u)][c];
}
in[u] = c;
int ans = 1;
for (int v : e[u]) {
if (v != f) {
ans += dfs(v, u, c);
}
}
return ans;
}
bool en;
signed main()
{
#ifdef IAKIOI
cerr << (&be - &en) / 1024.0 / 1024 << " MB\n----------------------------" << endl;
freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> a >> b >> k;
for (int i = 1; i <= a; ++i) {
cin >> e1[i].u >> e1[i].v >> e1[i].w;
}
for (int i = 1; i <= b; ++i) {
cin >> e2[i].u >> e2[i].v >> e2[i].w;
}
work(e1, a);
sum = work(e2, b);
sort(e2 + 1, e2 + 1 + b, [](edge a, edge b) {
return a.w > b.w;
});
for (int i = 1; i <= b; ++i) {
e[e2[i].u].emplace_back(e2[i].v);
e[e2[i].v].emplace_back(e2[i].u);
}
for (int i = 1; i <= n; ++i) {
fa[i] = i, siz[i] = 1;
v[i].emplace_back(i);
}
for (int i = 1; i <= n; ++i) {
if (!in[i]) {
dfs(i, 0, ++tot);
}
}
int ans = inf;
for (int i = 0, j = 1; ; ++i) {
while (sum >= k) {
// cerr << i << ' ' << j << ' ' << e1[i].w << ' ' << e2[j].w << ' ' << sum << '\n';
ans = min(ans, e1[i].w + e2[j].w);
if (j > b) break;
int szx = dfs(e2[j].u, e2[j].v, ++tot);
int szy = dfs(e2[j].v, e2[j].u, ++tot);
sum -= h(szx + szy);
sum += h(szx) + h(szy);
++j;
// cerr << i << ' ' << j << ' ' << e1[i].w << ' ' << e2[j].w << ' ' << sum << '\n';
}
int fx = find(e1[i + 1].u), fy = find(e1[i + 1].v);
if (siz[fx] > siz[fy]) swap(fx, fy);
fa[fx] = fy;
sum -= h(siz[fx]) + h(siz[fy]);
siz[fy] += siz[fx];
sum += h(siz[fy]);
for (int x : v[fx]) {
v[fy].emplace_back(x);
sum += h(mp[fx][in[x]]) + h(mp[fy][in[x]]);
--mp[fx][in[x]];
++mp[fy][in[x]];
sum -= h(mp[fx][in[x]]) + h(mp[fy][in[x]]);
}
v[fx].clear(), v[fx].shrink_to_fit();
unordered_map<int, int>_;
mp[fx].swap(_);
if (i == a) break;
}
cout << (ans == inf ? -1 : ans) << endl;
return 0;
}
详细
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 0
Runtime Error
input:
6 4 4 9 1 2 1 2 3 2 1 4 3 3 4 4 5 6 40 1 5 30 2 6 20 3 6 10
output:
result:
Subtask #2:
score: 0
Time Limit Exceeded
Test #53:
score: 0
Time Limit Exceeded
input:
200000 100000 100000 7628995 23677 113459 839167193 165893 15365 669621527 26287 109671 214795757 156871 136723 522277985 9957 100463 806718116 104783 161385 156110975 184577 92225 545172629 57373 130083 980035338 167231 49597 919886451 115601 24325 717233004 99413 141311 737488449 83437 62759 76873...
output:
result:
Subtask #3:
score: 0
Runtime Error
Test #103:
score: 6
Accepted
time: 14ms
memory: 69348kb
input:
1 0 0 0
output:
0
result:
ok single line: '0'
Test #104:
score: 6
Accepted
time: 5ms
memory: 71268kb
input:
2 0 0 1
output:
-1
result:
ok single line: '-1'
Test #105:
score: 0
Runtime Error
input:
2 10 200000 1 2 1 319832429 1 1 412617159 2 1 337734185 1 2 674652880 1 2 454610992 2 2 688717944 1 1 189208056 2 2 951221780 1 2 594191431 2 2 87592160 1 2 833491749 2 2 732961971 2 1 575969648 2 2 268359887 2 1 436382098 1 2 514959278 1 2 305446083 1 2 365989813 1 2 296840111 1 1 541558213 1 1 497...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%