求助 最朴素的kruscal为什么一直wa啊...

P3366 【模板】最小生成树

???我的代码咋成这样了。。。
by ʚ卡兔琳娜ɞ @ 2018-10-25 20:28:56


希望更丰富的展现?使用Markdown
by 雷人 @ 2018-10-25 20:29:04


~~您的头像好酷啊~~
by _stellar @ 2018-10-25 20:30:03


帖子怎么删???~~真是太丢人了~~
by ʚ卡兔琳娜ɞ @ 2018-10-25 20:30:43


@[katarina0v0](/space/show?uid=114846) 好像没有判断不连通的情况呢……
by Narcisuss @ 2018-10-25 20:31:39


``` include<bits/stdc++.h> using namespace std; define maxn 200050 int n,m,fa[maxn]; long long ans; struct mp { int x,y,val; } a[maxn]; bool cmp(mp a,mp b) { return a.val<b.val; } int find(int x) { if(fa[x]==x) return x; else return fa[x]=find(fa[x]); } void unionn(int x,int y) { x=find(x); y=find(y); if(x!=y) fa[y]=x; } int main() { scanf("%d""%d",&n,&m); for(int i=1; i<=m; i++) { scanf("%d""%d""%d",&a[i].x,&a[i].y,&a[i].val); } sort(a+1,a+1+n,cmp); for(int i=1; i<=n; i++) fa[i]=i; int k=0; for(int i=1; i<=m; i++) { if(find(a[i].x)!=find(a[i].y)) { unionn(a[i].x,a[i].y); k++; ans+=a[i].val; } if(k==n-1) break; } printf("%lld",ans); return 0; } ```
by _stellar @ 2018-10-25 20:31:59


``` #include<bits/stdc++.h> using namespace std; #define maxn 200050 int n,m,fa[maxn]; long long ans; struct mp{ int x,y,val; }a[maxn]; bool cmp(mp a,mp b){ return a.val<b.val;} int find(int x) { if(fa[x]==x) return x; else return fa[x]=find(fa[x]); } void unionn(int x,int y) { x=find(x);y=find(y); if(x!=y) fa[y]=x; } int main(){ scanf("%d""%d",&n,&m); for(int i=1;i<=m;i++) { scanf("%d""%d""%d",&a[i].x,&a[i].y,&a[i].val); } sort(a+1,a+1+n,cmp); for(int i=1;i<=n;i++) fa[i]=i; int k=0; for(int i=1;i<=m;i++) { if(find(a[i].x)!=find(a[i].y)) {unionn(a[i].x,a[i].y); k++; ans+=a[i].val; } if(k==n-1) break; } printf("%lld",ans); return 0; } ```
by ʚ卡兔琳娜ɞ @ 2018-10-25 20:32:13


~~哈哈哈我成功贴上代码了~~
by ʚ卡兔琳娜ɞ @ 2018-10-25 20:33:11


不判不连通也能过此题666
by tiandong123 @ 2018-10-25 20:33:57


@[Narcisuss](/space/show?uid=113464) 可是好像没有不连通的情况诶...而且我是全wa TvT
by ʚ卡兔琳娜ɞ @ 2018-10-25 20:34:03


| 下一页