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

wa了,那位检查一下,想不通

Posted by cuiaoxiang at 2005-11-04 10:11:43 on Problem 1182
#include <iostream>
using namespace std;

int p[100000];

int Find(int x)
{
	int r, s;
	for (r = x; r != p[r]; r = p[r]);
	while (x != r)
		s = p[x], p[x] = r, x = s;
	return r;
}

void Union(int a, int b)
{
	int ra, rb;
	ra = Find(a);
	rb = Find(b);
	if (ra == rb)
		return;
	p[rb] = ra;
}

int main()
{
	int n, m, i, j, a, b, countV = 0;
	scanf("%d %d", &n, &m);
	for (i = 0; i < n + n; i++)
		p[i] = i;
	for (i = 0; i < m; i++)
	{
		scanf("%d %d %d", &j, &a, &b);
		if (j == 1)
		{
			if (a > n || b > n || Find(a - 1) == Find(b - 1 + n))
				countV++;
			else Union(a - 1, b - 1), Union(a - 1 + n, b - 1 + n);
		}
		else
		{
			if (a > n || b > n || Find(a - 1) == Find(b - 1))
				countV++;
			else Union(a - 1, b - 1 + n), Union(a - 1 + n, b - 1);
		}
	}
	printf("%d\n", countV);
	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