首页 > ACM题库 > HDU-杭电 > HDU 3715-Go Deeper-分治-[解题报告]HOJ
2015
02-21

HDU 3715-Go Deeper-分治-[解题报告]HOJ

Go Deeper

问题描述 :

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?

输入:

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).

输出:

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).

样例输入:

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

样例输出:

1
1
2

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3715

思路:二分深度,2-sat判断可行性,根据矛盾关系建图:设a=0,a’=1,b=0,b’=1;如果c[i]==0,则a,b矛盾,连边a->b’,b->a’;如果c[i]==1,则a,b’矛盾,连边a->b,b’->a’,a’,b矛盾,连边a’->b’,b->a;如果c[i]==2,则连边a’->b,b’->a,然后就是强连通判断即可。

http://paste.ubuntu.com/5973263/

 

参考:http://www.cnblogs.com/wally/archive/2013/08/11/3251812.html