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

不知道错在什么地方?请明白的同学帮忙看看逻辑有没有问题,谢谢:)

Posted by letaotao at 2007-06-11 20:16:56 on Problem 1182
// 1182.cpp : 定义控制台应用程序的入口点。
//

#include <iostream>
#include <string>
using namespace std;

#define MAX_N 60000

int parent[MAX_N];
int eat[MAX_N];
int self[MAX_N];
int eaten[MAX_N];
int weight[MAX_N];

void MakeSet()
{
	memset(parent , 0 , sizeof(int)*MAX_N);
	memset(eat , 0 , sizeof(int)*MAX_N);
	//memset(self , 0 , sizeof(int)*MAX_N);
	memset(eaten , 0 , sizeof(int)*MAX_N);
	for(int i = 1 ; i < MAX_N ; i++)
	{
		weight[i] = 1;
	}
}

int wUnion(int U , int V)
{
	int newRoot;
	if((U == 0 && V == 0) || U == V)
		return U;
	if(weight[U] >= weight[V])
	{
		weight[U] += weight[V];
		parent[V] =  U ;
		parent[U] = 0;
		newRoot = U;
	}
	else
	{
		weight[V] += weight[U];
		parent[U] = V;
		parent[V] = 0;
		newRoot = V;
	}
	return newRoot;
}

int cFind(int U )
{
	if(U == 0)
		return 0;
	int oldParent = parent[U];
	if(oldParent == 0 || oldParent == U)
		return U;
	int node = U;
	while(parent[node] != 0 && parent[node]!= node)
	{
		node = parent[node];
	}
	if(oldParent != node)
		parent[U] = node;
	return node;
}


bool isTheSameClass(int U , int V)
{
	int rootU = cFind(U);
	int rootV = cFind(V);
	if(rootU == 0 || rootV == 0)
		return false;
	else if(rootU == rootV) 
		return true;
	else
		return false;
}
int main(int argc, char* argv[])
{
	int N , K , isEat , X , Y;
	while(cin >> N >> K)
	{
		int lies , i;
		lies = 0;
		MakeSet();
		for(i = 0 ; i < K ; i++)
		{
			cin >> isEat >> X >> Y;
			if(X > N || Y > N)
			{
				lies++;
				continue;
			}
			if(isEat == 1)
			{
				if(isTheSameClass(eat[X] , Y) || isTheSameClass(eaten[X] , Y) || isTheSameClass(X , eat[Y]) || isTheSameClass(X , eaten[Y]))
				{
					lies++;
					continue;
				}
				else
				{
					int root1 , root2 , newRoot;

					root1 = eat[X];root2 = eat[Y];
					newRoot = wUnion(root1 , root2);
					eat[X] = eat[Y] = newRoot;

					root1 = eaten[X];root2 = eaten[Y];
					newRoot = wUnion(root1 , root2);
					eaten[X] = eaten[Y] = newRoot;

					root1 = X;root2 = Y;
					newRoot = wUnion(root1 , root2);
					//parent[X] = parent[Y] = newRoot;
				}
			}
			if(isEat == 2)
			{
				if(X == Y )
				{
					lies++;
					continue;
				}
				if(isTheSameClass(X , Y) || isTheSameClass(X , eat[Y]) || isTheSameClass(eaten[X] , Y))
				{
					lies++;
					continue;
				}
				else
				{
					int root1 , root2 , newRoot;
					root1 = eaten[X];root2 = eat[Y];
					newRoot = wUnion(root1 , root2);
					eaten[X]= eat[Y] = newRoot;

					root1 = X;root2 = eaten[Y];
					newRoot = wUnion(root1 , root2);
					eaten[Y] = newRoot;
					//parent[X]= eaten[Y] = newRoot;

					root1 = eat[X];root2 = Y;
					newRoot = wUnion(root1 , root2);
					eat[X] =  newRoot;
					//eat[X] = parent[Y] = newRoot;
				}
			}
		}
		cout << lies<< 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