2-SAT

2-SAT

模版

白书P325

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
struct TwoSAT{
int n;
vector<int>G[maxn*2];
bool mark[maxn*2];
int S[maxn*2],c;
bool dfs(int x){
if(mark[x^1]) return false;
if(mark[x]) return true;
mark[x]=true;
S[c++]=x;
for(int i=0;i<G[x].size();i++){
if(!dfs(G[x][i])) return false;
}
return true;
}
void init(int n){
this->n=n;
for(int i=0;i<2*n;i++) G[i].clear();
memset(mark,0,sizeof(mark));
}
void add_clause(int x,int xval,int y,int yval){
x=x*2+xval;
y=y*2+yval;
G[x^1].push_back(y);
G[y^1].push_back(x);
}
bool solve(){
for(int i=0;i<2*n;i+=2){
if(!mark[i] && !mark[i+1]){
c=0;
if(!dfs(i)){
while(c>0) mark[S[--c]]=false;
if(!dfs(i+1)) return false;
}
}
}
return true;
}
}g;

la 3211

题目要求

白书P325

参考AC代码

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
77
#include<bits/stdc++.h>
#define maxn 2050
using namespace std;
int n;
struct TwoSAT{
int n;
vector<int>G[maxn*2];
bool mark[maxn*2];
int S[maxn*2],c;
bool dfs(int x){
if(mark[x^1]) return false;
if(mark[x]) return true;
mark[x]=true;
S[c++]=x;
for(int i=0;i<G[x].size();i++){
if(!dfs(G[x][i])) return false;
}
return true;
}
void init(int n){
this->n=n;
for(int i=0;i<2*n;i++) G[i].clear();
memset(mark,0,sizeof(mark));
}
void add_clause(int x,int xval,int y,int yval){
x=x*2+xval;
y=y*2+yval;
G[x^1].push_back(y);
G[y^1].push_back(x);
}
bool solve(){
for(int i=0;i<2*n;i+=2){
if(!mark[i] && !mark[i+1]){
c=0;
if(!dfs(i)){
while(c>0) mark[S[--c]]=false;
if(!dfs(i+1)) return false;
}
}
}
return true;
}
}g;
int T[maxn][2];
bool test(int diff){
g.init(n);
for(int i=0;i<n;i++){
for(int a=0;a<2;a++){
for(int j=i+1;j<n;j++){
for(int b=0;b<2;b++){
if(abs(T[i][a]-T[j][b])<diff) g.add_clause(i,a^1,j,b^1);
}
}
}
}
return g.solve();
}
int main(){
// freopen("input.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
int L=0,R=0;
for(int i=0;i<n;i++){
for(int a=0;a<2;a++){
scanf("%d",&T[i][a]);
R=max(R,T[i][a]);
}
}
int ans=0;
while(L<=R){
int mid=L+R>>1;
if(test(mid)) ans=mid,L=mid+1;
else R=mid-1;
}
printf("%d\n",ans);
}
return 0;
}

思路

白书P325
二分时间

la 3713

题目要求

白书P326

参考AC代码

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
#include<bits/stdc++.h>
#define maxn 100050
using namespace std;
int n,m;
struct TwoSAT{
int n;
vector<int>G[maxn*2];
bool mark[maxn*2];
int S[maxn*2],c;
bool dfs(int x){
if(mark[x^1]) return false;
if(mark[x]) return true;
mark[x]=true;
S[c++]=x;
for(int i=0;i<G[x].size();i++){
if(!dfs(G[x][i])) return false;
}
return true;
}
void init(int n){
this->n=n;
for(int i=0;i<2*n;i++) G[i].clear();
memset(mark,0,sizeof(mark));
}
void add_clause(int x,int xval,int y,int yval){
x=x*2+xval;
y=y*2+yval;
G[x^1].push_back(y);
G[y^1].push_back(x);
}
bool solve(){
for(int i=0;i<2*n;i+=2){
if(!mark[i] && !mark[i+1]){
c=0;
if(!dfs(i)){
while(c>0) mark[S[--c]]=false;
if(!dfs(i+1)) return false;
}
}
}
return true;
}
}g;
int age[maxn];
int sum;
int isyoung(int x){
return age[x]*n<sum;
}
int main(){
// freopen("input.txt","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF){
if(n==0 && m==0) break;
sum=0;
g.init(n);
for(int i=0;i<n;i++) scanf("%d",&age[i]),sum+=age[i];
for(int i=0;i<m;i++){
int x,y;
scanf("%d%d",&x,&y);
x--,y--;
if(x==y) continue;
g.add_clause(x,1,y,1); // 不能同去任务C
if(isyoung(x)==isyoung(y)) g.add_clause(x,0,y,0); // 同类宇航员不能同去任务A或者任务B
}
if(!g.solve()) puts("No solution");
else{
for(int i=0;i<n;i++){
if(g.mark[2*i]) puts("C"); // x[i]=false,去任务C
else if(isyoung(i)) puts("B"); // x[i]=true的年轻宇航员去任务B
else puts("A"); // x[i]=true的年长宇航员去任务A
}
}
}
return 0;
}

思路

白书P326

文章目录
  1. 1. 2-SAT
    1. 1.1. 模版
    2. 1.2. la 3211
      1. 1.2.1. 题目要求
      2. 1.2.2. 参考AC代码
      3. 1.2.3. 思路
    3. 1.3. la 3713
      1. 1.3.1. 题目要求
      2. 1.3.2. 参考AC代码
      3. 1.3.3. 思路
|