QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#128261 | #6560. Broken Minimum Spanning Tree | installb# | WA | 4ms | 3860kb | C++14 | 2.2kb | 2023-07-20 19:42:46 | 2023-07-20 19:42:48 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 8005;
int fa[N],dep[N],fw[N];
int eu[N],ev[N],w[N];
int swp[N];
vector <pair <int,int> > G[N];
void dfs(int u,int f){
fa[u] = f;
dep[u] = dep[f] + 1;
for(auto [v,ew] : G[u]){
if(v == f) continue;
dfs(v,u);
fw[v] = ew;
}
}
pair <int,int> get_max(int u,int v){
vector <pair <int,int> > H;
if(dep[u] < dep[v]) swap(u,v);
while(dep[u] > dep[v]){
H.push_back({w[fw[u]],-fw[u]});
u = fa[u];
}
while(u != v){
H.push_back({w[fw[u]],-fw[u]});
H.push_back({w[fw[v]],-fw[v]});
u = fa[u]; v = fa[v];
}
sort(H.begin(),H.end());
return H.back();
}
void solve(){
int n,m;
cin >> n >> m;
for(int u,v,i = 1;i < n;i ++){
cin >> u >> v >> w[i];
eu[i] = u; ev[i] = v;
G[u].push_back({v,i});
G[v].push_back({u,i});
}
for(int i = n;i <= m;i ++){
int u,v;
cin >> u >> v >> w[i];
eu[i] = u; ev[i] = v;
dfs(1,0);
auto [ew,id] = get_max(u,v);
// cout << ew << ' ' << id << endl;
id = -id;
if(ew > w[i]){
int las = id;
if(las >= n) las = swp[las];
swp[las] = i;
swp[i] = las;
for(int j = 0;j < G[eu[id]].size();j ++){
if(G[eu[id]][j].first == ev[id]){
swap(G[eu[id]][j],G[eu[id]].back());
}
}
for(int j = 0;j < G[ev[id]].size();j ++){
if(G[ev[id]][j].first == eu[id]){
swap(G[ev[id]][j],G[ev[id]].back());
}
}
G[eu[id]].pop_back();
G[ev[id]].pop_back();
G[u].push_back({v,i});
G[v].push_back({u,i});
}
}
vector <pair <int,int> > ans;
for(int i = 1;i < n;i ++){
if(swp[i]) ans.push_back({i,swp[i]});
}
cout << ans.size() << '\n';
for(auto [u,v] : ans) cout << u << ' ' << v << '\n';
}
signed main(){
ios::sync_with_stdio(false);
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3664kb
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: 3700kb
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 1 7 4 8
result:
ok correct!
Test #3:
score: 0
Accepted
time: 2ms
memory: 3804kb
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: 2ms
memory: 3860kb
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: 2ms
memory: 3744kb
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: 2ms
memory: 3812kb
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: 2ms
memory: 3812kb
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: 3ms
memory: 3816kb
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 1 500 2 501 3 502 4 503 5 504 6 505 7 506 8 507 9 508 10 509 11 510 12 511 13 512 14 513 15 514 16 515 17 516 18 517 19 518 20 519 21 520 22 521 23 522 24 523 25 524 26 525 27 526 28 527 29 528 30 529 31 530 32 531 33 532 34 533 35 534 36 535 37 536 38 537 39 538 40 539 41 540 42 541 43 542 44 5...
result:
ok correct!
Test #9:
score: 0
Accepted
time: 4ms
memory: 3748kb
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: 2ms
memory: 3704kb
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: 1ms
memory: 3736kb
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 1 826 2 743 3 651 4 800 5 992 6 845 7 867 8 789 9 911 10 500 11 926 12 572 13 513 14 882 15 720 16 941 17 554 18 526 19 568 20 964 21 593 22 748 23 828 24 744 25 579 26 942 27 909 28 707 29 681 30 633 31 947 32 592 33 990 34 904 35 742 36 723 37 899 38 621 39 898 40 709 41 552 42 769 43 580 44 5...
result:
ok correct!
Test #12:
score: 0
Accepted
time: 3ms
memory: 3796kb
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: 3708kb
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: 3ms
memory: 3780kb
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 1 500 2 501 3 502 4 503 5 504 6 505 7 506 8 507 9 508 10 509 11 510 12 511 13 512 14 513 15 514 16 515 17 516 18 517 19 518 20 519 21 520 22 521 23 522 24 523 25 524 26 525 27 526 28 527 29 528 30 529 31 530 32 531 33 532 34 533 35 534 36 535 37 536 38 537 39 538 40 539 41 540 42 541 43 542 44 5...
result:
ok correct!
Test #15:
score: 0
Accepted
time: 1ms
memory: 3772kb
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 2 15 3 10 6 14 7 17 9 20
result:
ok correct!
Test #16:
score: -100
Wrong Answer
time: 0ms
memory: 3672kb
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 2 156 3 116 4 171 5 108 6 127 7 153 8 115 9 134 10 170 13 182 15 121 16 181 17 103 19 184 20 194 21 196 23 186 24 137 25 104 28 166 31 157 35 160 36 131 39 183 41 177 44 107 45 180 47 138 49 189 50 195 52 150 58 163 60 168 61 174 64 148 65 132 67 197 69 125 71 120 74 192 75 135 77 136 80 126 81 1...
result:
wrong answer not a spanning tree after swap 3