hdu 3715 2-sat + 二分

来源:互联网 发布:粒子群算法电子书 编辑:程序博客网 时间:2024/05/22 15:44

题目:

Go Deeper

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


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
32 10 1 02 10 0 02 20 1 01 1 2
 

Sample Output
112
 

Author
CAO, Peng



分析见代码中的注释


代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#include <string>#include <vector>#include <stack>#include <bitset>#include <set>#include <list>#include <deque>#include <time.h>#include <map>#include <queue>using namespace std;const int maxn=20020;const int maxv=520;int t,m;int n,a[maxn],b[maxn],c[maxn];///x[0]-x[n-1]  x[n]-x[2*n-1]  x数组就是图中的点 int v,belong[maxv];///顶点数 所属强连通分量拓扑序vector<int> g[maxv]; vector<int> rg[maxv];///反向图 int vs[maxv],num;///后序遍历顶点标号 下标num属于0 - v-1bool used[maxv];inline void adde(int a,int b){///a->b    g[a].push_back(b); rg[b].push_back(a);}inline void dfs(int v){    used[v]=1;    for(int i=0;i<g[v].size();++i) if(!used[g[v][i]]) dfs(g[v][i]);    vs[num++]=v;}inline void rdfs(int v,int k){    used[v]=1;    belong[v]=k;    for(int i=0;i<rg[v].size();++i)  if(!used[rg[v][i]]) rdfs(rg[v][i],k);}inline int scc(){///返回缩点之后点数    memset(used,0,sizeof(used));    num=0;    for(int i=0;i<v;++i) if(!used[i]) dfs(i);///得到后序标号    memset(used,0,sizeof(used));    int k=0;    for(int i=v/*num*/-1;i>=0;--i) if(!used[vs[i]]) rdfs(vs[i],k++);    return k;}/*给出长为m的三个数组 a,b,c. a,b里面是[0,n-1]的数字,c里面是0,1或者2 另有一个x数组,里面是0或者1,让你找出一种x数组的构造方案使得dep值最大 dep取值于[0,m]  若 x[a[dep]] + x[b[dep]] != c[dep] 则输出dep 求最大输出值由于x数组只能取0或者1,问题抽象为布尔数组 x 是否有一组真值指派满足给定限制 其中限制有[0,m]个 这里可以用二分实现 主要就是建图了: C=2 a+b!=c,则a b不同时为1       a&b=0  C=1 a+b!=c,则a b不能是11或00   a^b=0C=0 a+b!=0,则a b至少一个为1     a|b=1*/ inline bool solve(int dep){///dep表示限制条件个数for(int i=0;i<=2*n;++i) g[i].clear(),rg[i].clear();///注意是2*n for(int i=0;i<dep;++i){if(c[i]==2){///a&&b=0  adde(a[i],b[i]+n); adde(b[i],a[i]+n);      ///注意+n放在[]外!!!! }else if(c[i]==1){///a^b=0adde(a[i],b[i]); adde(b[i],a[i]);adde(a[i]+n,b[i]+n); adde(b[i]+n,a[i]+n);}else if(c[i]==0){///a|b=1 adde(a[i]+n,b[i]); adde(b[i]+n,a[i]);}}scc();    bool f=1;    for(int i=0;i<n;++i){        if(belong[i]==belong[n+i]){            f=0; break;        }    }    return f;}int main(){///546MS2672Kint l,r,mid,tag;    scanf("%d",&t);    while(t--){    scanf("%d%d",&n,&m);    v=n*2;        for(int i=0;i<m;++i){            scanf("%d%d%d",&a[i],&b[i],&c[i]);        }        l=0,r=m,mid;///想输出1需要满足一个限制条件 输出m需要满足m个限制条件         while(l<=r){        if(r-l<=1){        if(solve(r)) tag=r;        else tag=l;        break;        }        mid=(l+r)/2;        if(solve(mid)) l=mid;        else r=mid;        }        printf("%d\n",tag);    }     return 0;}



原创粉丝点击