QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#792454 | #6560. Broken Minimum Spanning Tree | lukamosiashvili# | WA | 1ms | 3908kb | C++17 | 2.1kb | 2024-11-29 10:23:00 | 2024-11-29 10:23:00 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
array <int, 4> v[3009];
int par[3009], in[3009], t, bo[3009];
vector <pair <int, int> > adj[3009];
int fnd(int a){
if(par[a] == a) return a; else return par[a] = fnd(par[a]);
}
void mrg(int a, int b){
a = fnd(a);
b = fnd(b);
if(a == b) return;
par[b] = a;
}
vector <int> st;
vector <pair <int, int> > ans;
int edge;
void dfs(int v){
bo[v] = 1;
for(auto to:adj[v]){
if(to.first == t){
//cout << "found\n";
st.push_back(to.second);
for(int j = 0; j < st.size(); j++){
if(in[st[j]]){
in[st[j]] = 0;
ans.push_back({edge, st[j]});
t = -1;
return;
}
}
return;
}
if(bo[to.first]) continue;
st.push_back(to.second);
dfs(to.first);
if(t == 1) return;
}
st.pop_back();
}
int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int n, m;
cin >> n >> m;
for(int i = 1; i <= m; i++){
cin >> v[i][2] >> v[i][3] >> v[i][0];
if(i < n){
v[i][1] = -i;
}else{
v[i][1] = i;
}
}
sort(v + 1, v + m + 1);
for(int i = 1; i <= n; i++) par[i] = i;
for(int i = 1; i <= m; i++){
if(fnd(v[i][2]) != fnd(v[i][3])){
mrg(v[i][2], v[i][3]);
adj[v[i][2]].push_back({v[i][3], abs(v[i][1])});
adj[v[i][3]].push_back({v[i][2], abs(v[i][1])});
if(v[i][1] > 0) in[v[i][1]] = 1;
}else{
if(v[i][1] < 0){
t = v[i][3];
for(int j = 1; j <= n; j++) bo[j] = 0;
st.clear();
edge = abs(v[i][1]);
dfs(v[i][2]);
//cout << t << endl;
}
}
}
cout << ans.size() << endl;
for(int i = 0; i < ans.size(); i++){
cout << ans[i].first << " " << ans[i].second << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3672kb
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: 3756kb
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 4 8 1 7
result:
ok correct!
Test #3:
score: 0
Accepted
time: 1ms
memory: 3824kb
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: 3908kb
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: 3856kb
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: 3828kb
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: 3836kb
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: -100
Wrong Answer
time: 1ms
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:
498 499 585 498 997 497 996 495 995 494 994 493 993 492 992 491 991 490 990 489 989 488 988 487 987 486 986 485 985 484 984 483 983 482 982 481 981 480 980 479 979 478 978 477 977 476 976 475 975 474 974 473 973 472 972 471 971 470 970 469 969 468 968 467 967 466 966 465 965 464 964 463 963 462 962 ...
result:
wrong answer not a spanning tree after swap 1