ICPC2017 Urumqi A Possible Tree 带权并查集

5220: A Possible Tree

时间限制: 2 Sec  内存限制: 128 MB
提交: 156  解决: 52
[提交] [状态] [讨论版] [命题人: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

 

来源/分类

ICPC2017  Urumqi 

 

[提交] [状态]

题意:给定一个带权树,知道树的结构,但是不知道每条边的具体权重,然后给m个信息,u,v,val 表示在树上u 到 v 的路径上

经过的边的权重的异或和为val,问从前开始有多少个信息是不冲突的

只知道边之间的一个权重关系,然后判断冲突不冲突,那就是用带权并查集了(嗯,都是大佬的话,记住)

当 u,v 父亲是一个时,判断 val 等不等于已经存了的值

当父亲不是一个时,路径压缩加到一个,存下val值

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
int fa[maxn];
int val[maxn];
void init()
{
    for(int i=0; i<maxn; i++){
        fa[i] = i;
        val[i] = 0;
    }
}
int findd(int x)
{
    if(fa[x] == x)
        return x;
    int temp = fa[x];
    fa[x] = findd(fa[x]);
    val[x] ^= val[temp];
    return fa[x];
}
bool Union(int x,int y,int c)
{
    int fx = findd(x), fy = findd(y);
    if(fx != fy){
        fa[fx] = fy;
        val[fx] = val[x] ^ val[y] ^ c;
        return true;
    }
    int temp = val[x] ^ val[y];
    if( fx == fy && temp != c)  return false;
    return true;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        init();
        int n,m;
        scanf("%d%d",&n,&m);
        n--;
        int u,v,w;
        while(n--){
            scanf("%d%d",&u,&v);
        }
        int ans=0;
        for(int i=1; i<=m; i++){
            scanf("%d%d%d",&u,&v,&w);
            if(ans==0 && !Union(u,v,w) ){
                ans = i;
            }
        }
        printf("%d\n",ans-1);
    }
    return 0;
}

 

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值