QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#524682 | #6560. Broken Minimum Spanning Tree | amirreza# | WA | 2ms | 3928kb | C++20 | 3.2kb | 2024-08-20 00:39:27 | 2024-08-20 00:39:27 |
Judging History
answer
// ki bood ke goft Ghatinga?
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<pii,int> ppiii;
// vectors and Sets:
#define vc vector
#define pb push_back
#define all(c) (c).begin(), (c).end()
// pairs
#define mp make_pair
#define fr first
#define sc second
// execution time check and Debug
#define StartEX clock_t startExeTime = clock();
#define EndEX clock_t endExeTime = clock();
#define ExTime cerr << "\nTotal Execution Time is: " << double(-double(startExeTime)+double(endExeTime)) / CLOCKS_PER_SEC;
#define debug(x) cerr << #x << " : " << x << '\n'
#define endl "\n"
// time optimization
#define Heh ios::sync_with_stdio(false);cin.tie(0);
#define Bah ios::sync_with_stdio(false);
#pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// useful functions
ll pw(ll a,ll b){
ll ret = 1;
ll mul = a;
while(b > 0){
if(b&1)
ret = (ret * mul);
mul = (mul * mul);
b >>= 1;
}
return ret;
}
ll pw(ll a,ll b,ll mod){
ll ret = 1;
ll mul = a;
while(b > 0){
if(b&1)
ret = (ret * mul) % mod;
mul = (mul * mul) % mod;
b >>= 1;
}
return ret;
}
ll to_int(string s){
ll ret = 0;
for(int i = 0 ; i < (int)s.size() ; i++) ret = 10 * ret + s[i] - '0';
return ret;
}
bool deq(ld a , ld b){return (abs(a-b) < 0.000001);} // 10 ^ -6
const int MAXM = 3e3 + 33;
int n , m;
int par[MAXM] , rnk[MAXM];
int get(int v){return par[v] = (par[v] == v ? v : get(par[v]));}
void uni(int a , int b){
a = get(a) , b = get(b);
if(a == b) return;
if(rnk[a] < rnk[b]) swap(a , b);
par[b] = a;
rnk[a] += (rnk[a] == rnk[b]);
}
struct ed{
int u , v , w;
int id;
bool operator<(ed x){
return w < x.w;
}
};
int main()
{
Heh;
cin >> n >> m;
ed es[m] , ses[m] , nes[m];
for(int i = 0 ; i < m ; i++){
int u , v , w;
cin >> u >> v >> w;
u-- , v--;
es[i] = {u , v , w , i};
ses[i] = es[i];
nes[i] = es[i];
}
sort(es , es + n-1);
sort(ses , ses + m);
vc<int> add , rem;
for(int i = 0 ; i < n ; i++) par[i] = i , rnk[i] = 0;
int pt1 = 0 , pt2 = 0;
vc<pii> bs;
while(pt1 < n-1 or pt2 < m){
if(pt2 == m or (pt1 < n-1 and es[pt1].w <= ses[pt2].w)){
if(get(es[pt1].u) == get(es[pt1].v)){
rem.pb(es[pt1].id);
}
else{
uni(es[pt1].u , es[pt1].v);
bs.pb({es[pt1].u , es[pt1].v});
}
pt1++;
}
else{
if(get(ses[pt2].u) != get(ses[pt2].v)){
add.pb(ses[pt2].id);
uni(ses[pt2].u , ses[pt2].v);
}
pt2++;
}
}
cout << add.size() << endl;
for(int ri : rem){
for(int i = 0 ; i < n ; i++) par[i] = i , rnk[i] = 0;
for(auto [u , v] : bs) uni(u , v);
uni(nes[ri].u , nes[ri].v);
int sai = -1;
for(int ai : add){
if(get(nes[ai].u) == get(nes[ai].v)){
cout << ri+1 << " " << ai+1 << endl;
sai = ai;
break;
}
uni(nes[ai].u , nes[ai].v);
}
bs.pb({nes[sai].u , nes[sai].v});
vc<int> nadd;
for(int ai : add){
if(ai != sai) nadd.pb(ai);
}
add = nadd;
}
}
// ki seda kard Patinga?
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3628kb
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: 3572kb
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: 3776kb
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: 3784kb
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: 3712kb
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: 3928kb
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: 3728kb
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: 2ms
memory: 3636kb
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 328 828 343 843 342 842 341 841 340 840 339 839 338 838 337 837 336 836 335 835 334 834 333 833 332 832 331 831 330 830 329 829 344 844 327 827 326 826 325 825 324 824 323 823 322 822 321 821 320 820 319 819 318 818 317 817 316 816 315 815 314 814 313 813 359 859 374 874 373 873 372 872 371 871 ...
result:
ok correct!
Test #9:
score: 0
Accepted
time: 0ms
memory: 3664kb
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: 3568kb
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: -100
Wrong Answer
time: 2ms
memory: 3636kb
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 328 830 343 842 342 841 341 772 340 840 339 989 338 988 337 981 336 839 335 838 334 837 333 718 332 717 331 716 330 696 329 655 344 780 327 829 326 593 325 592 324 591 323 566 322 779 321 778 320 624 319 836 318 965 317 962 316 835 315 590 314 577 313 589 359 588 374 834 373 833 372 832 371 777 ...
result:
wrong answer not a spanning tree after swap 1