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 Iambitious at 2006-11-02 10:44:40 on Problem 1182
In Reply To:帮忙看看我这个想法对不对啊 Posted by:Iambitious at 2006-10-23 16:22:28
#include <iostream>
#include <cstdio>
using namespace std;

int same[50001];
int eat[50001];
int eaten[50001];

int par(int cur)
{
	while(same[cur] != cur){
		cur = same[cur];
	}
	return cur;
};
int merge(int a, int b)
{
	int pa = par(a);
	int pb = par(b);
	if(pa < pb){
		same[pb] = pa;
		return pa;
	}
	else{
		same[pa] = pb;
		return pb;
	}
};

int main()
{
	int n,k;
	int which,a,b;
	int i;
	int res;

	freopen("g:\\in.txt","r",stdin);
	res = 0;
	scanf("%d%d", &n, &k);
	for(i = 0; i <= n; i++){
		same[i] = i;
	}
	memset(eat, 0, n + 1);
	memset(eaten, 0, n + 1);
	for(i = 1; i <= k; i++){
		scanf("%d%d%d", &which, &a, &b);
		if(a > n || b > n){
			res++;
			continue;
		}
		if(which == 1){
			if(par(a) == par(b)){
				continue;
			}
			if(eat[a] > 0 && par(eat[a]) == par(b)){
				res++;
				continue;
			}
			if(eat[b] > 0 && par(eat[b]) == par(a)){
				res++;
				continue;
			}
			merge(a,b);
			if(eat[a] > 0 && eat[b] > 0){
				merge(eat[b], eat[b]);
			}
			else if(eat[a] > 0){
				eat[b] = par(eat[a]);
			}
			else if(eat[b] > 0){
				eat[a] = par(eat[b]);
			}
			if(eaten[a] > 0 && eaten[b] > 0){
				merge(eaten[a], eaten[b]);
			}
			else if(eaten[a] > 0){
				eaten[b] = par(eaten[a]);
			}
			else if(eaten[b] > 0){
				eaten[a] = par(eaten[b]);
			}
		}
		else if(which == 2){
			if(eat[a] > 0 && par(eat[a]) == par(b)){
				continue;
			}
			if(par(a) == par(b)){
				res++;
				continue;
			}
			if(eat[b] > 0 && par(eat[b]) == par(a)){
				res++;
				continue;
			}
			if(eat[a] > 0){
				merge(eat[a], b);
			}
			else{
				eat[a] = par(b);
			}
			if(eaten[b] > 0){
				merge(a, eaten[b]);
			}
			else{
				eaten[b] = par(a);
			}
			if(eaten[a] > 0 && eat[b] > 0){
				merge(eaten[a], eat[b]);
			}
			else if(eaten[a] > 0){
				eat[b] = par(eaten[a]);
			}
			else if(eat[b] > 0){
				eaten[a] = par(eat[b]);
			}
		}
	}
	printf("%d\n", res);
	return 0;
}
各位大侠帮我看看哪错了吧,都wa了2周了。谢谢谢谢!

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