求助,最后一个点没过

P3366 【模板】最小生成树

sunpengyu @ 2023-08-30 10:24:43

懒得判断orz了,因为我发现没有一个样例有orz

#include <bits/stdc++.h>
#define MAXN 200010
using namespace std;
int n,m,tot,sum;
int fa[MAXN];
struct nond{
    int x,y,z;
}edge[MAXN];
int cmp(nond a,nond b){
    return a.z<b.z;
}
int find(int x){
    if(fa[x]==x)    return fa[x];
    else return fa[x]=find(fa[x]);
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=m;i++) scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].z);
    sort(edge+1,edge+1+m,cmp);
    for(int i=1;i<=m;i++){
        int dx=find(edge[i].x);
        int dy=find(edge[i].y);
        if(dx==dy)    continue;
        fa[dy]=dx;
        sum+=edge[i].z;
        tot++;
        if(tot==n-1)    break;
    }
    cout<<sum;
}

by lraM416 @ 2023-08-30 10:26:38

@sunpengyu ?谁叫你不判的


by Iniaugoty @ 2023-08-30 10:26:42

Wrong Answer.wrong answer On line 1 column 1, read 1, expected o.

这他妈就是要判 orz 吧……


by sunpengyu @ 2023-08-30 10:28:29

@Algae 6


by sunpengyu @ 2023-08-30 10:30:35

@Algae 为啥判了也没过

#include <bits/stdc++.h>
#define MAXN 200010
using namespace std;
int n,m,tot,sum;
int fa[MAXN];
struct nond{
    int x,y,z;
}edge[MAXN];
int cmp(nond a,nond b){
    return a.z<b.z;
}
int find(int x){
    if(fa[x]==x)    return fa[x];
    else return fa[x]=find(fa[x]);
}
int main(){
    bool flag=1;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=m;i++) scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].z);
    sort(edge+1,edge+1+m,cmp);
    for(int i=1;i<=m;i++){
        int dx=find(edge[i].x);
        int dy=find(edge[i].y);
        if(dx==dy)    continue;
        fa[dy]=dx;
        sum+=edge[i].z;
        flag=0;
        tot++;
        if(tot==n-1)    break;
    }
    if(flag){
        cout<<"orz";
        return 0;
    }
    cout<<sum;
    return 0;
}

by lraM416 @ 2023-08-30 10:31:42

@sunpengyu 判错了,应该是若tot!=n-1


by sunpengyu @ 2023-08-30 10:32:17

@Algae 好的


|