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 zerotest at 2011-03-31 02:33:57 on Problem 1182
#include <bitset>
#include <map>
#include <queue>
#include <string.h>
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
#define N 50010
int py[N];
int dr[N];//dr[i]: i的敌人是dr[i],即dr[i]吃i
int n;
int find(int x)//找到祖先
{
	int r = x;
	while (py[r] != r)
	{
		r = py[r];
	}
	int t = x;
	while (py[t] != r)
	{
		py[t] = t;
	}
	return r;
}
int guanxi(int x, int y)
{
	int rx = find(x);
	int ry = find(y);
	if (rx == ry) return 1;//朋友
	else if (dr[rx] != -1 || dr[ry] != -1)//非朋友
	{
		if (dr[rx] == ry)//ry吃rx
		{
			return 3;//x是y的食物
		}
		else
		{
			return 2;//y是x的食物
		}
	}
	else return 0;//没有关系
}
void update(int r, int x, int y)
{
	int rx = find(x);
	int ry = find(y);
	switch (r)
	{
		case 1:
			if (rx < ry)
			{
				py[ry] = rx;
			}
			break;
		case 2:
			dr[ry] = rx;//rx吃ry
			if (dr[rx] != -1)//rx有敌人
			{
				dr[dr[rx]] = ry;
			}
			break;
	}
}
int f(int r, int x, int y)
{
	if (r == 2 && x == y || x > n || y > n) return 0;
	int gx = guanxi(x,y);
	if (gx == 0)//没有关系
	{
		update(r,x,y);
		return 1;
	}
	switch (r)
	{
		case 1:
			if (gx == 1) return 1;
			else return 0;
		case 2:
			if (gx == 2) return 1;
			else return 0;
		default:
			return 0;
	}
}
int main()
{
	//freopen("\tmp\acm\test.txt","r",stdin);
	int cas, res = 0, r, x, y;
	scanf("%d%d", &n, &cas);
	for (int i = 1; i <= n; ++ i) py[i] = i, dr[i] = -1;//初始化
	while (cas --)
	{
		scanf("%d%d%d", &r, &x, &y);
		bool b = f(r,x,y);
		if (!b) ++ res;
		//printf("\t\t%d\n", b);
	}
	printf("%d\n", res);
	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