hdu 3715 2-sat + 二分

题目:

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 a i-1 ,b i-1 and c i-1 (0 ≤ a i-1, b i-1 < n, 0 ≤ c i-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



分析见代码中的注释


代码:

#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-1
bool 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=0
C=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=0
			adde(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(){///546MS	2672K
	int 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;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值