奶牛议会
题目背景:
分析:显然的2-SAT,直接针对每一头牛的要求,如果第一个要求不满足,第二个要求就一定满足,如果第二个要求不满足第一个一定满足(!x -> y, !y-> x),本来我在担心直接上tarjan怎么输出方案,然后,就发现一共才2000个点,为什么不直接暴力判断呢,直接把每一个点分别定义为true,false,分别dfs判断是否可行如果两者可行为?,否则为Y或者T。
Source:
/*
created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
inline char read() {
static const int IN_LEN = 1024 * 1024;
static char buf[IN_LEN], *s, *t;
if (s == t) {
t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
if (s == t) return -1;
}
return *s++;
}
///*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return ;
if (c == '-') iosig = true;
}
for (x = 0; isdigit(c); c = read())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
*oh++ = c;
}
template<class T>
inline void W(T x) {
static int buf[30], cnt;
if (x == 0) write_char('0');
else {
if (x < 0) write_char('-'), x = -x;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
while (cnt) write_char(buf[cnt--]);
}
}
inline void flush() {
fwrite(obuf, 1, oh - obuf, stdout);
}
/*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
if (c == '-') iosig = true;
for (x = 0; isdigit(c); c = getchar())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int MAXN = 1000 + 10;
std::vector<int> edge[MAXN << 1];
int n, m, x, y;
char c1, c2;
char ans[MAXN];
bool vis[MAXN << 1];
inline void dfs(int cur) {
vis[cur] = true;
for (int p = 0; p < edge[cur].size(); ++p)
if (!vis[edge[cur][p]]) dfs(edge[cur][p]);
}
inline bool check(int i) {
memset(vis, 0, sizeof(vis)), dfs(i);
for (int i = 1; i <= n; ++i)
if (vis[i << 1] && vis[i << 1 | 1])
return false;
return true;
}
int main() {
R(n), R(m);
for (int i = 1; i <= m; ++i) {
R(x), c1 = read(), R(y), c2 = read();
if (c1 == 'Y') x <<= 1;
else x <<= 1, x |= 1;
if (c2 == 'Y') y <<= 1;
else y <<= 1, y |= 1;
edge[x ^ 1].push_back(y), edge[y ^ 1].push_back(x);
}
for (int i = 1; i <= n; ++i) {
bool p = check(i << 1), q = check(i << 1 | 1);
if (!p && !q) std::cout << "IMPOSSIBLE", exit(0);
if (p && q) ans[i] = '?';
else if (!p) ans[i] = 'N';
else ans[i] = 'Y';
}
for (int i = 1; i <= n; ++i) write_char(ans[i]);
flush();
return 0;
}