您的位置:首页 > 编程语言 > Go语言

2-sat->HDU 3715 Go Deeper

2013-10-13 21:12 513 查看
Go Deeper
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU
3715

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

#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <stack>

using namespace std;

#define MAXN 1000000 + 10
#define MAXV 40000 + 10

struct Node
{
int u, v;
}temp[MAXN];

int c[MAXV], n, m;

struct Edge
{
int v, next;
}edge[MAXN << 2];

int head[MAXV], e, cnt, ind;
int low[MAXV], dfn[MAXV], belong[MAXV];
bool ins[MAXV];

void add(int u, int v)
{
edge[e].v = v;
edge[e].next = head[u];
head[u] = e++;
}

void init()
{
e = ind = cnt = 0;
memset(head, -1, sizeof(head));
memset(low, 0, sizeof(low));
memset(dfn, 0, sizeof(dfn));
memset(belong, 0, sizeof(belong));
memset(ins, false, sizeof(ins));
}

stack <int> s;

void tarjan(int u)
{
dfn[u] = low[u] = ++ind;

ins[u] = true;

s.push(u);

for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].v;

if (!dfn[v])
{
tarjan(v);

low[u] = min(low[u], low[v]);
}
else if (ins[v])
{
low[u] = min(low[u], dfn[v]);
}
}

if (low[u] == dfn[u])
{
cnt++;

int x;

do
{
x = s.top();
s.pop();
ins[x] = false;
belong[x] = cnt;
}while (x != u);
}
}

bool is_true()
{
for (int i = 0; i < 2 * n; i++)
{
if (!dfn[i])
{
tarjan(i);
}
}

for (int i = 0; i < n; i++)
{
if (belong[i] == belong[i + n])
{
return false;
}
}

return true;
}

void solve()
{
int low = 0, high = m;
int ans = -1;

while (low <= high)
{
int mid = low + (high - low) / 2;

init();

for (int i = 0; i < mid; i++)
{
if (c[i] == 0)
{
add(temp[i].u + n, temp[i].v);
add(temp[i].v + n, temp[i].u);
}
else if (c[i] == 1)
{
add(temp[i].u, temp[i].v);
add(temp[i].v, temp[i].u);
add(temp[i].u + n, temp[i].v + n);
add(temp[i].v + n, temp[i].u + n);
}
else if (c[i] == 2)
{
add(temp[i].u, temp[i].v + n);
add(temp[i].v, temp[i].u + n);
}
}

if (is_true())
{
ans = max(mid, ans);
low = mid + 1;
}
else
{
high = mid - 1;
}
}

cout << ans << endl;
}

void input()
{
int t;

cin >> t;

while (t--)
{
scanf("%d %d", &n, &m);

for (int i = 0; i < m; i++)
{
scanf("%d %d %d", &temp[i].u, &temp[i].v, &c[i]);
}

solve();
}
}

int main()
{
input();
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: