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

 

posted @ 2018-08-25 10:58  任小喵  阅读(146)  评论(0编辑  收藏  举报