HDU3715(二分+2-SAT)

Posted Penn000

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU3715(二分+2-SAT)相关的知识,希望对你有一定的参考价值。

Go Deeper

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3184    Accepted Submission(s): 1035


Problem Description

Here is a procedure‘s pseudocode:

go(int dep, int n, int m)
begin
output the value of dep.
if dep < m and x[a[dep]] + x[b[dep]] != c[dep] then go(dep + 1, n, m)
end

In this code n is an integer. a, b, c and x are 4 arrays of integers. The index of array always starts from 0. Array a and b consist of non-negative integers smaller than n. Array x consists of only 0 and 1. Array c consists of only 0, 1 and 2. The lengths of array a, b and c are m while the length of array x is n. Given the elements of array a, b, and c, when we call the procedure go(0, n, m) what is the maximal possible value the procedure may output?
 

 

Input

There are multiple test cases. The first line of input is an integer T (0 < T ≤ 100), indicating the number of test cases. Then T test cases follow. Each case starts with a line of 2 integers n and m (0 < n ≤ 200, 0 < m ≤ 10000). Then m lines of 3 integers follow. The i-th(1 ≤ i ≤ m) line of them are ai-1 ,bi-1 and ci-1 (0 ≤ ai-1, bi-1 < n, 0 ≤ ci-1 ≤ 2).
 

 

Output

For each test case, output the result in a single line.
 

 

Sample Input

3 2 1 0 1 0 2 1 0 0 0 2 2 0 1 0 1 1 2
 

 

Sample Output

1 1 2
 

 

Author

CAO, Peng
 

 

Source

 
令 i 表示第i位为0,NOT i 表示第i位为1.
c == 0, 则 A and B == 1
c == 1, 则 A xor B == 0
c == 2, 则 A xor B != 0
建图,二分验证。
  1 //2017-08-27
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <iostream>
  5 #include <algorithm>
  6 #include <vector>
  7 #include <iomanip>
  8 #include <cmath>
  9 
 10 using namespace std;
 11 
 12 const int N = 5010;
 13 const int M = N*N;
 14 const double EPS = 1e-6;
 15 int head[N], rhead[N], tot, rtot;
 16 struct Edge{
 17     int to, next;
 18 }edge[M], redge[M];
 19 
 20 void init(){
 21     tot = 0;
 22     rtot = 0;
 23     memset(head, -1, sizeof(head));
 24     memset(rhead, -1, sizeof(rhead));
 25 }
 26 
 27 void add_edge(int u, int v){
 28     edge[tot].to = v;
 29     edge[tot].next = head[u];
 30     head[u] = tot++;
 31 
 32     redge[rtot].to = u;
 33     redge[rtot].next = rhead[v];
 34     rhead[v] = rtot++;
 35 }
 36 
 37 vector<int> vs;//后序遍历顺序的顶点列表
 38 bool vis[N];
 39 int cmp[N];//所属强连通分量的拓扑序
 40 
 41 //input: u 顶点
 42 //output: vs 后序遍历顺序的顶点列表
 43 void dfs(int u){
 44     vis[u] = true;
 45     for(int i = head[u]; i != -1; i = edge[i].next){
 46         int v = edge[i].to;
 47         if(!vis[v])
 48           dfs(v);
 49     }
 50     vs.push_back(u);
 51 }
 52 
 53 //input: u 顶点编号; k 拓扑序号
 54 //output: cmp[] 强连通分量拓扑序
 55 void rdfs(int u, int k){
 56     vis[u] = true;
 57     cmp[u] = k;
 58     for(int i = rhead[u]; i != -1; i = redge[i].next){
 59         int v = redge[i].to;
 60         if(!vis[v])
 61           rdfs(v, k);
 62     }
 63 }
 64 
 65 //Strongly Connected Component 强连通分量
 66 //input: n 顶点个数
 67 //output: k 强连通分量数;
 68 int scc(int n){
 69     memset(vis, 0, sizeof(vis));
 70     vs.clear();
 71     for(int u = 0; u < n; u++)
 72       if(!vis[u])
 73         dfs(u);
 74     int k = 0;
 75     memset(vis, 0, sizeof(vis));
 76     for(int i = vs.size()-1; i >= 0; i--)
 77       if(!vis[vs[i]])
 78         rdfs(vs[i], k++);
 79     return k;
 80 }
 81 
 82 int n, m;
 83 int a[10010], b[10010], c[10010];
 84 
 85 bool check(int len){
 86     init();
 87     for(int i = 0; i < len; i++){
 88         if(c[i] == 0){
 89             add_edge(a[i]+n, b[i]);
 90             add_edge(b[i]+n, a[i]);
 91         }else if(c[i] == 1){
 92             add_edge(a[i], b[i]);
 93             add_edge(a[i]+n, b[i]+n);
 94             add_edge(b[i], a[i]);
 95             add_edge(b[i]+n, a[i]+n);
 96         }else if(c[i] == 2){
 97             add_edge(a[i], b[i]+n);
 98             add_edge(b[i], a[i]+n);
 99         }
100     }
101     scc(n<<1);
102     for(int i = 0; i < n; i++)
103       if(cmp[i] == cmp[i+n])
104         return false;
105     return true;
106 }
107 
108 int main()
109 {
110     std::ios::sync_with_stdio(false);
111     //freopen("inputD.txt", "r", stdin);
112     int T;
113     cin>>T;
114     while(T--){
115         cin>>n>>m;
116         for(int i = 0; i < m; i++)
117           cin>>a[i]>>b[i]>>c[i];
118         int l = 0, r = m, mid, ans;
119         while(l <= r){
120             mid = (l+r)/2;
121             if(check(mid)){
122                 ans = mid;
123                 l = mid+1;
124             }else
125                   r = mid-1;
126         }
127         cout<<ans<<endl;
128     }
129 
130     return 0;
131 }

 

以上是关于HDU3715(二分+2-SAT)的主要内容,如果未能解决你的问题,请参考以下文章

二分+2-sat——hdu3062

HDU1815 2-sat+二分

HDU3622(二分+2-SAT)

hdu 3622 二分+2-sat

HDU 3622 Bomb Game(二分+2-SAT)

HDU 3622 Bomb Game