QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#864462 | #4272. Phone Plans | lfxxx | 0 | 213ms | 107752kb | C++14 | 3.2kb | 2025-01-20 17:04:29 | 2025-01-20 17:04:29 |
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);
e[m + 1] = {0, 0, 0};
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';
}
if (i == a) break;
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]]);
}
unordered_map<int, int>_;
mp[fx].swap(_);
}
cout << (ans == inf ? -1 : ans) << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 6
Accepted
time: 8ms
memory: 75492kb
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:
33
result:
ok single line: '33'
Test #2:
score: 6
Accepted
time: 12ms
memory: 75364kb
input:
1 0 0 0
output:
0
result:
ok single line: '0'
Test #3:
score: 6
Accepted
time: 11ms
memory: 73444kb
input:
2 0 0 1
output:
-1
result:
ok single line: '-1'
Test #4:
score: 6
Accepted
time: 10ms
memory: 75236kb
input:
2 10 10 1 1 1 915886339 2 2 430624192 1 1 879808770 1 2 577221915 1 1 439429731 1 2 304911826 1 1 148009333 1 2 51122687 1 1 558282772 1 2 421196385 2 1 436692145 1 2 654020563 2 2 162573477 2 2 989531779 1 1 646687051 2 2 549037477 2 2 699532275 1 1 679375858 2 1 83352965 2 1 415698228
output:
51122687
result:
ok single line: '51122687'
Test #5:
score: 6
Accepted
time: 10ms
memory: 75368kb
input:
2 10 10 1 1 1 1000000000 1 2 1000000000 2 2 1000000000 2 1 1000000000 1 2 1000000000 1 1 1000000000 1 2 1000000000 2 2 1000000000 1 2 1000000000 1 2 1000000000 2 1 1000000000 1 2 1000000000 2 1 1000000000 2 2 1000000000 1 2 1000000000 2 2 1000000000 1 1 1000000000 1 1 1000000000 2 2 1000000000 1 2 1...
output:
1000000000
result:
ok single line: '1000000000'
Test #6:
score: 0
Wrong Answer
time: 213ms
memory: 107752kb
input:
2000 0 200000 1199833 636 1231 120395621 689 1640 497332138 1861 1980 798839749 560 1283 560726905 1332 328 431171189 1203 1764 466367210 1102 347 317109768 1462 789 761470540 350 1093 551905741 1718 1047 548650524 51 546 56733461 58 1519 744207013 826 956 943929583 1969 207 238061756 99 47 99683567...
output:
10581874
result:
wrong answer 1st lines differ - expected: '9768257', found: '10581874'
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
Time Limit Exceeded
Test #103:
score: 6
Accepted
time: 13ms
memory: 75320kb
input:
1 0 0 0
output:
0
result:
ok single line: '0'
Test #104:
score: 6
Accepted
time: 11ms
memory: 75492kb
input:
2 0 0 1
output:
-1
result:
ok single line: '-1'
Test #105:
score: 6
Accepted
time: 48ms
memory: 81640kb
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:
10104
result:
ok single line: '10104'
Test #106:
score: 6
Accepted
time: 37ms
memory: 79464kb
input:
2 10 200000 1 1 1 1000000000 1 1 1000000000 1 2 1000000000 1 2 1000000000 1 2 1000000000 1 1 1000000000 2 1 1000000000 2 2 1000000000 2 2 1000000000 2 2 1000000000 1 1 1000000000 1 2 1000000000 1 1 1000000000 2 1 1000000000 1 1 1000000000 1 1 1000000000 2 1 1000000000 1 1 1000000000 2 2 1000000000 2...
output:
1000000000
result:
ok single line: '1000000000'
Test #107:
score: 0
Time Limit Exceeded
input:
200000 10 200000 17900 199675 76237 290240030 50211 6922 761990536 92097 120746 607490 192856 130409 616541101 50427 80049 330957286 129588 180294 466199684 8674 110653 155297749 91380 156344 798960399 102127 40858 801752583 94673 105892 152356242 185676 6183 263664373 169026 112948 812501808 93517 ...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%