UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#214546#2709. Maximum Weightwanghanyu393356466ms4268kbC++112.3kb2024-11-19 22:16:312024-11-19 23:04:12

answer

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<numeric>
#include<map>
using namespace std;
const int N = 1e5 + 5;

class UnionFind{

    public:
    std::vector<int> par;
    std::vector<int> rank;
    std::vector<int> sz;
    std::vector<int> root;

    UnionFind(int size){
        rank=std::vector<int>(size+1,0);
        
        par = std::vector<int>(size+1,0);
        std::iota(par.begin(),par.end(),0);//#include<numeric>
        sz = std::vector<int>(size+1,1);
        root=std::vector<int>(size+1);
        std::iota(root.begin(),root.end(),0);
    }  

    

    ~UnionFind(){
        std::vector<int>().swap(rank);
        std::vector<int>().swap(par);
        std::vector<int>().swap(sz);
        std::vector<int>().swap(root);
    }  


    int find(int x){
        if(par[x]==x)return x;
        else return par[x] = find(par[x]);
    }

    bool issame(int u,int v){
        return find(u)==find(v);
    }


    void merge(int u,int v){
        u = find(u);
        v = find(v);
        if(u==v) return;

       
        if(rank[u]<rank[v]) std::swap(u,v);
        
        par[v]=u;
        sz[u]+=sz[v];
        if(rank[u]==rank[v])rank[u]++;

    }
    

    int size(int u){
        u=find(u);
        return sz[u];
    }

    std::map<int,std::vector<int>> element(){
        std::map<int,std::vector<int>> mp;
        for(int i=0;i<par.size();i++){
            root[i]=find(i);
            mp[root[i]].push_back(i);
        }
        return mp;

    }
};

struct edge{
    int u, v, c;
};
bool cmp(edge a, edge b){
    return a.c < b.c;
}

void solve(){
    int n, m;
    cin >> n >> m;
    vector<edge>e;
    for(int i = 1; i <= m; i++){
        int u, v, c;
        cin >> u >> v >> c;
        u--, v--;
        e.push_back({u, v, c});
    }
    sort(e.begin(), e.end(), cmp);
    UnionFind dsu(N);
    long long ans = 0;
    for(int i = 0; i < m; i++){
        if(!dsu.issame(e[i].u, e[i].v)){
            ans += e[i].c * dsu.size(e[i].u) * dsu.size(e[i].v);
            dsu.merge(e[i].u, e[i].v);
        }
    }
    cout << ans << '\n';
}

