A Possible Tree
时间限制: 2 Sec 内存限制: 128 MB
提交: 154 解决: 50
[提交] [状态] [讨论版] [命题人: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
[题意]
带权的树, 给m 个信息, u,v,val, u-v 的权异或和为val.
问 你 前面 做到有多少个 是不冲突的, 不冲突 就是 异或和 是不是 和 他 不一样..
[思路]
学习了, 并查集 带个 数组, 代权并查集, 数组维护, 这里 是维护的是异或和,
[代码]
#include <bits/stdc++.h>
#include <stdio.h>
#define rep(i,a,n) for(int i= a; i<=n;i++)
#define per(i,a,n) for(int i = n;i>=a;i--)
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;
int pre[maxn];
ll sum[maxn];
int n,m;
int finds(int x)
{
if( pre[x] == x) return x;
else{
int temp = pre[x];
pre[x]= finds(pre[x]);
sum[x] ^=sum[temp];
return pre[x];
}
}
bool join(int u,int v, ll val)
{
int fx = finds(u);
int fy = finds(v);
if(fx == fy) return false;
if(fx != fy)
{
pre[fx] = fy;
sum[fx] ^=sum[u]^sum[v]^val;
return true;
}
}
int main(int argc, char const *argv[])
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
rep(i,0,n)
{
pre[i] = i;
sum[i] = 0;
}
int u,v;
ll val;
rep(i,1,n-1)
scanf("%*d %*d");
int ans = 0;
rep(i,1,m)
{
scanf("%d %d %lld",&u,&v,&val);
if( !join(u,v,val) && !ans && (sum[u]^sum[v])!=val)
ans = i-1 ;
}
printf("%d\n",ans);
}
return 0;
}