51nod2860 奶牛议会

2860 奶牛议会

本题出自于bzoj2199[Usaco2011 Jan]奶牛议会,这里只放代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
 
const int maxn = 2E3 + 20;
 
int n,m,tail,q[maxn],bo[maxn],b2[maxn];
bool flag = 0;
 
vector <int> v[maxn];
 
bool dfs(int x)
{
	for (int i = 0; i < v[x].size(); i++) {
		int to = v[x][i];
		if (bo[to^1]) return 0;
		if (bo[to]) continue;
		bo[to] = 1; q[++tail] = to;
		if (!dfs(to)) return 0;
	}
	return 1;
}
 
bool Judge(int x)
{
	tail = 0;
	if (!dfs(x)) return 0; 
	for (int i = 0; i < n; i++)
		if (!bo[2*i] && !bo[2*i+1]) {
			bo[2*i] = 1; q[tail = 1] = 2*i;
			if (dfs(2*i)) continue;
			for (int j = 1; j <= tail; j++) bo[q[j]] = 0;
			bo[2*i+1] = 1; tail = 0;
			if (dfs(2*i+1)) continue;
			return 0;
		}
	return 1;
}
 
void CLEAR(int x) {memset(bo,0,sizeof(bo)); bo[x] = 1;}
 
int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	cin >> n >> m;	
	for (int i = 0; i < m; i++) {
		int a1,a2; char c1,c2;
		scanf("%d",&a1); c1 = getchar(); --a1;
		while (c1 != 'Y' && c1 != 'N') c1 = getchar();
		scanf("%d",&a2); c2 = getchar(); --a2;
		while (c2 != 'Y' && c2 != 'N') c2 = getchar();
		int x1 = c1 == 'Y'?2*a1:2*a1+1;
		int x2 = c2 == 'Y'?2*a2:2*a2+1;
		v[x1^1].push_back(x2);
		v[x2^1].push_back(x1);
	} 
	
	for (int i = 0; i < n; i++) {
		CLEAR(2*i); if (Judge(2*i)) b2[2*i] = 1,flag = 1;
		CLEAR(2*i+1); if (Judge(2*i+1)) b2[2*i+1] = 1,flag = 1;
	}
	if (!flag) printf("IMPOSSIBLE");
	else
		for (int i = 0; i < n; i++) {
			if (b2[2*i] && b2[2*i+1]) printf("?");
			else if (b2[2*i]) printf("Y");
			else printf("N");
		}
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值