# A Possible Tree（裸带权并查集）

### 问题 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, 260 −1]. She knows its structure but does not know the speciﬁc 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 ﬁnd the maximum number W such that the ﬁrst W given conclusions are compatible. That is say that at least one allocation of edge weights satisﬁes the ﬁrst W conclusions all together but no way satisﬁes all the ﬁrst W + 1 conclusions (or there are only W conclusions provided in total).
Help Alice ﬁnd 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

[提交][状态]

1、对于每次询问，并查集逐渐完善这棵树。

2、用sum[i]表示i到根节点所有边权的异或和。

3、并查集维护这个异或和。

4、每次询问都进行判断，如果a,b根节点相同，看一下sum[a]^sum[b]是否等于c。如果不相同，就他们并起来，更新sum[fx]。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
int pre[MAX];
int sum[MAX];
int n,m,ans;
int find(int x)
{
int k=pre[x];
if(x!=pre[x])
{
pre[x]=find(pre[x]);
sum[x]=sum[x]^sum[k];
}
return pre[x];
}

int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ans=0;
scanf("%d%d",&n,&m);

memset(sum,0,sizeof(sum));
for(int i=0;i<=n;i++)
pre[i]=i;

for(int i=1;i<=n-1;i++)
{
int x,y;
scanf("%d%d",&x,&y);
}
ans=0;///数据不完善，ans应该初始化为m+1.
for(int i=1;i<=m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(ans)///这里根据上面也该写成ans!=m+1.
continue;

int fx=find(a);
int fy=find(b);
if(fx!=fy)
{
pre[fx]=fy;
sum[fx]=sum[a]^c^sum[b];
}
else if(fx==fy && (sum[a]^sum[b])!=c)
ans=i;
}
printf("%d\n",ans-1);
}
return 0;
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 打赏

打赏

七月初七小柠七

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 举报
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

08-23 90

02-25 1023
01-25 2858
01-22 237
06-19 129