一名苦逼的OIer,想成为ACMer

Iowa_Battleship

POJ3678 Katu Puzzle

原题链接

\(2-SAT\)模板题。
\(AND,OR,XOR\)转换成\(2-SAT\)的命题形式连边,用\(tarjan\)求强连通分量并检验即可。

#include<cstdio>
using namespace std;
const int N = 2010;
const int M = 4e6 + 10;
int fi[N], di[M], ne[M], dfn[N], low[N], st[N], bl[N], l, tp, ti, SCC;
bool v[N];
inline int re()
{
	int x = 0;
	char c = getchar();
	bool p = 0;
	for (; c < '0' || c > '9'; c = getchar())
		p |= c == '-';
	for (; c >= '0' && c <= '9'; c = getchar())
		x = x * 10 + c - '0';
	return p ? -x : x;
}
inline int re_l()
{
	char c = getchar();
	for (; c < 'A' || c > 'Z'; c = getchar());
	return !(c ^ 'A') ? 1 : (c ^ 'O' ? 2 : 0);
}
inline void add(int x, int y)
{
	di[++l] = y;
	ne[l] = fi[x];
	fi[x] = l;
}
inline int minn(int x, int y)
{
	return x < y ? x : y;
}
void tarjan(int x)
{
	int i, y;
	dfn[x] = low[x] = ++ti;
	st[++tp] = x;
	v[x] = 1;
	for (i = fi[x]; i; i = ne[i])
	{
		y = di[i];
		if (!dfn[y])
		{
			tarjan(y);
			low[x] = minn(low[x], low[y]);
		}
		else
			if (v[y])
				low[x] = minn(low[x], dfn[y]);
	}
	if (!(low[x] ^ dfn[x]))
	{
		++SCC;
		do
		{
			y = st[tp--];
			v[y] = 0;
			bl[y] = SCC;
		} while (x ^ y);
	}
}
int main()
{
	int i, n, m, x, y, z, p;
	n = re();
	m = re();
	for (i = 1; i <= m; i++)
	{
		x = re() + 1;
		y = re() + 1;
		z = re();
		p = re_l();
		if (!p)
		{
			if (z)
			{
				add(x, y + n);
				add(y, x + n);
			}
			else
			{
				add(x + n, x);
				add(y + n, y);
			}
		}
		else
			if (!(p ^ 1))
			{
				if (z)
				{
					add(x, x + n);
					add(y, y + n);
				}
				else
				{
					add(x + n, y);
					add(y + n, x);
				}
			}
			else
			{
				if (z)
				{
					add(x, y + n);
					add(y, x + n);
					add(x + n, y);
					add(y + n, x);
				}
				else
				{
					add(x, y);
					add(y, x);
					add(x + n, y + n);
					add(y + n, x + n);
				}
			}
	}
	for (i = 1; i <= (n << 1); i++)
		if (!dfn[i])
			tarjan(i);
	for (i = 1; i <= n; i++)
		if (!(bl[i] ^ bl[i + n]))
		{
			printf("NO");
			return 0;
		}
	printf("YES");
	return 0;
}

posted on 2018-09-12 13:28  Iowa_Battleship  阅读(130)  评论(0编辑  收藏  举报

导航