0%

HDU3062 Party

题意

有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席。在2n个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同时出现在聚会上的。有没有可能会有n个人同时列席?

思路

每对夫妻只能取两种值,可以转化成显然的2-SAT问题
对于$i \vee j$,可以连边$\neg i \rightarrow j$和$\neg j \rightarrow i$
然后dfs求解即可

代码

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
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
int mark[2010];
vector<int> G[2010];
stack<int> S;
void addedge(int ui,int vi){
G[ui].push_back(vi);
}
bool dfs(int u){
if(mark[u^1])
return false;
if(mark[u])
return true;
mark[u]=1;
S.push(u);
for(int i=0;i<G[u].size();i++){
bool req=dfs(G[u][i]);
if(!req){
return false;
}
}
return true;
}
int n,m;
void solve(){
for(int i=1;i<=m;i++){
int A1,A2,C1,C2;
scanf("%d %d %d %d",&A1,&A2,&C1,&C2);
addedge((2*A1+C1),((2*A2+C2)^1));
addedge((2*A2+C2),((2*A1+C1)^1));
}
for(int i=0;i<n;i++){
if(!mark[2*i]&&!mark[2*i+1]){
while(S.size())
S.pop();
bool req=dfs(2*i);
if(!req){
while(S.size()){
mark[S.top()]=0;
S.pop();
}
req=dfs(2*i+1);
if(!req){
printf("NO\n");
return;
}
}
}
}
printf("YES\n");
}
int main(){
while(scanf("%d %d",&n,&m)==2){
solve();
for(int i=0;i<2*n;i++){
G[i].clear();
mark[i]=0;
}
}
return 0;
}