HDU - 4751 Divide Groups 二分图的判定 染色问题 反向建边

Training 7 - B题

This year is the 60th anniversary of NJUST, and to make the celebration more colorful, Tom200 is going to invite distinguished alumnus back to visit and take photos.

After carefully planning, Tom200 announced his activity plan, one that contains two characters:

  1. Whether the effect of the event are good or bad has nothing to do with the number of people join in.

  2. The more people joining in one activity know each other, the more interesting the activity will be. Therefore, the best state is that, everyone knows each other.

The event appeals to a great number of alumnus, and Tom200 finds that they may not know each other or may just unilaterally recognize others. To improve the activities effects, Tom200 has to divide all those who signed up into groups to take part in the activity at different time. As we know, one’s energy is limited, and Tom200 can hold activity twice. Tom200 already knows the relationship of each two person, but he cannot divide them because the number is too large.

Now Tom200 turns to you for help. Given the information, can you tell if it is possible to complete the dividing mission to make the two activity in best state.

Input

The input contains several test cases, terminated by EOF.

Each case starts with a positive integer n (2<=n<=100), which means the number of people joining in the event.

N lines follow. The i-th line contains some integers which are the id

of students that the i-th student knows, terminated by 0. And the id starts from 1.

Output

If divided successfully, please output “YES” in a line, else output “NO”.

Sample Input

3
3 0
1 0
1 2 0

Sample Output

YES

#pragma warning (disable:4996)
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 100 + 5;
const int M = 1e4 + 5;
const int maxn = 1e6 + 5;

int head[N], ver[M], nex[M];
int tot;
int v[N];
bool k[N][N];
int n;

void add(int x, int y)
{
	++tot;
	ver[tot] = y;
	nex[tot] = head[x];
	head[x] = tot;
}

bool color(int x, int col)
{
	v[x] = col;
	for (int i = head[x]; i; i = nex[i])
	{
		int y = ver[i];
		if (!v[y])
		{
			if (!color(y, 3 - col))
				return 0;
		}
		else if (v[y] == v[x])
			return 0;
	}
	return 1;
}

int main()
{
	while (scanf("%d", &n) != EOF)
	{
		memset(head, 0, sizeof(head));
		memset(ver, 0, sizeof(ver));
		memset(nex, 0, sizeof(nex));
		memset(k, 0, sizeof(k));
		memset(v, 0, sizeof(v));
		tot = 0;
		for (int i = 1; i <= n; i++)
		{
			int j;
			while (scanf("%d", &j) != EOF && j)
				k[i][j] = 1;
		}
		for (int i = 1; i <= n; i++)
			for (int j = i + 1; j <= n; j++)
				if (!k[i][j] || !k[j][i])
				{
					//给不认识的建边
					add(i, j);
					add(j, i);
				}
				bool flag = 1;
		for (int i = 1; i <= n; i++)
			if (!v[i])
				if (!color(i, 1))
				{
					flag = 0;
					break;
				}
		if (flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}

思路:
不认识的人不能在同一个聚会,给不认识的人建边后染色,如果染色不成功说明不能将不认识的人分为两部分。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值