只有四十分

P3366 【模板】最小生成树

这个数据大小,,,额,边是200000条无向边
by violinwang @ 2018-04-10 23:16:19


推荐代码: ```cpp #include <bits/stdc++.h> #define M 200000+3 #define N 5100 using namespace std; struct node{int u,v,w;}e[M]; int fa[N],m,n; int k=0; bool vis[M]; void ad (int u,int v,int w){ k++;e[k].u=u;e[k].v=v;e[k].w=w; } bool cmp(node aa,node bb){return aa.w<bb.w;} void cs(){ for (int i=1;i<=n;++i) fa[i]=i; } int fnd(int x){ if (fa[x]==x) return x; return fa[x]=fnd(fa[x]); } void un(int x,int y){ int fx=fnd(x),fy=fnd(y); if (fx!=fy) fa[fx]=fy; } int main (){ scanf ("%d%d",&n,&m); int u,v,w; for (int i=1;i<=m;++i){ scanf ("%d%d%d",&u,&v,&w); ad(u,v,w); } sort(e+1,e+m+1,cmp); int mst=0,bn=0; cs(); for (int i=1;i<=m;++i){ // printf("%d",i); if (fnd(e[i].u)!=fnd(e[i].v)){ mst+=e[i].w;bn++; un(e[i].u,e[i].v); if(bn==n-1) break; } } if (bn!=n-1) puts("orz"); else printf("%d",mst); return 0; } ``` 看不懂的话可以问
by violinwang @ 2018-04-10 23:24:18


用前向星
by violinwang @ 2018-04-10 23:25:31


@[百里雨晴](/space/show?uid=65458) 数组开小了。 这像是 P1546 的代码?
by Anguei @ 2018-04-11 04:36:23


@[violinwang](/space/show?uid=76204) 谢谢
by 百里雨晴 @ 2018-04-11 21:27:31


@[yyfcpp](/space/show?uid=53062) 谢谢,您的大名早有耳闻
by 百里雨晴 @ 2018-04-11 21:29:41


@[violinwang](/space/show?uid=76204) 呵呵,没学过#define
by 百里雨晴 @ 2018-04-11 21:32:34


|