hdu 3062 Party

本文从WordPress迁移而来, 查看全部WordPress迁移文章

2-sat模板题

判断是否存在可行解,建图后求强连通分量逐一判断所有对立点是否都在不同强连通分量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define N 2010
#define M 8000010 //2 * N * N
#define cl memset
#define pb push_back

int n,tot,head[N];
struct edge{
int v,next;
}e[M];
int dfn[N],low[N],belong[N],stack[N],dcnt,bcnt,top;
bool ins[N];

inline void add(int u ,int v){
e[tot].v = v; e[tot].next = head[u]; head[u] = tot++;
}

void tarjan(int u){
dfn[u] = low[u] = ++dcnt;
stack[++top] = u; ins[u] = true;
for(int k = head[u]; k != -1; k = e[k].next){
int v = e[k].v;
if(!dfn[v]){
tarjan(v);
low[u] = min(low[u],low[v]);
}
else if(ins[v])
low[u] = min(low[u],dfn[v]);
}
if(dfn[u] == low[u]){
++bcnt;
while(true){
int x = stack[top--];
ins[x] = false;
belong[x] = bcnt;
if(x == u) break;
}
}
}

void scc(){
dcnt = bcnt = top = 0;
cl(dfn,0,sizeof(dfn));
cl(ins,false,sizeof(ins));
for(int i = 0; i < 2*n; i++)
if(!dfn[i])
tarjan(i);
}

int main(){
while(scanf("%d",&n)!=EOF){
int m;
scanf("%d",&m);
tot = 0;
cl(head,-1,sizeof(head));
while(m--){
int x,y,fx,fy;
scanf("%d%d%d%d",&x,&y,&fx,&fy);
add((x<<1) + fx , (y<<1|1) - fy);
add((y<<1) + fy , (x<<1|1) - fx);
}
scc();
bool res = true;
for(int i = 0; i < n ; i++)
if(belong[i<<1] == belong[i<<1|1]){
res = false; break;
}
if(res) puts("YES");
else puts("NO");
}
return 0;
}