2017乌鲁木齐ICPC: I. A Possible Tree(带权并查集)

版权声明:本文为博主原创文章,你们可以随便转载 https://blog.csdn.net/Jaihk662/article/details/82667362

 

I. A Possible Tree

Alice knows that Bob has a secret tree (in terms of graph theory) with n nodes with n -1n−1 weighted edges with integer values in [0, 260 - 1][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)OR(XOR) sum of edge weights in the unique shortest path between uu and vv is equal to val.

Some conclusions provided might be wrong and Alice wants to find the maximum number WW such that the first WWgiven conclusions are compatible. That is say that at least one allocation of edge weights satisfies the first WWconclusions all together but no way satisfies all the first W + 1W+1 conclusions (or there are only WW conclusions provided in total).

Help Alice find the exact value of WW .

Input

The input has several test cases and the first line contains an integer t (1 \le t \le 30)t(1≤t≤30) which is the number of test cases.

For each case, the first line contains two integers n (1 \le n \le 100000)n(1≤n≤100000) and c (1 \le c \le 100000)c(1≤c≤100000) which are the number of nodes in the tree and the number of conclusions provided. Each of the following n - 1n−1 lines contains two integers uu and v (1 \le u,v \le n)v(1≤u,v≤n) indicating an edge in the tree between the uu-th node and the vv-th node. Each of the following cc lines provides aa conclusion with three integers uu, vv and val where 1 \le u1≤u, v \le nv≤n and val \in [0, 260 - 1]val∈[0,260−1].

Output

For each test case, output the integer WW in a single line.

题意:

给你一棵树,每个节点都有一个值但你不知道是什么,m组讯息,每组三个数x, y, val表示x到y这条路径上所有的值异或为val,问从什么时候开始出现冲突(输出第一次发生冲突的组编号-1)

思路:

带权并查集模板题

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL long long
int fa[100005];
LL len[100005];
int Find(int x)
{
	int temp;
	if(fa[x]==0)
		return x;
	temp = Find(fa[x]);
	len[x] ^= len[fa[x]];
	fa[x] = temp;
	return temp;
}
int main(void)
{
	LL val;
	int T, i, m, x, y, t1, t2, n, flag;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d%d", &n, &m);
		for(i=0;i<=n;i++)
			len[i] = fa[i] = 0;
		for(i=1;i<=n-1;i++)
			scanf("%d%d", &x, &y);
		flag = m;
		for(i=1;i<=m;i++)
		{
			scanf("%d%d%lld", &x, &y, &val);
			t1 = Find(x);
			t2 = Find(y);
			if(t1!=t2)
			{
				fa[t2] = t1;
				len[t2] = len[x]^len[y]^val;
			}
			else
			{
				if((len[x]^len[y])!=val)
					flag = min(flag, i-1);
			}
		}
		printf("%d\n", flag);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Jaihk662/article/details/82667362
今日推荐