int main(){
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 5
Accepted
time: 5ms
memory: 2688kb

input:

5
200 200
87 21 609
97 9 566
169 28 893
181 137 280
139 67 622
78 186 503
104 59 24
58 7 460
2 5 972...

output:

16352732
16336938
18311542
17074338
17304585

result:

ok 5 lines

Test #2:

score: 5
Accepted
time: 0ms
memory: 2680kb

input:

5
200 200
138 19 453
178 159 227
89 99 937
38 60 806
145 45 448
169 146 982
180 153 382
73 167 608
1...

output:

17101828
17550376
16746021
17786837
16222316

result:

ok 5 lines

Test #3:

score: 5
Accepted
time: 5ms
memory: 2684kb

input:

5
200 200
140 37 428
167 191 722
18 70 854
12 63 869
40 107 253
146 21 701
198 126 594
55 183 67
2 4...

output:

16466132
16907763
17447896
17281330
17606236

result:

ok 5 lines

Test #4:

score: 5
Accepted
time: 0ms
memory: 2684kb

input:

5
200 200
187 173 189
59 103 160
12 143 13
16 115 179
130 108 854
7 39 124
164 72 407
66 200 625
105...

output:

16551712
17073788
18382571
16125810
17394218

result:

ok 5 lines

Test #5:

score: 5
Accepted
time: 5ms
memory: 2680kb

input:

5
200 200
29 8 307
152 26 149
114 137 559
9 146 81
179 5 203
3 150 8
26 81 660
82 69 356
60 51 591
1...

output:

17720164
16949531
17616778
17620129
16288612

result:

ok 5 lines

Test #6:

score: 5
Accepted
time: 0ms
memory: 2684kb

input:

5
200 200
35 144 182
145 194 241
163 140 918
102 193 228
167 130 338
40 65 646
22 1 238
131 120 122
...

output:

17829792
17475817
17019069
17199330
17653988

result:

ok 5 lines

Test #7:

score: 0
Wrong Answer
time: 450ms
memory: 4264kb

input:

5
53735 100000
39892 35843 390
26908 36823 655
6371 23805 896
22798 20233 34
26039 37466 620
22807 2...

output:

732544305334
2707620787988
382579317384
1280102815136
1224736660383

result:

wrong answer 2nd lines differ - expected: '2754865428244', found: '2707620787988'

Test #8:

score: 0
Wrong Answer
time: 569ms
memory: 4264kb

input:

5
10229 100000
7299 6840 113
6306 8765 160
5192 8138 791
6357 5200 186
9344 6620 230
2728 5157 294
1...

output:

5404797207
20352728657
353943098727
1920173966646
21748452142

result:

wrong answer 4th lines differ - expected: '1937353835830', found: '1920173966646'

Test #9:

score: 0
Wrong Answer
time: 443ms
memory: 4264kb

input:

5
57846 100000
40438 9781 106
35677 46516 444
39104 10920 598
14172 5965 925
10993 42212 964
34922 5...

output:

904138784672
1221318148591
3149624405322
13466143626
117116026446

result:

wrong answer 2nd lines differ - expected: '1229908083183', found: '1221318148591'

Test #10:

score: 0
Wrong Answer
time: 442ms
memory: 4264kb

input:

5
83866 100000
54676 45243 758
42970 31099 721
27422 62458 4
64200 18592 974
7505 20672 900
83354 49...

output:

2556792784945
123598549967
873449242705
163261591501
6530129535

result:

wrong answer 1st lines differ - expected: '2595447490609', found: '2556792784945'

Test #11:

score: 0
Wrong Answer
time: 436ms
memory: 4264kb

input:

5
10818 100000
6480 6451 261
10486 2548 495
8142 9400 330
4568 1781 682
8289 5945 660
1058 5902 378
...

output:

6395040382
453510144589
1146821490214
1976570152991
2123018768142

result:

wrong answer 4th lines differ - expected: '1989455054879', found: '1976570152991'

Test #12:

score: 0
Wrong Answer
time: 429ms
memory: 4268kb

input:

5
28267 100000
6205 10002 70
12719 28165 6
6174 15544 940
3052 1398 62
648 16984 103
17068 3616 397
...

output:

112235086092
704232646899
3205263357093
5409166707
847127748242

result:

wrong answer 3rd lines differ - expected: '3480141264037', found: '3205263357093'

Test #13:

score: 0
Wrong Answer
time: 430ms
memory: 4264kb

input:

5
33283 100000
134 21400 430
22159 6741 87
2473 18253 422
13630 28077 144
22002 24779 437
13034 2417...

output:

181764918984
10494375881
2797318740487
2770655186674
1939716496878

result:

wrong answer 3rd lines differ - expected: '2904692922887', found: '2797318740487'

Test #14:

score: 0
Wrong Answer
time: 430ms
memory: 4268kb

input:

5
95017 100000
37418 35411 167
6415 94231 616
3744 60077 591
73070 5537 37
50016 18446 825
55281 519...

output:

3246038436549
493802436522
2993477705353
254959337484
400341250283

result:

wrong answer 1st lines differ - expected: '3761434512069', found: '3246038436549'

Test #15:

score: 5
Accepted
time: 595ms
memory: 4268kb

input:

5
35374 100000
7390 8869 566
34086 23636 990
32887 4674 494
6452 18007 424
12723 34687 25
11020 3193...

output:

217535971486
821519157137
1089950355527
203558732186
222284648865

result:

ok 5 lines

Test #16:

score: 0
Wrong Answer
time: 447ms
memory: 4268kb

input:

5
99901 100000
35090 96613 813
67573 26667 941
3022 45474 629
10290 52654 196
12701 59286 893
80077 ...

output:

2027872076538
2478803250188
2603423626008
861236891829
1043488374786

result:

wrong answer 1st lines differ - expected: '4639212192506', found: '2027872076538'

Test #17:

score: 0
Wrong Answer
time: 432ms
memory: 4268kb

input:

5
60029 100000
51021 51437 658
17839 25439 501
51521 1893 516
20462 309 128
58519 8607 623
34157 498...

output:

1003441431151
1687189476670
1791163494281
18745776112
210354312422

result:

wrong answer 2nd lines differ - expected: '1691484443966', found: '1687189476670'

Test #18:

score: 0
Wrong Answer
time: 457ms
memory: 4268kb

input:

5
25779 100000
2274 4201 54
11775 9137 148
697 21064 449
14126 25608 840
5579 19585 779
9641 15433 5...

output:

85132266903
182947677225
1097763476065
2629190294807
2770076597531

result:

wrong answer 4th lines differ - expected: '2659255065879', found: '2629190294807'

Test #19:

score: 0
Wrong Answer
time: 473ms
memory: 4268kb

input:

5
33220 100000
31269 1383 683
18955 25297 299
700 12197 974
27221 20329 796
29285 29654 414
21164 21...

output:

179906133251
3124645757814
3283991178524
3220773343248
1387185986674

result:

wrong answer 2nd lines differ - expected: '3317919286134', found: '3124645757814'

Test #20:

score: 0
Wrong Answer
time: 418ms
memory: 4264kb

input:

5
17719 100000
11733 10279 4
15938 11631 578
9235 13763 91
13846 2450 912
12426 13022 236
13098 1628...

output:

27833343898
883915472421
13546738134
16496672955
438926144015

result:

wrong answer 2nd lines differ - expected: '888210439717', found: '883915472421'