QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#643826#6560. Broken Minimum Spanning Treeenze114514WA 6ms4280kbC++203.9kb2024-10-16 01:48:222024-10-16 01:48:23

Judging History

你现在查看的是最新测评结果

  • [2024-10-16 01:48:23]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:4280kb
  • [2024-10-16 01:48:22]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

#define pb push_back

const ld pi = 3.14159265358979323846;
// const int mod = 998244353;
const ll INF = 1e18;

template<typename T>
T chmax(T a, T b) {
    return a > b ? a : b;
}

template<typename T>
T chmin(T a, T b) {
    return a > b ? b : a;
}

const int N = (int)2e5 + 1, M = N * 2;

struct DSU{
    vector<int> node, size;
 
    DSU(){}
    DSU(int n){
        init(n);
    }
 
    void init(int n){
        node.resize(n);
        size.resize(n);
 
        for(int i = 0; i < n; i++){
            node[i] = i;
            size[i] = 1;
        }
    }
 
    int find(int x){
        return x == node[x] ? x : node[x] = find(node[x]);
    }
 
    void erase(int x){
        size[find(x)] -= 1;
        node[x] = x;
    }
 
    void relocate(int x, int y){ //can't be root
        int dx = find(x), dy = find(y);
        if(dx == dy){
            return;
        }
        node[x] = dy;
        size[dx] -= 1;
        size[dy] += 1;
    }
 
    int set(int x, int y){
        return find(x) == find(y);
    }
 
    int merge(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y){
            return 0;
        }
 
        size[x] += size[y];
        node[y] = x;
        return 1;
    }
 
    int heuristic_merge(int x, int y){ 
        x = find(x);
        y = find(y);
        if(size[x] < size[y]){
            swap(x, y);
        }
        return merge(x, y);
    }
 
    int get(int x){
        return size[find(x)];
    }
};

vector<vector<int>> get_span(vector<vector<int>> a, int n){
    sort(a.begin(), a.end());
    DSU dsu(n + 1);

    vector<vector<int>> b;
    for(int i = 0; i < a.size(); i++){
        int u = a[i][2], v = a[i][3];
        if(dsu.set(u, v)) continue;
        else{
            dsu.heuristic_merge(u, v);
            b.pb(a[i]); 
        }
    }
    return b;
}

void solve(){
    int n, m;
    cin >> n >> m;

    vector<vector<int>> a, b, vec;
    vector<int> deg(n + 1);

    for(int i = 0; i < m; i++){
        int u, v, w;
        cin >> u >> v >> w;

        if(i < n - 1){
            a.pb({w, 1, u, v, i + 1});
            vec.pb({w, 1, u, v, i + 1});
            deg[u]++;
            deg[v]++;
        }
        else{
            vec.pb({w, 2, u, v, i + 1});
        }
    }

    auto tr = get_span(vec, n);
    map<int, int> mp;

    for(int i = 0; i < a.size(); i++){
        for(int j = 0; j < tr.size(); j++){
            if(a[i][4] == tr[j][4]){
                mp[tr[j][4]] = 1;
            }
        }
    }

    while(1){
        for(int i = 0; i < a.size(); i++){
            if(!mp[a[i][4]]){
                for(int j = 0; j < tr.size(); j++){
                    deg[tr[j][2]]++;
                    deg[tr[j][3]]++;
                    deg[a[i][2]]--;
                    deg[a[i][3]]--;

                    if(mp[tr[j][4]] || !deg[a[i][2]]|| !deg[a[i][3]]){
                        deg[tr[j][2]]--;
                        deg[tr[j][3]]--;
                        deg[a[i][2]]++;
                        deg[a[i][3]]++;
                        continue;
                    }

                    b.pb({a[i][4], tr[j][4]});
                    mp[tr[j][4]] = 1;
                    break;
                }
            }
        }

        int qwq = 0;
        for(int i = 0; i < tr.size(); i++){
            if(mp[tr[i][4]] == 0){
                qwq = 1;
            }
        }
        if(!qwq) break;
    }

    cout << b.size() << endl;
    for(auto p : b){
        cout << p[0] << " " << p[1] << endl;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
//    cin >> t;

    while(t--){
        solve();
    }

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3808kb

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: 3600kb

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: 5ms
memory: 4060kb

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: 6ms
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: 6ms
memory: 4280kb

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: 3984kb

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: 4004kb

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: 3820kb

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: 1ms
memory: 3804kb

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: 0ms
memory: 3980kb

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: 0ms
memory: 4004kb

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 801
5 993
6 846
7 867
8 790
9 912
10 500
11 927
12 573
13 514
14 883
15 720
16 941
17 555
18 527
19 569
20 965
21 594
22 748
23 829
24 744
25 580
26 943
27 909
28 708
29 682
30 633
31 948
32 592
33 990
34 905
35 997
36 724
37 900
38 621
39 898
40 710
41 552
42 770
43 584
44 5...

result:

wrong answer not a spanning tree after swap 35