如果你莫名其妙RE,且数组没开小

P3366 【模板】最小生成树

????
by c_subtract_subtract @ 2024-10-29 18:15:15


@[Small_Traveler](/user/565852) 感谢大佬
by dapipi3878 @ 2024-10-29 18:15:47


代码顺便发: ```cpp #include<iostream> #include<algorithm> #define int long long using namespace std; const int N=1e7; int n,m,f[N],ans; struct Node{int u,v,n;}a[N*10]; int find(int x){return f[x]==x?x:f[x]=find(f[x]);} void unit(int x,int y){f[find(x)]=find(y);} bool cmp(Node x,Node b){return x.n<b.n;} signed main() { cin>>n>>m; for(int i=1;i<=n;i++) f[i]=i; for(int i=1;i<=m;i++) cin>>a[i].u>>a[i].v>>a[i].n; sort(a,a+m+1,cmp); int cnt=0; for(int i=1;i<=m;i++) { int x=find(a[i].u),y=find(a[i].v); if(x!=y) { ans+=a[i].n; f[y]=x; cnt++; unit(x,y); } if(cnt==n-1) break; } if(cnt!=n-1) cout<<"orz"; else cout<<ans; } ```
by c_subtract_subtract @ 2024-10-29 18:16:06


|