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 |
一次调试,一次ac,有点小得意啊!附代码,仅供参考!#include<iostream> using namespace std; int father[50000]; int num[50000]; int groupnum,groupmember; int student; int find(int a) { if(father[a] == a) return a; else return father[a] = find(father[a]); } void init() { for(int i = 0;i < student;i++) { father[i] = i; num[i] = 1; } } int main() { int i,j; while(cin>>student>>groupnum) { if(!student && !groupnum) break; init(); int a,b; for(i = 0;i < groupnum;i++) { cin>>groupmember>>a; int x = find(a); for(j = 1;j < groupmember;j++) { cin>>b; int y = find(b); if(x != y) { father[y] = x; num[x] = num[x] + num[y]; } } } cout<<num[find(0)]<<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