1. 程式人生 > >Poj3678:Katu Puzzle

Poj3678:Katu Puzzle

lin href ++ [] www. mark mat stream mes

大概題意

\(n\)個數,可以為\(0/1\),給\(m\)個條件,表示某兩個數經過\(or, and, xor\)後的數是多少
判斷是否有解

Sol

\(2-SAT\)判定
建圖

# include <iostream>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <math.h>
# include <algorithm>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std; typedef long long ll; const int _(2005); const int __(4e6 + 5); IL int Input(){ RG int x = 0, z = 1; RG char c = getchar(); for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1; for(; c >= '0' && c <= '9'
; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); return x * z; } int n, m, first[_], cnt, num; int S[_], vis[_], dfn[_], low[_], Index, col[_]; struct Edge{ int to, next; } edge[__]; IL void Add(RG int u, RG int v){ edge[cnt] = (Edge){v, first[u]}; first[u] = cnt++; } IL void
Tarjan(RG int u){ vis[u] = 1, dfn[u] = low[u] = ++Index, S[++S[0]] = u; for(RG int e = first[u]; e != -1; e = edge[e].next){ RG int v = edge[e].to; if(!dfn[v]) Tarjan(v), low[u] = min(low[u], low[v]); else if(vis[v]) low[u] = min(low[u], dfn[v]); } if(dfn[u] != low[u]) return; RG int v = S[S[0]--]; col[v] = ++num, vis[v] = 0; while(v != u) v = S[S[0]--], col[v] = num, vis[v] = 0; } int main(RG int argc, RG char* argv[]){ Fill(first, -1), n = Input(), m = Input(); for(RG int i = 1; i <= m; ++i){ RG int u = Input() + 1, v = Input() + 1, w = Input(); RG char op; scanf(" %c", &op); if(op == 'A'){ if(w) Add(u, v), Add(v, u), Add(u + n, u), Add(v + n, v); else Add(u, v + n), Add(v, u + n); } else if(op == 'O'){ if(w) Add(u + n, v), Add(v + n, u); else Add(u, u + n), Add(v, v + n), Add(u + n, v + n), Add(v + n, u + n); } else{ if(w) Add(u, v + n), Add(v, u + n), Add(u + n, v), Add(v + n, u); else Add(u, v), Add(v, u), Add(u + n, v + n), Add(v + n, u + n); } } for(RG int i = 1, tmp = n << 1; i <= tmp; ++i) if(!dfn[i]) Tarjan(i); for(RG int i = 1; i <= n; ++i) if(col[i] == col[i + n]) return puts("NO"), 0; return puts("YES"), 0; }

Poj3678:Katu Puzzle