[CF1788F] XOR, Tree, and Queries
题目描述
You are given a tree of vertices. The vertices are numbered from to .
You will need to assign a weight to each edge. Let the weight of the -th edge be ( ). The weight of each edge should be an integer between and , inclusive.
You are given conditions. Each condition consists of three integers , , and . This means that the bitwise XOR of all edges on the shortest path from to should be .
Find out if there exist that satisfy the given conditions. If yes, print a solution such that is the smallest. Here, denotes the bitwise XOR operation.
If there are multiple solutions such that is the smallest, print any.
输入格式
The first line contains two integers and ( , ).
The -th of the following lines contains two integers and ( , ), meaning that the -th edge connects vertices and in the tree.
It is guaranteed that the given edges form a tree.
The following lines contain information about conditions.
Each line contains three integers , , ( , , ), meaning that the bitwise XOR of all edges on the shortest path from to should be .
输出格式
If there do not exist , , ..., that satisfy the given conditions, print "No".
Otherwise, print "Yes" in the first line.
Then print integers on the next line, where the -th integer is the weight of the -th edge. If there are multiple solutions that satisfy the given conditions, print a solution such that is the smallest.
If there are multiple solutions such that is the smallest, print any.
When printing "Yes" or "No", you can print each letter in any case (either upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
样例 #1
样例输入 #1
4 4
1 2
2 3
3 4
1 4 3
2 4 2
1 3 1
2 3 1
样例输出 #1
No
样例 #2
样例输入 #2
6 2
1 2
2 3
3 4
2 5
5 6
1 4 2
2 6 7
样例输出 #2
Yes
4 2 4 1 6
样例 #3
样例输入 #3
6 2
1 2
2 3
3 4
2 5
5 6
1 4 3
1 6 5
样例输出 #3
Yes
6 1 4 3 0
提示
For the first example, there does not exist a set of edge weights that satisfies the given conditions.
For the second example, the two given conditions are and . There can be multiple solutions, for example, .
For the third example, the two given conditions are and . There are multiple solutions that satisfy the given conditions.
satisfies the given conditions, but the bitwise XOR of all edge weights is , which does not have the smallest , so it cannot be the answer.
在树上路径的异或值有一个很巧妙的结论:设一个根,如果根节点到点 的异或和为 ,那么路径 的异或和为
那么此时一个限制的意思就是 ,转化一下,,那么此时可以通过构图的方式判定是否有解。
至于使所有点的权值异或和最小。那么考虑一个 什么时候会被算到总的点权异或和里面,当且仅当 在树中的度数是奇数。
那么此时要让异或和最小,可以将上面判断是否有解的图的每一个连通块钦定一个点 为代表,那么连通块中的每一个点的 值可以写为 ,那么如果这一个点会计算入总点权异或和,答案一定会多异或上一个 , 是否会被计入也会发生改变。
然后此时如果没有一个连通块的点权会被计入答案,那就随便定 ,不影响答案。否则可以记录所有的点的 异或之和为 ,将一个被记入点权的 赋值为 ,此时总异或和为0。
// LUOGU_RID: 103370522
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5;
int n,q,d[N],in[N],f[N],p[N],ans,fl,a[N],b[N];
struct graph{
int hd[N],e_num;
struct edge{
int v,nxt,w;
}e[N<<1];
void add_edge(int u,int v,int w)
{
e[++e_num]=(edge){v,hd[u],w};
hd[u]=e_num;
// printf("%d %d %d\n",u,v,w);
}
}g,h;
void dfs(int x,int w,int t)
{
if(d[x]^w&&f[x])
{
puts("No");
exit(0);
}
if(!f[x])
{
d[x]=w,f[x]=t;
// printf("%d %d\n",x,f[x]);
for(int i=h.hd[x];i;i=h.e[i].nxt)
{
// printf("qzmyyds:%d\n",e[i].v);
dfs(h.e[i].v,w^h.e[i].w,t);
}
}
}
void sou(int x,int y)
{
for(int i=g.hd[x];i;i=g.e[i].nxt)
{
if(g.e[i].v^y)
sou(g.e[i].v,x);
else
b[g.e[i].w]=a[x]^a[y];
}
}
int main()
{
// memset(d,-1,sizeof(d));
scanf("%d%d",&n,&q);
for(int i=1;i<n;++i)
{
int u,v;
scanf("%d%d",&u,&v);
g.add_edge(u,v,i);
g.add_edge(v,u,i);
++in[u],++in[v];
}
while(q--)
{
int u,v,x;
scanf("%d%d%d",&u,&v,&x);
// printf("%d\n",x);
h.add_edge(u,v,x);
h.add_edge(v,u,x);
}
for(int i=1;i<=n;i++)
if(!f[i])
dfs(i,0,i);
// for(int i=1;i<=n;i++)
// printf("%d %d\n",f[i],d[i]);
puts("Yes");
for(int i=1;i<=n;i++)
if(in[i]&1)
p[f[i]]^=1,ans^=d[i];
// for(int i=1;i<=n;i++)
// printf("%d ",p[i]);
// puts("") ;
p[1]=0;
for(int i=2;i<=n;i++)
{
if(f[i]==i&&p[i])
p[i]=ans,ans=0;
// printf("hjhyyds:%d %d\n",d[i],p[f[i]]);
a[i]=d[i]^p[f[i]];
}
// for(int i=1;i<=n;i++)
// print
sou(1,0);
for(int i=1;i<n;i++)
printf("%d ",b[i]);
return 0;
}
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术