题目链接:http://poj.org/problem?id=3678

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std; const int maxn = ; struct Two_Sat
{
int n;
vector<int> G[maxn*];
bool mark[maxn*];
int s[maxn*],cnt; void init(int n)
{
this->n = n;
memset(mark,,sizeof(mark));
for(int i=; i<n*; i++) G[i].clear();
} void add_clause(int u,int v,int flag,char symbol)
{
u = u* + flag;
v = v* + flag;
if(symbol == 'A')
{
if(flag)
{
G[u].push_back(v);
G[v].push_back(u);
G[u^].push_back(u);
G[v^].push_back(v);
}
else
{
G[u^].push_back(v);
G[v^].push_back(u);
}
}
else if(symbol == 'O')
{
if(flag)
{
G[u^].push_back(v);
G[v^].push_back(u);
}
else
{
G[u].push_back(v);
G[v].push_back(u);
G[u^].push_back(u);
G[v^].push_back(v);
}
}
else
{
if(flag)
{
G[u^].push_back(v);
G[v].push_back(u^);
G[v^].push_back(u);
G[u].push_back(v^);
}
else
{
G[u].push_back(v);
G[v].push_back(u);
G[u^].push_back(v^);
G[v^].push_back(u^);
}
}
} bool dfs(int u)
{
if(mark[u^]) return false;
if(mark[u]) return true;
mark[u] = true;
s[cnt++] = u;
for(int i=; i<G[u].size(); i++)
{
if(!dfs(G[u][i])) return false;
}
return true;
} bool solve()
{
for(int i=; i<n*; i+=)
{
if(!mark[i] && !mark[i+])
{
cnt = ;
if(!dfs(i))
{
while(cnt>) mark[s[--cnt]] = false;
if(!dfs(i+)) return false;
}
}
}
return true;
}
}solver; int main()
{
//freopen("E:\\acm\\input.txt","r",stdin);
int N,M;
while(cin>>N>>M)
{
solver.init(N);
for(int i=; i<=M; i++)
{
int a,b,c;
char s[];
scanf("%d %d %d %s",&a,&b,&c,s);
solver.add_clause(a,b,c,s[]);
}
if(solver.solve()) printf("YES\n");
else printf("NO\n");
}
}
05-04 03:08