QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#864473 | #4272. Phone Plans | lfxxx | 0 | 457ms | 130532kb | C++14 | 3.4kb | 2025-01-20 17:13:44 | 2025-01-20 17:13:45 |
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) sort(all(e[i]));
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 u = e2[j].u, v = e2[j].v;
int szx = dfs(u, v, ++tot);
int szy = dfs(v, u, ++tot);
sum -= h(szx + szy);
sum += h(szx) + h(szy);
++j;
auto it = lower_bound(all(e[u]), v);
swap(*it, e[u].back());
e[u].pop_back();
it = lower_bound(all(e[v]), u);
swap(*it, e[v].back());
e[v].pop_back();
// 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: 75364kb
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: 7ms
memory: 75360kb
input:
1 0 0 0
output:
0
result:
ok single line: '0'
Test #3:
score: 6
Accepted
time: 9ms
memory: 75492kb
input:
2 0 0 1
output:
-1
result:
ok single line: '-1'
Test #4:
score: 6
Accepted
time: 7ms
memory: 75488kb
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: 14ms
memory: 73440kb
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: 6
Accepted
time: 225ms
memory: 105064kb
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:
9768257
result:
ok single line: '9768257'
Test #7:
score: 6
Accepted
time: 45ms
memory: 77976kb
input:
2000 200000 0 1937051 1325 1770 628367155 105 1670 728177982 358 778 959944062 826 1691 651665248 1119 1906 382208628 1684 1232 677646622 807 265 902880211 1685 1660 405567549 1853 1982 988679307 1241 1054 385406922 862 1049 356441384 1837 673 443580113 1082 1795 738355567 1703 663 221254144 1792 84...
output:
20263921
result:
ok single line: '20263921'
Test #8:
score: 6
Accepted
time: 184ms
memory: 90348kb
input:
2000 200000 200000 1999000 1303 1065 135024256 1400 1409 157921645 1208 515 422761224 466 1398 267944161 40 1202 560552418 722 1817 773826339 1022 1534 720452215 1512 200 145291518 538 230 98848391 434 529 911575234 470 1050 103133355 1800 351 180303134 1527 1871 779519820 258 1872 279904732 1 512 4...
output:
1999
result:
ok single line: '1999'
Test #9:
score: 6
Accepted
time: 182ms
memory: 88300kb
input:
2000 200000 200000 1999000 1566 1720 828051227 411 209 634755980 293 258 80524979 1149 1694 253684780 71 597 899974207 1934 440 11614281 1846 870 794303074 800 1090 576722282 1022 1619 486756658 1135 1004 589873220 1300 326 565865513 114 341 308227260 310 134 735603010 437 291 714079414 930 1131 136...
output:
1999
result:
ok single line: '1999'
Test #10:
score: 6
Accepted
time: 222ms
memory: 86248kb
input:
2000 200000 200000 1999000 1505 1008 194848918 955 1466 251380587 306 119 329528655 1072 1067 684468438 1798 755 822161204 1475 1987 886864916 609 790 525637795 1937 1639 534864650 63 792 948290690 1282 553 998185081 1995 243 367638087 1517 982 238895274 1891 358 612397768 229 1599 453255817 200 115...
output:
1999
result:
ok single line: '1999'
Test #11:
score: 6
Accepted
time: 183ms
memory: 82284kb
input:
2000 200000 200000 1999000 888 998 519088944 896 217 366603047 963 1703 281897731 1419 1583 296352197 318 1644 449932680 1252 683 299241251 1763 1736 16908823 400 673 940814267 1864 1654 16539370 21 1360 526031095 320 1052 879613936 383 555 433309121 243 869 603865861 567 236 829646077 1057 1138 545...
output:
2000
result:
ok single line: '2000'
Test #12:
score: 6
Accepted
time: 7ms
memory: 73700kb
input:
2000 0 0 0
output:
0
result:
ok single line: '0'
Test #13:
score: 6
Accepted
time: 9ms
memory: 75588kb
input:
2000 0 0 1
output:
-1
result:
ok single line: '-1'
Test #14:
score: 6
Accepted
time: 7ms
memory: 73696kb
input:
2000 0 0 1999000
output:
-1
result:
ok single line: '-1'
Test #15:
score: 6
Accepted
time: 457ms
memory: 130408kb
input:
2000 200000 200000 0 584 721 144517612 1593 1184 19655376 572 91 267489352 441 830 206284803 326 901 399207297 1220 164 270714861 1760 1765 242123575 1341 1187 778391819 247 104 618482901 1650 876 469853007 1338 660 237312298 593 1856 254405769 477 1212 387844191 603 1896 336160240 1397 444 77343103...
output:
0
result:
ok single line: '0'
Test #16:
score: 0
Wrong Answer
time: 424ms
memory: 130532kb
input:
2000 200000 200000 1 1961 656 558726882 677 479 191436128 1411 291 100142168 932 1506 28223846 1264 1394 780504516 1276 1479 56569386 1363 65 480245792 1598 78 760054359 78 36 593906829 1112 535 999996793 1792 953 155333434 149 239 901391869 791 1800 803139774 1856 629 626568419 1931 779 330675974 8...
output:
3956442
result:
wrong answer 1st lines differ - expected: '2823', found: '3956442'
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: 10ms
memory: 75488kb
input:
1 0 0 0
output:
0
result:
ok single line: '0'
Test #104:
score: 6
Accepted
time: 7ms
memory: 75496kb
input:
2 0 0 1
output:
-1
result:
ok single line: '-1'
Test #105:
score: 6
Accepted
time: 48ms
memory: 77548kb
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: 35ms
memory: 79592kb
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%