默写模板,不知道哪里错了

P3366 【模板】最小生成树

mayijian @ 2023-10-18 18:42:17

rt

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+5;
int f[maxn], n, m;
struct nd{
    int x, y, val;
}a[maxn];
int find(int x){
    while(x!=f[x]) x=f[x]=f[f[x]];
    return x;
}
bool cmp(nd a, nd b){
    return a.val<b.val;
}
void merge(int x, int y){
    f[find(x)] = find(y);
}

bool check(int x, int y){
    return (find(x)==find(y));

}

int main(){
    cin>>n>>m;
    for(int i=1; i<=n; i++) f[i] = i;
    for(int i=1; i<=m; i++){
        int x, y, z;
        cin>>x>>y>>z;
        a[i].x=x, a[i].y=y, a[i].val=z;
    }
    sort(a+1, a+1+m, cmp);
    int cnt=0, ans=0;
    for(int i=1; i<=m; i++){
        if(check(a[i].x, a[i].y)) continue;
        ans+=a[i].val;
        merge(a[i].x, a[i].y);
        if(++cnt = n-1){
            break;
        }
    }
    if(cnt != n-1) cout<<"orz";
    else cout<<ans;
//  for(int i=1;i<=m;i++){
//      cout<<a[i].x<<" "<<a[i].y<<" "<<a[i].val<<endl;
//      cout<<f[i];
    }
}

by LgxTpre @ 2023-10-18 18:45:08

if(++cnt = n-1){

/jy


by mayijian @ 2023-10-18 20:59:33

若至了


|