Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
不知为啥错了,代码如下,求大牛指点迷津~~#include<iostream> using namespace std; int set[30003]; void init_set(int n) { for(int i=0;i<=n;i++) set[i]=i; } int find(int x) { int r=x; while(set[r]!=r) r=set[r]; int i=x; while(i!=r) { int j=set[i]; set[i]=r; i=j; } return r; } void merge(int a,int b) { int fa=find(a); int fb=find(b); if(fb<fa) set[fa]=fb; else if(fa<fb) set[fb]=fa; } void inputMerge() { int k; cin>>k; int a,b; cin>>a; k--;; while(k--) { cin>>b; merge(a,b); } } int find0(int n) { int sum=0; for(int i=0;i<=n;i++) if(set[i]==0) sum++; return sum; } int main() { int n,m; while(cin>>n>>m,n||m) { init_set(n); while(m--) inputMerge(); cout<<find0(n)<<endl; } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator