QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#534564#7069. Farmxiojoy#WA 245ms13456kbC++201.6kb2024-08-27 13:54:282024-08-27 13:54:29

Judging History

This is the latest submission verdict.

  • [2024-08-27 13:54:29]
  • Judged
  • Verdict: WA
  • Time: 245ms
  • Memory: 13456kb
  • [2024-08-27 13:54:28]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
int n,m,q;
const int N = 1e5+10;
const int M = 5e5+10;
int f[N],to[M];
int tot,ans,tt;
struct Node{
    int x,y,id;
    int z;
}e[M];
bool cmp(Node a,Node b){
    return a.z<b.z;
}
int find(int x){
    if(f[x]!=x) f[x]=find(f[x]);
    return f[x];
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        cin>>e[i].x>>e[i].y>>e[i].z;
        e[i].id=i;
    }
    sort(e+1,e+1+m,cmp);
    for(int i=1;i<=m;i++){
        to[e[i].id]=i;
    }
    cin>>q;
    while(q--){
        ans=1e9+7;
        for(int i=1;i<=n;i++){
            f[i]=i;
        }
        int tot=0;
        tt=0;
        int u,v;
        cin>>u>>v;
        tot+=e[to[u]].z;
        f[e[to[u]].x]=e[to[u]].y;
        if(e[to[u]].x!=e[to[u]].y) tt++;
        for(int i=1;i<=m;i++){ 
            int fx=find(e[i].x);
            int fy=find(e[i].y);
            if(fx==fy) continue;
            f[fy]=fx;
            tot+=e[i].z;
            tt++;
        }
        if(tt==n-1) ans=min(ans,tot);
        for(int i=1;i<=n;i++){
            f[i]=i;
        }
        tot=0;
        tt=0;
        tot+=e[to[v]].z;
        f[e[to[v]].x]=e[to[v]].y;
        if(e[to[v]].x!=e[to[v]].y) tt++;
        for(int i=1;i<=m;i++){
            int fx=find(e[i].x);
            int fy=find(e[i].y);
            if(fx==fy) continue;
            f[fx]=fy;
            tot+=e[i].z;
            tt++;
        }
        if(tt==n-1)ans=min(ans,tot);
        if(ans!=1e9+7) cout<<ans<<endl;
        else cout<<-1<<endl;
    }
}

詳細信息

Test #1:

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

input:

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

output:

11

result:

ok single line: '11'

Test #2:

score: -100
Wrong Answer
time: 245ms
memory: 13456kb

input:

100000 500000
2516 13348 191
37713 25720 216
41568 13765 877
2116 27917 895
76904 65435 37
73053 24687 44
97127 44338 700
2251 85769 378
95166 20208 42
59303 57463 158
26863 18030 31
58613 6818 2
15455 18106 254
3232 13720 610
85677 16778 650
25618 72746 813
80365 162 47
10930 7403 645
79272 54568 6...

output:


result:

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