QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#239596 | #6560. Broken Minimum Spanning Tree | Fyind | AC ✓ | 20ms | 4188kb | C++20 | 3.2kb | 2023-11-04 21:36:00 | 2023-11-04 21:36:00 |
Judging History
answer
#include <bits/stdc++.h>
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define ls o << 1
#define rs o << 1 | 1
using namespace std;
typedef long long ll;
#include <chrono>
using namespace chrono;
void solve() {
ll n, m;
cin >> n >> m;
ll sss = 0;
vector<pair<pair<ll, pair<ll, ll>>, ll>> e(m + 1);
vector<bool> mm(m + 1);
vector<vector<pair<ll, ll>>> g(n + 1);
for (ll i = 1; i <= m; ++i)
cin >> e[i].fi.se.fi >> e[i].fi.se.se >> e[i].fi.fi;
for (ll i = 1; i <= m; ++i) {
e[i].se = i;
if (e[i].fi.se.fi > e[i].fi.se.se)
swap(e[i].fi.se.fi, e[i].fi.se.se);
if (i <= n - 1)
sss += e[i].fi.fi, mm[i] = true, g[e[i].fi.se.fi].push_back({e[i].fi.se.se, i}), g[e[i].fi.se.se].push_back({e[i].fi.se.fi, i});
}
vector<ll> ee(m + 1);
vector<ll> del(m + 1);
vector<pair<ll, ll>> la(n + 1);
function<bool(ll, ll)> dfs = [&](ll u, ll tt){
if (u == tt)
return true;
for (auto [v, i] : g[u])
if (v != la[u].fi && !del[i]) {
la[v] = {u, ee[i]};
if (dfs(v, tt))
return true;
}
return false;
};
sort(e.begin() + 1, e.end());
for (ll i = 1; i <= m; ++i)
ee[e[i].se] = i;
vector<ll> fa(n + 1);
ll ansss = 0;
for (ll i = 1; i <= n; ++i)
fa[i] = i;
function<ll(ll)> getf = [&](ll o) {
return o == fa[o] ? o : fa[o] = getf(fa[o]);
};
for (ll i = 1; i <= m; ++i)
if (getf(e[i].fi.se.fi) != getf(e[i].fi.se.se))
fa[getf(e[i].fi.se.fi)] = getf(e[i].fi.se.se), ansss += e[i].fi.fi;
vector<pair<ll, ll>> ans;
for (ll i = 1; i <= m; ++i) {
ll u = e[i].fi.se.fi, v = e[i].fi.se.se;
if (mm[e[i].se])
continue;
if (sss == ansss)
break;
for (int i = 1; i <= n; ++i)
la[i] = {0, 0};
dfs(u, v);
ll pos = -1, mx = e[i].fi.fi, cur = v;
while (cur != u && cur) {
if (mx < e[la[cur].se].fi.fi) {
mx = e[la[cur].se].fi.fi, pos = la[cur].se;
}
cur = la[cur].fi;
}
// cout << pos << ',' << mx << endl;
if (pos != -1) {
g[e[pos].fi.se.fi].erase(find(g[e[pos].fi.se.fi].begin(), g[e[pos].fi.se.fi].end(), make_pair(e[pos].fi.se.se, e[pos].se)));
g[e[pos].fi.se.se].erase(find(g[e[pos].fi.se.se].begin(), g[e[pos].fi.se.se].end(), make_pair(e[pos].fi.se.fi, e[pos].se)));
g[u].push_back({v, e[i].se}), g[v].push_back({u, e[i].se});
mm[e[pos].se] = false;
mm[e[i].se] = true;
ans.push_back({e[pos].se, e[i].se});
sss -= e[pos].fi.fi, sss += e[i].fi.fi;
}
}
cout << ans.size() << '\n';
for (auto [x, y] : ans)
cout << x << ' ' << y << '\n';
}
int main() {
auto start = system_clock::now();
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifdef LLOCAL
freopen("a.in", "r", stdin);
#endif
// int t;
// cin >> t;
// while (t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3840kb
input:
4 4 1 2 10 2 3 3 3 4 1 1 4 4
output:
1 1 4
result:
ok correct!
Test #2:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
6 8 1 2 10 2 3 10 3 4 10 4 5 10 5 6 10 6 1 10 1 3 1 4 6 1
output:
2 2 7 5 8
result:
ok correct!
Test #3:
score: 0
Accepted
time: 1ms
memory: 4152kb
input:
2000 1999 1262 1505 968661582 323 1681 787089412 1132 129 88786681 1909 587 762050278 979 1371 230688681 1686 521 980519364 975 191 887826021 869 461 899130441 1433 259 961154249 1718 547 721696188 1254 1042 458319755 1779 267 85751052 1170 813 283230029 309 20 971682908 224 417 255325364 1084 986 7...
output:
0
result:
ok correct!
Test #4:
score: 0
Accepted
time: 1ms
memory: 4124kb
input:
1999 1998 1757 1820 444157563 1757 395 754598547 1757 1571 432619009 1757 1009 456234067 1757 824 935569725 1757 1698 476714469 1757 1420 901765343 1757 1175 225295107 1757 1512 721959801 1757 1585 955067704 1757 1739 635181418 1757 1686 891225461 1757 84 132683224 1757 1696 48915557 1757 1623 42602...
output:
0
result:
ok correct!
Test #5:
score: 0
Accepted
time: 1ms
memory: 3960kb
input:
1999 1998 1345 647 232183406 40 837 279910457 819 857 137486924 255 1378 517489941 827 1565 894953662 1556 1545 898170464 965 877 72248541 1631 298 635713424 895 197 366305735 966 1160 515776809 1870 1638 220711661 1736 220 716014108 1914 1609 759121968 1293 153 272816132 1936 1433 263859075 985 460...
output:
0
result:
ok correct!
Test #6:
score: 0
Accepted
time: 1ms
memory: 3776kb
input:
500 998 105 1 1 105 2 1 105 3 1 105 4 1 105 5 1 105 6 1 105 7 1 105 8 1 105 9 1 105 10 1 105 11 1 105 12 1 105 13 1 105 14 1 105 15 1 105 16 1 105 17 1 105 18 1 105 19 1 105 20 1 105 21 1 105 22 1 105 23 1 105 24 1 105 25 1 105 26 1 105 27 1 105 28 1 105 29 1 105 30 1 105 31 1 105 32 1 105 33 1 105 ...
output:
0
result:
ok correct!
Test #7:
score: 0
Accepted
time: 1ms
memory: 3736kb
input:
500 998 364 1 1 364 2 1 364 3 1 364 4 1 364 5 1 364 6 1 364 7 1 364 8 1 364 9 1 364 10 1 364 11 1 364 12 1 364 13 1 364 14 1 364 15 1 364 16 1 364 17 1 364 18 1 364 19 1 364 20 1 364 21 1 364 22 1 364 23 1 364 24 1 364 25 1 364 26 1 364 27 1 364 28 1 364 29 1 364 30 1 364 31 1 364 32 1 364 33 1 364 ...
output:
0
result:
ok correct!
Test #8:
score: 0
Accepted
time: 1ms
memory: 3948kb
input:
500 998 86 1 2 86 2 2 86 3 2 86 4 2 86 5 2 86 6 2 86 7 2 86 8 2 86 9 2 86 10 2 86 11 2 86 12 2 86 13 2 86 14 2 86 15 2 86 16 2 86 17 2 86 18 2 86 19 2 86 20 2 86 21 2 86 22 2 86 23 2 86 24 2 86 25 2 86 26 2 86 27 2 86 28 2 86 29 2 86 30 2 86 31 2 86 32 2 86 33 2 86 34 2 86 35 2 86 36 2 86 37 2 86 38...
output:
499 496 500 1 501 2 502 3 503 4 504 5 505 6 506 7 507 8 508 9 509 10 510 11 511 12 512 13 513 14 514 15 515 16 516 17 517 18 518 19 519 20 520 21 521 22 522 23 523 24 524 25 525 26 526 27 527 28 528 29 529 30 530 31 531 32 532 33 533 34 534 35 535 36 536 37 537 38 538 39 539 40 540 41 541 42 542 43 ...
result:
ok correct!
Test #9:
score: 0
Accepted
time: 0ms
memory: 3772kb
input:
500 998 198 227 1 227 315 1 315 426 1 426 400 1 400 61 1 61 143 1 143 487 1 487 65 1 65 415 1 415 434 1 434 327 1 327 190 1 190 411 1 411 51 1 51 91 1 91 364 1 364 185 1 185 393 1 393 89 1 89 53 1 53 66 1 66 69 1 69 13 1 13 5 1 5 45 1 45 314 1 314 291 1 291 490 1 490 92 1 92 175 1 175 458 1 458 218 ...
output:
0
result:
ok correct!
Test #10:
score: 0
Accepted
time: 1ms
memory: 3716kb
input:
500 998 360 250 1 250 71 1 71 170 1 170 492 1 492 419 1 419 145 1 145 188 1 188 433 1 433 186 1 186 161 1 161 398 1 398 19 1 19 479 1 479 401 1 401 40 1 40 176 1 176 212 1 212 353 1 353 290 1 290 43 1 43 322 1 322 447 1 447 47 1 47 468 1 468 456 1 456 343 1 343 339 1 339 52 1 52 251 1 251 130 1 130 ...
output:
0
result:
ok correct!
Test #11:
score: 0
Accepted
time: 3ms
memory: 3848kb
input:
500 998 369 45 2 45 364 2 364 300 2 300 195 2 195 291 2 291 390 2 390 122 2 122 331 2 331 408 2 408 91 2 91 298 2 298 116 2 116 301 2 301 287 2 287 338 2 338 4 2 4 79 2 79 177 2 177 387 2 387 125 2 125 477 2 477 11 2 11 284 2 284 102 2 102 305 2 305 395 2 395 112 2 112 280 2 280 294 2 294 232 2 232 ...
output:
499 134 996 209 997 455 584 491 583 163 607 321 608 244 554 161 555 104 576 64 575 60 688 79 689 255 541 86 540 193 858 159 857 346 978 416 979 34 742 254 741 302 829 481 828 475 566 128 567 432 793 281 792 295 960 236 961 408 814 283 813 168 660 141 661 470 878 44 879 333 691 425 690 219 962 409 96...
result:
ok correct!
Test #12:
score: 0
Accepted
time: 1ms
memory: 3708kb
input:
500 998 298 314 1 467 314 1 9 314 1 345 298 1 497 298 1 315 467 1 147 345 1 154 345 1 16 345 1 226 497 1 406 147 1 204 298 1 351 406 1 432 314 1 274 406 1 340 274 1 395 226 1 173 315 1 180 274 1 207 9 1 495 204 1 213 298 1 413 207 1 450 204 1 25 147 1 161 497 1 231 180 1 175 467 1 199 231 1 454 231 ...
output:
0
result:
ok correct!
Test #13:
score: 0
Accepted
time: 1ms
memory: 3776kb
input:
500 998 42 349 1 256 42 1 202 349 1 23 42 1 252 42 1 175 42 1 67 252 1 302 67 1 337 252 1 495 252 1 14 349 1 347 202 1 494 495 1 206 347 1 1 302 1 434 349 1 475 206 1 243 206 1 135 494 1 179 495 1 226 202 1 490 226 1 481 1 1 165 243 1 114 495 1 463 256 1 282 114 1 411 202 1 25 1 1 163 67 1 388 179 1...
output:
0
result:
ok correct!
Test #14:
score: 0
Accepted
time: 2ms
memory: 3668kb
input:
500 998 493 328 2 444 493 2 356 328 2 374 328 2 135 328 2 292 444 2 323 135 2 296 328 2 407 493 2 207 374 2 118 296 2 490 135 2 357 323 2 464 292 2 279 323 2 183 493 2 81 356 2 367 407 2 235 356 2 354 292 2 479 464 2 214 118 2 406 357 2 164 279 2 230 356 2 380 164 2 399 135 2 344 81 2 190 490 2 422 ...
output:
499 89 588 34 552 425 924 241 740 415 914 186 696 255 801 42 743 344 843 69 568 68 567 440 939 28 563 317 816 275 810 160 659 173 672 137 658 310 809 133 678 180 870 359 858 436 935 72 642 253 976 121 620 87 601 369 868 183 899 182 681 63 679 175 683 278 777 199 761 114 666 171 670 342 841 267 797 3...
result:
ok correct!
Test #15:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
10 20 3 5 132699872 7 3 667475629 10 7 829222331 1 7 265644695 4 3 226461311 2 7 720348681 6 10 703702759 8 4 153004599 9 10 646988804 1 9 45480111 2 4 784301144 1 9 628023542 7 8 449200681 9 2 240371799 3 2 420603433 3 9 838425734 4 6 623790050 1 7 513829155 1 9 883183260 10 3 422484921
output:
5 3 10 6 14 2 15 9 20 7 17
result:
ok correct!
Test #16:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
100 200 69 52 334673965 90 52 598347660 62 52 671196898 38 90 561150605 97 69 844448459 25 90 865251171 41 38 773653441 49 97 813975775 99 41 996226580 54 69 583281785 34 38 385173507 56 97 285801905 17 38 946715780 67 17 139770128 43 97 890101081 68 90 370458274 74 17 698466900 6 67 19950896 58 56 ...
output:
50 17 103 13 174 44 168 6 180 5 181 52 150 15 196 69 125 24 195 20 184 83 145 65 170 9 134 8 182 25 132 87 109 77 163 60 104 31 194 95 185 3 116 45 166 58 120 71 115 10 156 2 131 41 136 80 171 61 160 16 177 7 153 92 190 74 192 47 112 21 138 4 189 39 183 23 186 49 126 28 108 50 137 35 121 89 152 64 1...
result:
ok correct!
Test #17:
score: 0
Accepted
time: 20ms
memory: 4020kb
input:
2000 3000 1279 1465 434468566 1062 1279 993799662 1494 1465 490141333 529 1279 207090506 119 1279 494706603 1830 1062 798435525 1307 1279 501822892 362 119 776215279 1330 1494 64095945 1823 529 302809447 1882 529 298925061 1394 529 639185117 1852 362 939130818 752 529 845078929 104 752 853251112 126...
output:
617 12 2790 2 2921 16 2517 58 2345 734 2409 35 2862 14 2901 549 2978 41 2645 22 2897 268 2537 328 2651 199 2364 161 2026 69 2404 1357 2446 492 2315 395 2055 143 2449 1409 2877 1527 2829 551 2255 1890 2116 39 2710 1188 2911 1313 2415 1021 2064 87 2171 364 2836 457 2927 1174 2318 13 2181 1709 2532 742...
result:
ok correct!
Test #18:
score: 0
Accepted
time: 19ms
memory: 4160kb
input:
2000 3000 285 1808 694643588 224 285 690510187 908 1808 663193044 486 224 663712643 324 908 165916788 1403 285 948845412 1310 324 12561437 1948 285 642808470 883 1310 358793640 396 1808 869731392 1276 1310 621641177 203 1948 231802320 1547 1276 39692873 830 285 636658714 1357 1948 177401445 303 203 ...
output:
613 41 2567 44 2230 256 2412 782 2255 167 2054 77 2135 35 2979 1687 2143 16 2435 1073 2483 101 2058 1010 2539 1902 2445 219 2314 1674 2011 17 2635 142 2966 964 2287 255 2097 122 2897 271 2737 1777 2956 451 2547 469 2093 1862 2538 10 2610 453 2591 6 2234 755 2425 341 2965 497 2895 587 2929 1178 2702 ...
result:
ok correct!
Test #19:
score: 0
Accepted
time: 19ms
memory: 4024kb
input:
2000 3000 1966 1337 886061561 1564 1966 321739163 878 1966 383102115 15 1337 355428698 392 15 389233814 1520 1337 163779508 1349 392 323493610 1126 1349 804548395 1739 1337 508691040 956 1564 924027693 674 1126 845489957 1749 1739 290423046 1926 1966 647294733 456 1966 656155212 1746 1564 106274278 ...
output:
603 46 2532 252 2140 1353 2455 849 2657 21 2356 22 2845 373 2001 132 2107 10 2535 1000 2688 278 2276 906 2139 41 2667 204 2685 48 2591 1112 2122 188 2752 1139 2456 1775 2632 1376 2142 182 2225 1455 2180 265 2134 1274 2482 20 2590 547 2295 1872 2554 828 2924 181 2061 359 2780 1179 2270 421 2288 1875 ...
result:
ok correct!
Test #20:
score: 0
Accepted
time: 19ms
memory: 4084kb
input:
2000 3000 487 1828 891595258 848 1828 70120465 399 1828 222566316 2000 1828 390057442 589 487 561090448 1878 399 923567050 1547 848 289163461 724 1828 712597149 856 487 612088317 1932 848 498697630 177 1932 225589816 1541 856 745128386 1229 399 501103338 40 1828 283700123 1206 1878 364593718 519 40 ...
output:
624 176 2755 52 2971 1 2335 43 2584 1095 2285 6 2748 1671 2349 1348 2705 178 2699 261 2201 670 2308 500 2324 40 2361 29 2301 630 2776 515 2475 617 2878 275 2176 39 2961 320 2891 1678 2133 717 2745 418 2025 119 2062 94 2336 236 2105 100 2794 17 2064 707 2096 955 2647 1924 2304 91 2910 664 2420 129 29...
result:
ok correct!
Test #21:
score: 0
Accepted
time: 19ms
memory: 4188kb
input:
2000 3000 28 909 901469954 874 909 630039044 1150 874 369081856 180 1150 796073964 199 874 607566492 1260 1150 672891947 233 180 524809142 390 909 531859461 122 874 275924720 457 1260 521407422 872 28 975420599 497 872 901775699 885 390 839588422 1242 199 380484388 1598 28 823494399 202 885 41696165...
output:
618 1 2493 11 2631 207 2420 90 2260 89 2221 591 2865 123 2060 1138 2194 510 2728 30 2503 375 2641 1595 2950 64 2107 192 2226 112 2237 523 2162 241 2674 47 2611 107 2813 1109 2033 549 2572 673 2006 134 2486 639 2319 618 2512 13 2786 947 2448 1307 2125 433 2431 282 2584 240 2627 245 2209 536 2531 1271...
result:
ok correct!
Test #22:
score: 0
Accepted
time: 1ms
memory: 3900kb
input:
2 3000 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1...
output:
0
result:
ok correct!
Test #23:
score: 0
Accepted
time: 1ms
memory: 4120kb
input:
2000 2000 273 274 976318149 1818 1819 911362963 920 921 733992701 1147 1148 968069222 1076 1077 479630568 1576 1577 723601562 860 861 477629418 747 748 636289483 219 220 254346042 610 611 561106993 1173 1174 117741584 1788 1789 433959137 437 438 566901968 723 724 578256290 984 985 201368344 954 955 ...
output:
1 1575 2000
result:
ok correct!
Test #24:
score: 0
Accepted
time: 10ms
memory: 4044kb
input:
2000 2500 1936 1937 470205868 750 751 637463850 75 76 353874306 1012 1013 575007557 679 680 452883390 268 269 382879319 1885 1886 619233286 1617 1618 985926999 365 366 731212904 703 704 136815299 1543 1544 6628104 1586 1587 963856921 1904 1905 377843376 254 255 540189789 690 691 218468543 1169 1170 ...
output:
371 1281 2405 1310 2118 1813 2155 1341 2388 588 2119 484 2236 185 2476 1652 2029 792 2473 1250 2404 592 2035 815 2415 1287 2463 779 2131 366 2247 239 2138 1405 2090 757 2482 1956 2184 562 2377 1774 2057 1617 2079 1555 2008 1200 2349 1128 2450 1201 2054 22 2303 1500 2145 499 2175 1995 2253 1019 2494 ...
result:
ok correct!
Test #25:
score: 0
Accepted
time: 18ms
memory: 4184kb
input:
2000 3000 1100 1101 966680160 584 585 619523116 196 197 969093892 1265 1266 112963336 1463 1464 437550508 1320 1321 888461822 1414 1415 755948833 897 898 48495011 365 366 564439441 869 870 108232038 1323 1324 469077928 1432 1433 609528786 1885 1886 447585062 81 82 480544752 1819 1820 385633491 1371 ...
output:
588 785 2566 1022 2037 1714 2618 179 2531 1610 2985 1940 2590 1999 2959 1394 2244 1414 2451 1764 2715 1036 2249 759 2075 853 2971 983 2632 1184 2954 1004 2832 530 2218 373 2498 1404 2797 337 2102 1602 2935 1805 2317 724 2952 1487 2490 981 2124 1555 2879 275 2579 831 2144 1548 2417 1492 2784 1186 277...
result:
ok correct!
Test #26:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
10 18 2 6 2 6 1 2 1 10 2 10 4 2 4 3 2 3 9 2 9 8 2 8 5 2 5 7 2 2 4 1 4 10 1 10 3 1 3 5 1 5 6 1 6 7 1 7 8 1 8 1 1 1 9 1
output:
9 7 17 6 18 4 10 8 13 3 12 5 11 1 14 9 15 2 16
result:
ok correct!