2-SAT 裸题,搞之
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std; const int maxn=+;
int N,M;
char s1[],s2[]; stack<int>S;
vector<int>G[maxn];
vector<int>FG[maxn];
int Belong[maxn];
int flag[maxn];
int Block; void init()
{
for(int i=; i<maxn; i++) G[i].clear();
for(int i=; i<maxn; i++) FG[i].clear();
memset(Belong,,sizeof Belong);
memset(flag,,sizeof flag);
while(!S.empty()) S.pop();
Block=;
} void addEgde(int x,int y)
{
G[x].push_back(y);
FG[y].push_back(x);
} void dfs1(int now)
{
flag[now]=;
for(int i=; i<G[now].size(); i++)
if(!flag[G[now][i]])
dfs1(G[now][i]);
S.push(now);
} void dfs2(int now)
{
Belong[now]=Block;
for(int i=; i<FG[now].size(); i++)
if(!Belong[FG[now][i]])
dfs2(FG[now][i]);
} bool judge()
{
for(int i=; i<*N; i++) if(!flag[i]) dfs1(i);
while(!S.empty())
{
int Top=S.top();
S.pop();
if(!Belong[Top])
{
Block++;
dfs2(Top);
}
}
for(int i=; i<N; i++)
if(Belong[*i]==Belong[*i+])
return ;
return ;
} int main()
{
while(~scanf("%d%d",&N,&M))
{
init();
for(int i=;i<=M;i++)
{
scanf("%s%s",s1,s2);
int num1=,num2=;
for(int i=;i<strlen(s1);i++) num1=num1*+s1[i]-'';
for(int i=;i<strlen(s2);i++) num2=num2*+s2[i]-''; num1--;num2--; if(s1[]=='+'&&s2[]=='+')
{
addEgde(*num1,*num2+);
addEgde(*num2,*num1+);
}
if(s1[]=='-'&&s2[]=='-')
{
addEgde(*num1+,*num2);
addEgde(*num2+,*num1);
}
if(s1[]=='+'&&s2[]=='-')
{
addEgde(*num1,*num2);
addEgde(*num2+,*num1+);
}
if(s1[]=='-'&&s2[]=='+')
{
addEgde(*num1+,*num2+);
addEgde(*num2,*num1);
}
}
if(judge()) printf("1\n");
else printf("0\n");
}
return ;
}