您的位置:首页 > 其它

BZOJ2199 [Usaco2011 Jan]奶牛议会

2015-02-01 23:58 387 查看
首先建立一个2-SAT的裸模型,然后发现。。。tarjan没法判断'?'的情况

于是暴力对每一个议案check一下,直接dfs即可

/**************************************************************
Problem: 2199
User: rausen
Language: C++
Result: Accepted
Time:160 ms
Memory:884 kb
****************************************************************/

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const char ch[3] = {'?', 'N', 'Y'};
const int N = 2005;
const int M = N << 2;

struct edge {
int next, to;
edge() {}
edge(int _n, int _t) : next(_n), to(_t) {}
} e[M];

int n, m;
int first
, tot;
bool vis
;
int ans
;

int read() {
int x = 0;
char ch = getchar();
while (ch < '0' || '9' < ch)
ch = getchar();
while ('0' <= ch && ch <= '9')
(x *= 10) += ch - '0', ch = getchar();
return x;
}

int get() {
int x = read();
char ch = getchar();
while (ch != 'Y' && ch != 'N')
ch = getchar();
if (ch == 'Y') --(x <<= 1);
else x <<= 1;
return x;
}

void add_edge(int x, int y) {
e[++tot] = edge(first[x], y);
first[x] = tot;
}

#define y e[x].to
void dfs(int p) {
int x;
vis[p] = 1;
for (x = first[p]; x; x = e[x].next)
if (!vis[y]) dfs(y);
}
#undef y

bool check(int p) {
int i;
memset(vis, 0, sizeof(vis));
dfs(p);
for (i = 1; i <= n; ++i)
if (vis[2 * i] && vis[2 * i - 1]) return 0;
return 1;
}

int main() {
int i, a, b, c, d, p, q;
n = read(), m = read();
for (i = 1; i <= m; ++i) {
a = get(), c = get();
if (a & 1) b = a + 1;
else b = a - 1;
if (c & 1) d = c + 1;
else d = c - 1;
add_edge(b, c);
add_edge(d, a);
}
for (i = 1; i <= n; ++i) {
p = check(2 * i - 1);
q = check(2 * i);
if (!p && !q) {
puts("IMPOSSIBLE");
return 0;
}
else if (p && q) ans[i] = 0;
else if (!p) ans[i]= 1;
else ans[i] = 2;
}
for (i = 1; i <= n; ++i)
putchar(ch[ans[i]]);
puts("");
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: