Katu Puzzle POJ - 3678(水2 - sat)

题意:

  有n个未知量,m对未知量之间的关系,判断是否能求出所有的未知量且满足这些关系

解析:

  关系建边就好了

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e6 + 10, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
int n, m;
vector<int> G[maxn << 1];
int sccno[maxn], vis[maxn], low[maxn], scc_cnt, scc_clock;
stack<int> S;
void init()
{
    mem(vis, 0);
    mem(low, 0);
    mem(sccno, 0);
    scc_cnt = scc_clock = 0;
    for(int i = 0; i < maxn; i++) G[i].clear();
}

void dfs(int u)
{
    low[u] = vis[u] = ++scc_clock;
    S.push(u);
    for(int i = 0; i < G[u].size(); i++)
    {
        int v = G[u][i];
        if(!vis[v])
        {
            dfs(v);
            low[u] = min(low[u], low[v]);
        }
        else if(!sccno[v])
            low[u] = min(low[u], vis[v]);
    }
    if(low[u] == vis[u])
    {
        scc_cnt++;
        for(;;)
        {
            int x = S.top(); S.pop();
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}

bool check()
{
    for(int i = 0; i < n * 2; i+=2)
        if(sccno[i] == sccno[i + 1])
            return false;
    return true;
}

int main()
{
    init();
    int a, b, c;
    char op[5];
    rd(n), rd(m);
    for(int i = 0; i < m; i++)
    {
        rd(a), rd(b), rd(c), rs(op);
        if(op[0] == 'A')
        {
            if(c)
            {
                G[a << 1 | 1].push_back(b << 1 | 1);
                G[b << 1 | 1].push_back(a << 1 | 1);
                G[a << 1].push_back(a << 1 | 1);
                G[b << 1].push_back(b << 1 | 1);
            }
            else
            {
                G[a << 1 | 1].push_back(b << 1);
                G[b << 1 | 1].push_back(a << 1);
            }
        }
        else if(op[0] == 'O')
        {
            if(c)
            {
                G[a << 1].push_back(b << 1 | 1);
                G[b << 1].push_back(a << 1 | 1);
            }
            else
            {
                G[a << 1].push_back(b << 1);
                G[b << 1].push_back(a << 1);
                G[a << 1 | 1].push_back(a << 1);
                G[b << 1 | 1].push_back(b << 1);
            }
        }
        else if(op[0] == 'X')
        {
            if(c)
            {
                G[a << 1 | 1].push_back(b << 1);
                G[b << 1].push_back(a << 1 | 1);
                G[a << 1].push_back(b << 1 | 1);
                G[b << 1 | 1].push_back(a << 1);
            }
            else
            {
                G[a << 1].push_back(b << 1);
                G[b << 1].push_back(a << 1);
                G[a << 1 | 1].push_back(b << 1 | 1);
                G[b << 1 | 1].push_back(a << 1 | 1);
            }
        }
    }
    for(int i = 0; i < n * 2; i++)
        if(!vis[i]) dfs(i);
    if(check()) puts("YES");
    else puts("NO");

    return 0;
}

 

posted @ 2018-10-18 20:16  WTSRUVF  阅读(123)  评论(0编辑  收藏  举报