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

为什么是RE?

Posted by yeknight at 2009-09-13 22:46:28 on Problem 1611
#include<iostream>
using namespace std;


int Find(int x,int S[])
{
	if(S[x] < 0)
		return x;
	else
		return S[x] = Find(S[x],S);
}

void SetUnion(int S[],int root1,int root2)
{
	int max,min;
	root1 = Find(root1,S);
	root2 = Find(root2,S);
	max = root1 > root2 ? root1 : root2;
	min = root1 > root2 ? root2 : root1;
	S[max] = min;
}

void MakeSet(int S[],int n)
{
	int i;
	for(i = 0; i < n; i++)
		S[i] = -1;
}

int set[30001];
int main()
{
	int m,n;
	int k,i,j,mm,nn;
	int total;
	while(cin>>m>>n)
	{
		total = 0;
		if((0 == m) && (0 == n))
			break;
		MakeSet(set,m);
		for(i = 0; i < n; i++)
		{
			cin>>k;
			for(j = 0; j < k; j++)
			{
				cin>>mm;
				if(j)
					SetUnion(set,nn,mm);
				nn = mm;
			}
		}
		for(i = 0; i < m; i++)
		{
			Find(i,set);
		}
		for(i = 0; i < m; i++)
		{
			if(Find(0,set) == Find(i,set))
				total++;
		}
		cout<<total<<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