POJ 3678 Katu Puzzle

大意不再赘述。

思路:有几个表达式我们可以假设c的值为0或者1,然后去推相互之间有矛盾的点,连边时,我们将点拆为2个,一个表示0,一个表示1,Tarjan时就需要循环到2*n啦,有些表达式重复了,比如异或所有的重复了,所以不需要再增加边。

a AND b == 1 (a,b同时为1)     a->b, b->a, ~a-->a, ~b-->b;
a AND b == 0 (a,b不同时为1)   a-->~b, b-->~a;
a OR  b == 1  (a,b不同时为0)  ~a-->b, ~b-->a;
a OR  b == 0  (a,b同时为0)    ~a->~b, ~b->~a, a-->~a,  b-->~b;
a XOR b == 1 (a,b不相同为1)   a->~b, b->~a, ~a->b, ~b->a;
a XOR b == 0 (a,b相同为0)     a->b, b->a, ~a->~b, ~b->~a;

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdlib>
using namespace std;

const int MAXN = 2020;
const int MAXM = 1000100;

struct Edge
{
    int u, v, next;
}edge[MAXM];

int first[MAXN], stack[MAXN], ins[MAXN], low[MAXN], dfn[MAXN];
int belong[MAXM];
int ind[MAXN], outd[MAXN];

int cnt;
int n, m;
int scnt, top, tot;

void init()
{
    cnt = 0;
    scnt = top = tot = 0;
    memset(first, -1, sizeof(first));
    memset(ins, 0, sizeof(ins));
    memset(dfn, 0, sizeof(dfn));
}

void read_graph(int u, int v)
{
    edge[cnt].v = v;
    edge[cnt].next = first[u], first[u] = cnt++;
}

void dfs(int u)
{
    int v;
    low[u] = dfn[u] = ++tot;
    stack[top++] = u;
    ins[u] = 1;
    for(int e = first[u]; e != -1; e = edge[e].next)
    {
        v = edge[e].v;
        if(!dfn[v])
        {
            dfs(v);
            low[u] = min(low[u], low[v]);
        }
        else if(ins[v])
        {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u])
    {
        scnt++;
        do
        {
            v = stack[--top];
            belong[v] = scnt;
            ins[v] = 0;
        }while(u != v);
    }
}

void Tarjan()
{
    for(int v = 0; v < 2*n; v++) if(!dfn[v])
        dfs(v);
}

void read_case()
{
	init();
	while(m--)
	{
		int x, y, c;
		char str[5];
		scanf("%d%d%d%s", &x, &y, &c, str);
		if(str[0] == 'A') //拆点为2*x, 2*x+1,前者表示~a,后者表示a 
		{
			if(c)
			{
				read_graph(2*x, 2*x+1);
				read_graph(2*y, 2*y+1);
			}
			else
			{
				read_graph(2*x+1, 2*y);
				read_graph(2*y+1, 2*x);
			}
		}
		else if(str[0] == 'O')
		{
			if(c)
			{
				read_graph(2*x, 2*y+1);
				read_graph(2*y, 2*x+1);
			}
			else
			{
				read_graph(2*x+1, 2*x);
				read_graph(2*y+1, 2*y);
			}
		}
	}
}



void solve()
{
    read_case();
    Tarjan();
    for(int i = 0; i < 2*n; i += 2)
    {
        if(belong[i] == belong[i+1])
        {
            printf("NO\n");
            return ;
        }
    }
    printf("YES\n");    
}

int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        solve();
    }
    return 0;
}


你可能感兴趣的:(POJ 3678 Katu Puzzle)