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;
}