QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#721175#5548. Increase the Toll Feessubarudesu#WA 142ms11756kbC++141.3kb2024-11-07 15:28:462024-11-07 15:28:47

Judging History

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

  • [2024-11-07 15:28:47]
  • 评测
  • 测评结果:WA
  • 用时:142ms
  • 内存:11756kb
  • [2024-11-07 15:28:46]
  • 提交

answer

#include<iostream>
#include<algorithm>
using namespace std;
const long long N=2e5+5;
struct node{
    long long u,v,w,used;
}edge[N];
bool cmp(node x,node y){
    return x.w<y.w;
}
long long n,m,fa[N],Fa[N],tot1,tot2;
long long finded(long long x){
    if(fa[x]!=x)fa[x]=finded(fa[x]);
    return fa[x];
}
long long Finded(long long x){
    if(Fa[x]!=x)Fa[x]=Finded(Fa[x]);
    return Fa[x];
}
int main(){
    cin>>n>>m;
    for(long long i=1;i<=n;++i){
        fa[i]=i;Fa[i]=i;
    }
    for(long long i=1;i<=m;++i){
        cin>>edge[i].u>>edge[i].v>>edge[i].w;
        edge[i].used=0;
    }
    sort(edge+1,edge+m+1,cmp);
    long long cnt=n-1,now=0;
    while(cnt){
        now++;
        long long u=edge[now].u,v=edge[now].v,w=edge[now].w;
        if(Finded(u)!=Finded(v)){
            Fa[Finded(u)]=Finded(v);
            tot1+=w;
            cnt--;
            edge[now].used=1;
        }
    }
//    cout<<tot1;
    cnt=n-1,now=0;
    while(cnt){
        now++;
        if(now==m+1){
            cout<<"-1";
            return 0;
        }
        if(edge[now].used)continue;
        long long u=edge[now].u,v=edge[now].v,w=edge[now].w;
        if(finded(u)!=finded(v)){
            fa[finded(u)]=finded(v);
            tot2+=w;
            cnt--;
        }
    }
    cout<<tot2+n-1-tot1;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7700kb

input:

4 6
1 2 2
1 3 5
1 4 5
2 3 3
2 4 5
3 4 4

output:

9

result:

ok single line: '9'

Test #2:

score: 0
Accepted
time: 0ms
memory: 7768kb

input:

3 4
1 2 3
2 3 4
1 3 5
1 3 10

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 0ms
memory: 7724kb

input:

5 10
1 2 14
1 3 14
1 4 9
1 5 15
2 3 8
2 3 10
2 4 13
3 4 8
4 5 10
4 5 15

output:

21

result:

ok single line: '21'

Test #4:

score: 0
Accepted
time: 1ms
memory: 5680kb

input:

2 1
1 2 1

output:

-1

result:

ok single line: '-1'

Test #5:

score: 0
Accepted
time: 62ms
memory: 11756kb

input:

29171 100000
7223 21138 270743473
5598 27967 847631606
12666 26050 847631606
75 15747 270743473
8522 12955 847631606
6069 23750 270743473
18708 22605 847631606
16814 22169 847631606
11550 27119 847631606
562 15959 847631606
9400 11110 270743473
15325 23805 270743473
19169 24404 270743473
6649 12062 ...

output:

16827826868780

result:

ok single line: '16827826868780'

Test #6:

score: 0
Accepted
time: 142ms
memory: 11736kb

input:

47977 200000
10970 47321 440845807
1166 29708 767952745
319 37520 546280762
17581 29425 558321466
22079 26884 344816304
7479 44260 791002634
14685 44163 837529020
1537 10359 330017953
8634 27064 969738917
32950 37192 728271930
34751 42782 63025978
32540 34226 86057211
36786 46050 826927874
30444 436...

output:

-1

result:

ok single line: '-1'

Test #7:

score: -100
Wrong Answer
time: 38ms
memory: 7884kb

input:

28825 57648
9446 22014 286256842
14902 20222 14175
3246 20218 80493268
1783 13768 931622563
11107 24862 918832025
25070 27312 98899079
8535 20222 16037
9184 17491 294248461
8799 17834 456827944
1152 11687 960740527
17849 23045 9706
5774 21436 444202963
5417 23045 3092
20222 20370 11232
16585 20222 1...

output:

14315443217237

result:

wrong answer 1st lines differ - expected: '28822649262260', found: '14315443217237'