• 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;
    }
    
  • 相关阅读:
    .net core 下编码问题
    spring一些简单小注意知识点
    使用ORM插入数据报错 Duplicate entry '0' for key 'PRIMARY'
    python:零散记录
    python:端口扫描邮件推送
    redis:哨兵集群配置
    redis:安装配置主从
    iptables:ipset批量管理ip
    Django:调用css、image、js
    Python:字体颜色
  • 原文地址:https://www.cnblogs.com/Iowa-Battleship/p/9634283.html
Copyright © 2020-2023  润新知