这个代码为什么过不了啊?

P1195 口袋的天空

@[pengruxia](/user/620232) `sort(edge+1,edge+1+n,cmp)` -> `sort(edge+1,edge+1+m,cmp)`
by ieeqwq @ 2023-04-20 14:38:55


@[pengruxia](/user/620232) ``` #include<iostream> #include<algorithm> using namespace std; struct node { int x,y,v; } edge[200005]; int pa[200005]; bool cmp(node x,node y) { return x.v<y.v; } int find(int x) { if(pa[x]==x) return x; return pa[x]=find(pa[x]); } void init(int n) { for(int i=1; i<=n; i++) { pa[i]=i; } } int main() { int n,m,ans=0,cnt=0,k; cin>>n>>m>>k; init(n); for(int i=1; i<=m; i++) { cin>>edge[i].x>>edge[i].y>>edge[i].v; } // sort(edge+1,edge+1+n,cmp); sort(edge+1,edge+1+m,cmp); for(int i=1; i<=m; i++) { int xx=find(edge[i].x); int yy=find(edge[i].y); if(xx!=yy) { pa[xx]=yy; ans+=edge[i].v; cnt++; } if(cnt>=n-k) { break; } } if(cnt>=n-k) cout<<ans; else cout<<"No Answer"; // cout<<ans<<" "<<n-k; return 0; } ```
by VitrelosTia @ 2023-04-20 14:53:20


谢谢
by prx1460 @ 2023-04-21 13:07:16


|