莫名其妙求调

P3366 【模板】最小生成树

_Hongshi_ @ 2024-11-19 19:34:17


#include <bits/stdc++.h>
using namespace std;
//const int N=1e6+5;
int f[5005];
int n,m,t1,t2,ans,anse,tmp;
int find(int a){
    if(f[a]==0){
        return a;
    }
    else{
        return f[a]=find(f[a]);
    }
}

struct node{
    int a,b,w,fi,ed;
    bool operator<(node oth)const{
        return w<oth.w;
    }
};
vector<node>e;
int main(){
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        e.push_back({a,b,c});
    }
    sort(e.begin(),e.end());
    for(auto i:e){
        t1=find(i.a);
        t2=find(i.b);
        if(t1!=t2){
            tmp++;
            e[tmp].fi=i.a;
            e[tmp].ed=i.b;
            ans+=i.w;
            f[t1]=t2;
            anse++;
        }
    }
    if(tmp==n-1) cout<<ans;//<<endl;
    else cout<<"orz";
    return 0;
}
111

|