hdu 3062 2-sat
Party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3521 Accepted Submission(s): 1141
Problem Description
有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席。在2n 个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同时出现在聚会上的。有没有可能会有n 个人同时列席?
Input
n: 表示有n对夫妻被邀请 (n<= 1000)
m: 表示有m 对矛盾关系 ( m < (n - 1) * (n -1))
在接下来的m行中,每行会有4个数字,分别是 A1,A2,C1,C2
A1,A2分别表示是夫妻的编号
C1,C2 表示是妻子还是丈夫 ,0表示妻子 ,1是丈夫
夫妻编号从 0 到 n -1
m: 表示有m 对矛盾关系 ( m < (n - 1) * (n -1))
在接下来的m行中,每行会有4个数字,分别是 A1,A2,C1,C2
A1,A2分别表示是夫妻的编号
C1,C2 表示是妻子还是丈夫 ,0表示妻子 ,1是丈夫
夫妻编号从 0 到 n -1
Output
如果存在一种情况 则输出YES
否则输出 NO
否则输出 NO
Sample Input
2
1
0 1 1 1
Sample Output
YES
Source
Recommend
lcy
2-sat 水题
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
using namespace std;
#define LL long long
#define UINT unsigned int
#define MAX_INT 0x7fffffff
#define cint const int
#define MAXN 2222
vector<int> g[MAXN];
bool mark[MAXN];
int n, m, s[MAXN];
void add(int x, int a, int y, int b){ //a=0表妻子,=1表丈夫,b同
x = (x<<1) + a;
y = (y<<1) + b;
g[x].push_back(y^1);
g[y].push_back(x^1);
}
int c;
bool dfs(int u){
if(mark[u]) return true;
if(mark[u^1]) return false;
mark[u] = true;
s[c++] = u;
for(int i=0; i<g[u].size(); i++) if(!dfs(g[u][i]))
return false;
return true;
}
int main(){
// freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
while(scanf(" %d %d", &n, &m)==2){
int i, u, v, w, h;
n = n << 1;
for(i=0; i<n; i++) g[i].clear();
while(m--){
scanf(" %d %d %d %d", &u, &v, &w, &h);
add(u, w, v, h);
}
fill_n(mark, n, false);
for(i=0; i<n; i+=2) if(!mark[i]&&!mark[i+1]){
c=0;
if(!dfs(i)){
while(c--) mark[s[c]]=false;
if(!dfs(i+1)) break;
}
}
if(i<n) printf("NO\n");
else printf("YES\n");
}
return 0;
}

浙公网安备 33010602011771号