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 -1 weighted edges with integer values in [0,2601]. 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 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 Wconclusions 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 .

Input

The input has several test cases and the first line contains an integer t(1t30) which is the number of test cases.

For each case, the first line contains two integers n(1n100000) and c(1c100000) 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 (1u,vn) 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 uv and val where 1 uvn and val[0,2601].

Output

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
思路:带权并查集。r[i]表示i到p[i]路径上所有边的异或值。
#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+10;
const int MOD=1e9+7;
const double PI=acos(-1.0);
typedef long long ll;
int p[MAX],r[MAX];
int f(int x)
{
    if(p[x]==x)return x;
    int fa=p[x];
    p[x]=f(p[x]);
    r[x]^=r[fa];
    return p[x];
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n,c;
        scanf("%d%d",&n,&c);
        for(int i=1;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
        }
        for(int i=0;i<=n;i++)
        {
            p[i]=i;
            r[i]=0;
        }
        int ans=c+1;
        for(int i=1;i<=c;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            if(ans!=c+1)continue;
            int fx=f(x),fy=f(y);
            if(fx==fy&&(r[x]^r[y])!=z)ans=i;
            else
            {
                p[fx]=fy;
                r[fx]=r[x]^r[y]^z;
            }
        }
        printf("%d\n",ans-1);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值