题意:中文题。
分析:裸的2-sat模板题。
代码:
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
const db eps=1e-5;
const int N=1e3+10;
const int M=2e6+10;
const ll MOD=1000000007;
const int mod=1000000007;
const int MAX=1000000010;
const double pi=acos(-1.0);
struct twosat {
int n,sn,S[2*N];
bool mark[2*N];
vector<int>g[2*N];
void init(int tot) {
n=tot*2;
for (int i=0;i<n;i+=2) {
g[i].clear();g[i^1].clear();
}
memset(mark,false,sizeof(mark));
}
void add_Edge(int u,int uval,int v,int vval) {
u=u*2+uval;v=v*2+vval;
g[u^1].push_back(v);g[v^1].push_back(u);
}
void delete_Edge(int u,int uval,int v,int vval) {
u=u*2+uval;v=v*2+vval;
g[u^1].pop_back();g[v^1].pop_back();
}
bool dfs(int u) {
if (mark[u]) return true ;
if (mark[u^1]) return false ;
mark[u]=true;S[sn++]=u;
for (int i=0;i<g[u].size();i++)
if (!dfs(g[u][i])) return false ;
return true ;
}
bool solve() {
int i,j;sn=0;
for (i=0;i<n;i+=2)
if (!mark[i]&&!mark[i^1]) {
if (!dfs(i)) {
for (j=0;j<sn;j++) mark[S[j]]=false;
sn=0;if (!dfs(i^1)) return false ;
}
}
return true ;
}
}TS;
int main()
{
int a,b,c,d,i,n,m;
while (~scanf("%d%d", &n, &m)) {
TS.init(n);
for (i=1;i<=m;i++) {
scanf("%d%d%d%d", &a, &b, &c, &d);
TS.add_Edge(a,c,b,d);
}
if (TS.solve()) printf("YES\n");
else printf("NO\n");
}
return 0;
}