hdoj 3715 Go Deeper 【2-sat 判断可行解 + 二分】

19 篇文章 0 订阅

Go Deeper

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


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
 

题意:让你根据伪代码的流程求出go(0, N, M) 的可能的最大值。
 a[],b[]由0、1、2...n构成,长度为M 
 c[]数组由0、1、2构成,长度为M
 x[]数组由0、1构成,长度为N 

核心算法实现:二分查找区间[0, M],对于当前查找的值mid,建新图,判断解是否可行。


建图思路:用a[ i ] 表示x[ a[ i ] ] 为0,用 a[ i ] + N 表示 x[ a[ i ] ] 为1。我们设定 x = a[ i ],y = b[ i ], z = c[ i ]

则有

一:z 为 0

1,x为0时 y一定不为0; addEdge(x, y + N);    
2,y为0时 x一定不为0; addEdge(y, x + N);


二:z 为 1

1,x为1时y一定不为0;  addEdge(x + N, y + N);
2,y为1时x一定不为0;  addEdge(y + N, x + N);
3,x为0时y一定不为1;  addEdge(x, y);  
4,y为0时x一定不为1;  addEdge(y, x); 

三:z 为 2

1,x为1时y一定不为1;  addEdge(x + N, y);      
2,y为1时x一定不为1;  addEdge(y + N, x); 
建好图,就是tarjan求SCC然后判断当前解是否可行了。注意数组a[],b[],c[] 的范围。


AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 10000+10
#define MAXM 100000+10
#define INF 100000
#define eps 1e-5
using namespace std;
struct Edge
{
	int from, to, next;
}edge[MAXM];
int head[MAXN], edgenum;
int low[MAXN], dfn[MAXN];
int dfs_clock;
int sccno[MAXN], scc_cnt;
stack<int> S;
bool Instack[MAXN];
int N, M;
int a[MAXN], b[MAXN], c[MAXN];//注意数组的范围 
void input()
{
	for(int i = 0; i < M; i++)
	scanf("%d%d%d", &a[i], &b[i], &c[i]);
}
void init()
{
	edgenum = 0;
	memset(head, -1, sizeof(head));
}
void addEdge(int u, int v)
{
	Edge E = {u, v, head[u]};
	edge[edgenum] = E;
	head[u] = edgenum++;
}
void getMap(int IT)
{
	int x, y, z;
	for(int i = 0; i < IT; i++)
	{
		int x = a[i];
		int y = b[i];
		int z = c[i];
		switch(z)
		{
			case 0:
			//x为0时y一定不为0  y为0时x一定不为0 
			addEdge(x, y + N);     addEdge(y, x + N); 
			break;
			case 1:
			//x为1时y一定不为0      y为1时x一定不为0  
			addEdge(x + N, y + N); addEdge(y + N, x + N);
			//x为0时y一定不为1      y为0时x一定不为1 
			addEdge(x, y);         addEdge(y, x); 
			break;
			case 2: 
			//x为1时y一定不为1      y为1时x一定不为1 
			addEdge(x + N, y);     addEdge(y + N, x); 
		}
	}
}
void tarjan(int u, int fa)//tarjan 求 SCC 
{
	int v;
	low[u] = dfn[u] = ++dfs_clock;
	S.push(u);
	Instack[u] = true;
	for(int i = head[u]; i != -1; i = edge[i].next)
	{
		v = edge[i].to;
		if(!dfn[v])
		{
			tarjan(v, u);
			low[u] = min(low[u], low[v]);
		}
		else if(Instack[v])
		low[u] = min(low[u], dfn[v]);
	}
	if(low[u] == dfn[u])
	{
		scc_cnt++;
		for(;;)
		{
			v = S.top(); S.pop();
			Instack[v] = false;
			sccno[v] = scc_cnt;
			if(v == u) break;
		}
	}
}
void find_cut(int l, int r)
{
	memset(low, 0, sizeof(low));
	memset(dfn, 0, sizeof(dfn));
	memset(sccno, 0, sizeof(sccno));
	memset(Instack, false, sizeof(Instack));
	dfs_clock = scc_cnt = 0;
	for(int i = l; i <= r; i++)
	if(!dfn[i]) tarjan(i, -1); 
}
bool two_sat()//判断当前解 是否可行
{
	for(int i = 0; i < N; i++)
	{
		if(sccno[i] == sccno[i + N])
		return false;
	}
	return true;
}
void solve()//二分查找 
{
	int left, right, mid, ans;
	left = 0, right = M;
	while(right >= left)
	{
		mid = (left + right) / 2;
		init();//每次初始化 
		getMap(mid);//建新图 
		find_cut(0, 2*N-1);//求SCC 
		if(two_sat())//判断当前解 是否可行 
		{
			ans = mid;
			left = mid + 1;
		}
		else
		right = mid - 1;
	}
	printf("%d\n", ans);
}
int main()
{
	int t;
	scanf("%d", &t);
	while(t--)
	{
		scanf("%d%d", &N, &M);
		input();
		solve();
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值