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

 

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;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值