Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

一次调试,一次ac,有点小得意啊!附代码,仅供参考!

Posted by 1805180411 at 2012-10-25 15:34:47 on Problem 1611
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator