帮个忙,样例都过不了

P3366 【模板】最小生成树

MWL_wma @ 2023-11-24 21:04:05


#include<bits/stdc++.h>
using namespace std;
int n,m,fa[5002],z,n1;
struct UIiyTYYTTYytyiyTYIGFB{
    int v;
    int x,y;
}l[200002];
bool px(UIiyTYYTTYytyiyTYIGFB a,UIiyTYYTTYytyiyTYIGFB b){return a.v<b.v;}
int find(int x){
    if(x==fa[x]){
        return x;
    }
    return fa[x]=find(fa[x]);
}
int main(){
    scanf("%d%d",&n,&m);
    n1=n;
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&l[i].x,&l[i].y,& l[i].v);
    }
    for(int i=1;i<=n;i++){
        fa[i]=i;
    }
    sort(l+1,l+1+m,px);
    for(int i=1;i<=m;i++){
        int x1=find(l[i].x),y1=find(l[i].y);
        if(x1-y1){
            --n1;
            fa[l[i].x]=y1;
            z+=l[i].v;
        }
    }
    if(n1>1){
        printf("%d",z);
    }else{
        puts("orz");}
    return 0;
}

by FL_sleake @ 2023-11-24 21:05:49

if(x1-y1)

by MWL_wma @ 2023-11-24 21:06:20

@FL_sleake ?


by Special_Tony @ 2023-11-24 21:07:43

@yma_y_wma 他是指这一句错了啊


by __LYC__qwq @ 2023-11-24 21:08:34

#include<bits/stdc++.h>
using namespace std;
int n,m,fa[5002],z,n1;
struct UIiyTYYTTYytyiyTYIGFB{
    int v;
    int x,y;
}l[200002];
bool px(UIiyTYYTTYytyiyTYIGFB a,UIiyTYYTTYytyiyTYIGFB b){return a.v<b.v;}
int find(int x){
    if(x==fa[x]){
        return x;
    }
    return fa[x]=find(fa[x]);
}
int main(){
    scanf("%d%d",&n,&m);
    n1=n;
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&l[i].x,&l[i].y,& l[i].v);
    }
    for(int i=1;i<=n;i++){
        fa[i]=i;
    }
    sort(l+1,l+1+m,px);
    for(int i=1;i<=m;i++){
        int x1=find(l[i].x),y1=find(l[i].y);
        if(x1-y1){
            --n1;
            fa[x1]=y1;
            z+=l[i].v;
        }
    }
    if(n1 < 2){
        printf("%d",z);
    }else{
        puts("orz");}
    return 0;
}

by __LYC__qwq @ 2023-11-24 21:09:28

n1 > 1 && fa[l[i].x]=y1;


by FL_sleake @ 2023-11-24 21:10:17

@yma_y_wma 改了3个点

if(x1-y1) ---> if(x1!=y1)

fa[l[i].x]=y1;---> fa[x1]=y1

if(n1>1) --->  if(n1==1)

应该就能过了


by MWL_wma @ 2023-11-24 21:10:44

@LYCqwq THK


by Po7ed @ 2023-11-24 21:11:58

@yma_y_wma 应是

if(n1==1){
        printf("%d",z);
    }else{
        puts("orz");}

罢。 按我理解 n1 表示当前连通块数量,那不应该是连通时连通块数量为 1 时输出边权和吗?


by MWL_wma @ 2023-11-24 21:12:31

@FL_sleake THK

此贴结


|