ACM-ICPC 2017 Asia Urumqi 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 WW given 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.

样例输入复制

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<stdio.h>
#include<string.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxm = 200005;
int p[maxm], sum[maxm];
int find(int k)
{
	if (k != p[k])
	{
		int tmp = p[k];
		p[k] = find(p[k]);
		sum[k] ^= sum[tmp];
	}
	return p[k];
}
int main()
{
	int n, i, j, k, x, y, tx, ty, val, m, ans, t;
	scanf("%d", &t);
	while (t--)
	{
		ans = 0;
		scanf("%d%d", &n, &m);
		for (i = 0;i <= n;i++)
			p[i] = i, sum[i] = 0;
		for (i = 1;i < n;i++)
			scanf("%d%d", &x, &y);
		for (i = 1;i <= m;i++)
		{
			scanf("%d%d%d", &x, &y, &val);
			tx = find(x), ty = find(y);
			if (tx == ty)
			{
				if ((sum[x] ^ sum[y]) != val&&ans == 0)
					ans = i;
			}
			else
			{
				p[tx] = ty;
				sum[tx] = sum[x] ^ val ^ sum[y];
			}
		}
		printf("%d\n", ans - 1);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值