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

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

  1. 2017乌鲁木齐区域赛I(带权并查集)

    #include<bits/stdc++.h> using namespace std; int f[200010];//代表元 long long rl[200010];//记rl[i] ...

  2. 2017 西安网络赛A Tree(树上静态查询,带权并查集,矩阵乘法压位,好题)

    题目链接 题意: 给出 \(n(n \leq 3000)\) 个结点的一棵树,树上每个结点有一个 \(64 \times 64\) 的 \(0,1\)矩阵,每个结点上的矩阵是根据输入的 \(seed\ ...

  3. Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]

    传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  4. HDU 5176 The Experience of Love 带权并查集

    The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  5. BZOJ 2303 方格染色(带权并查集)

    要使得每个2*2的矩形有奇数个红色,如果我们把红色记为1,蓝色记为0,那么我们得到了这2*2的矩形里的数字异或和为1. 对于每个方格则有a(i,j)^a(i-1,j)^a(i,j-1)^a(i-1,j ...

  6. POJ1703带权并查集(距离或者异或)

    题意:       有两个黑社会帮派,有n个人,他们肯定属于两个帮派中的一个,然后有两种操作 1 D a b 给出a b 两个人不属于同一个帮派 2 A a b 问a b 两个人关系 输出 同一个帮派 ...

  7. POJ1988(带权并查集,搬砖块)

    题意:        可以这样理解,有n快方形积木,一开始都是单独的放到哪,然后有两种操作 1 M a b 把a所在的那一堆落到b所在那一堆的上面(一开始自己是一堆) 2 C a 问a下面有多少个积木 ...

  8. LA3027简单带权并查集

    题意:       有n个点,一开始大家都是独立的点,然后给出一些关系,a,b表示a是b的父亲节点,距离是abs(a-b)%1000,然后有一些询问,每次询问一个节点a到父亲节点的距离是多少? 思路: ...

  9. hdu3234 带权并查集(XOR)

    题意:       给你n个未知的正整数,有三总操作       I P V            P的值是V       I P Q V          P XOR Q = V       Q K ...

最新文章

  1. python包NiBabel对医学影像文件格式进行读写并可视化实战:查看和显示.nii.gz、.nii文件
  2. Thinkphp5.1允许uni-app的H5跨域请求接口解决方法
  3. Hadoop学习笔记(8) ——实战 做个倒排索引
  4. SpringBoot - 优雅的实现【流控】
  5. 动态规划求解0-1背包问题
  6. 【Flink】Flink 运行 实时修改消费组offset
  7. 测试原理_耐压测试仪绝缘电阻测试仪基本原理与选用
  8. 计算机 外存储能直接被cpu处理??,cpu可直接处理外存上的信息吗?_常见问题解析...
  9. mysql 覆盖式索引_【MySQL】性能优化之 覆盖索引
  10. 主板usb接口全部失灵_usb接口没反应,主板usb接口全部失灵
  11. 计算机图桌面打印出来,电脑excel图片无法打印怎么处理
  12. 数据到物联网服务器作用,物联网数据分析是什么?物联网数据分析如何操作?...
  13. proteus仿真 引脚显示电平变化但不能显示波形
  14. 【深度学习基础-02】概念学习-例子3则
  15. 文件误删除如何找回呢?四步妙招解决
  16. pagefile.sys删除
  17. 认知升级三部曲(深度好文)
  18. python 还原九宫格图片_python生成九宫格图片
  19. 超市会员管理系统 code
  20. line-height的底线、基线、中线、顶线

热门文章

  1. 零基础学python比较好的网课-请问自学 Python 有必要买课程吗?
  2. 为何python不好找工作-Python这么火,为何有人说Python不好找工作?
  3. python能做什么游戏-用Python可以做哪些有意思的小游戏呢?
  4. 编程语言python怎么读-Python之编程基础(编程语言分类)
  5. 今天的语音识别,我们就用Python来做,从基础的知识到实践的运用
  6. 谷歌语音识别功能Live Transcribe,让AI帮助语言障碍者“说话”
  7. linux系统怎么安装python3视频教程_Linux系统,python3.7安装教程
  8. 算法-查找数组两个数据相加等于目标值
  9. java 参数类型可变_java – 具有可变类型参数的通用
  10. java https请求 证书_java发https请求,证书配置