5220: A Possible Tree
时间限制: 2 Sec 内存限制: 128 MB
提交: 80 解决: 27
[提交] [状态] [讨论版] [命题人:admin]
题目描述
Alice knows that Bob has a secret tree (in terms of graph theory) with n nodes with n − 1 weighted edges with integer values in [0, 260 −1]. She knows its structure but does not know the specific information about edge weights.
Thanks to the awakening of Bob’s conscience, Alice gets m conclusions related to his tree. Each conclusion provides three integers u, v and val saying that the exclusive OR (XOR) sum of edge weights in the unique shortest path between u and v is equal to val.
Some conclusions provided might be wrong and Alice wants to find the maximum number W such that the first W given conclusions are compatible. That is say that at least one allocation of edge weights satisfies the first W conclusions all together but no way satisfies all the first W + 1 conclusions (or there are only W conclusions provided in total).
Help Alice find the exact value of W.
输入
The input has several test cases and the first line contains an integer t (1 ≤ t ≤ 30) which is the number of test cases.
For each case, the first line contains two integers n (1 ≤ n ≤ 100000) and c (1 ≤ c ≤ 100000) which are the number of nodes in the tree and the number of conclusions provided. Each of the following n−1 lines contains two integers u and v (1 ≤ u, v ≤ n) indicating an edge in the tree between the u-th node and the v-th node. Each of the following c lines provides a conclusion with three integers u, v and val where 1 ≤ u, v ≤ n and val ∈ [0, 260 − 1].
输出
For each test case, output the integer W in a single line.
样例输入
2 7 5 1 2 2 3 3 4 4 5 5 6 6 7 1 3 1 3 5 0 5 7 1 1 7 1 2 3 2 7 5 1 2 1 3 1 4 3 5 3 6 3 7 2 6 6 4 7 7 6 7 3 5 4 5 2 5 6
样例输出
3 4
题目是说给你一课树,然后每次给你两个点之间的所有边的亦或和的值,让你判断前面多少个给的信息是正确的。
其实答案和给的树并没有关系,用带权并查集维护一下一个点到他的祖先点的距离的亦或和。每次读入两个点和他们之间的距离的亦或和,如果和现有的并查集里的数据发生冲突了,则得到了结果,否则把新读入的两点之间的距离的亦或和存入并查集。
#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
typedef long long ll;
int n,c;
const int maxn = 100005;
int f[maxn];
ll vxor[maxn];
void init()
{
for (int i = 0; i <= n; i++)
f[i] = i;
memset(vxor, 0, sizeof(vxor));
}
int findd(int x)
{
if (f[x] == x)
return f[x];
// vxor[x] ^= vxor[f[x]];
ll tmp = f[x];
f[x] = findd(f[x]);
vxor[x] ^= vxor[tmp];
return f[x];
}
int main()
{
// freopen("in.txt","r",stdin);
int t,x,y,ans;
scanf("%d", &t);
while (t--)
{
ll w;
scanf("%d%d", &n, &c);
ans = c;
init();
for (int i = 1; i < n; i++)
scanf("%d%d", &x, &y);
scanf("%d%d%lld", &x, &y, &w);
int fx = findd(x);
int fy = findd(y);
f[fx] = fy;
vxor[fx] = vxor[x] ^ vxor[y] ^ w;
for (int i = 1; i < c; i++)
{
scanf("%d%d%lld", &x, &y, &w);
fx = findd(x);
fy = findd(y);
if (fx == fy && (vxor[x] ^ vxor[y]) != w)
{
ans = i;
for (; i < c - 1; i++) //把剩余的数据读完
scanf("%d%d%lld", &x, &y, &w);
break;
}
f[fx] = fy;
vxor[fx] = vxor[x] ^ vxor[y] ^ w;
}
printf("%d\n", ans);
}
return 0;
}