题目描述

You are given a tree of $ n $ vertices. The vertices are numbered from $ 1 $ to $ n $ .

You will need to assign a weight to each edge. Let the weight of the $ i $ -th edge be $ a_i $ ( $ 1 \leq i \leq n-1 $ ). The weight of each edge should be an integer between $ 0 $ and $ 2^{30}-1 $ , inclusive.

You are given $ q $ conditions. Each condition consists of three integers $ u $ , $ v $ , and $ x $ . This means that the bitwise XOR of all edges on the shortest path from $ u $ to $ v $ should be $ x $ .

Find out if there exist $ a_1, a_2, \ldots, a_{n-1} $ that satisfy the given conditions. If yes, print a solution such that $ a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1} $ is the smallest. Here, $ \oplus $ denotes the bitwise XOR operation.

If there are multiple solutions such that $ a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1} $ is the smallest, print any.

输入格式

The first line contains two integers $ n $ and $ q $ ( $ 2 \le n \le 2.5 \cdot 10^5 $ , $ 0 \le q \le 2.5 \cdot 10^5 $ ).

The $ i $ -th of the following $ n-1 $ lines contains two integers $ x_i $ and $ y_i $ ( $ 1 \le x_i, y_i \le n $ , $ x_i \neq y_i $ ), meaning that the $ i $ -th edge connects vertices $ x_i $ and $ y_i $ in the tree.

It is guaranteed that the given edges form a tree.

The following $ q $ lines contain information about conditions.

Each line contains three integers $ u $ , $ v $ , $ x $ ( $ 1 \le u, v \le n $ , $ u \neq v $ , $ 0 \le x \le 2^{30}-1 $ ), meaning that the bitwise XOR of all edges on the shortest path from $ u $ to $ v $ should be $ x $ .

输出格式

If there do not exist $ a_1 $ , $ a_2 $ , ..., $ a_{n-1} $ that satisfy the given conditions, print "No".

Otherwise, print "Yes" in the first line.

Then print $ n-1 $ integers on the next line, where the $ i $ -th integer is the weight of the $ i $ -th edge. If there are multiple solutions that satisfy the given conditions, print a solution such that $ a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1} $ is the smallest.

If there are multiple solutions such that $ a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1} $ 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 $ a_1 \oplus a_2 \oplus a_3=2 $ and $ a_4 \oplus a_5=7 $ . There can be multiple solutions, for example, $ (a_1, a_2, a_3, a_4, a_5)=(1, 2, 1, 4, 3) $ .

For the third example, the two given conditions are $ a_1 \oplus a_2 \oplus a_3=3 $ and $ a_1 \oplus a_4 \oplus a_5=5 $ . There are multiple solutions that satisfy the given conditions.

$ (a_1, a_2, a_3, a_4, a_5)=(1, 1, 3, 4, 0) $ satisfies the given conditions, but the bitwise XOR of all edge weights is $ 7 $ , which does not have the smallest $ a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1} $ , so it cannot be the answer.

在树上路径的异或值有一个很巧妙的结论:设一个根,如果根节点到点 \(x\) 的异或和为 \(v_x\),那么路径 \((x,y)\) 的异或和为 \(v_x\oplus v_y\)

那么此时一个限制的意思就是 \(v_x\oplus v_y=a\),转化一下,\(v_x\oplus a=v_y\),那么此时可以通过构图的方式判定是否有解。

至于使所有点的权值异或和最小。那么考虑一个 \(v_x\) 什么时候会被算到总的点权异或和里面,当且仅当 \(x\) 在树中的度数是奇数。

那么此时要让异或和最小,可以将上面判断是否有解的图的每一个连通块钦定一个点 \(a\) 为代表,那么连通块中的每一个点的 \(v\) 值可以写为 \(v_a\oplus w\),那么如果这一个点会计算入总点权异或和,答案一定会多异或上一个 \(w\),\(v_a\) 是否会被计入也会发生改变。

然后此时如果没有一个连通块的点权会被计入答案,那就随便定 \(v_a\),不影响答案。否则可以记录所有的点的 \(w\) 异或之和为 \(s\),将一个被记入点权的 \(a\) 赋值为 \(s\),此时总异或和为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;
}

[CF1788F] XOR, Tree, and Queries的更多相关文章

  1. [Codeforces Round #221 (Div. 1)][D. Tree and Queries]

    题目链接:375D - Tree and Queries 题目大意:给你一个有n个点的树,每个点都有其对应的颜色,给出m次询问(v,k),问v的子树中有多少种颜色至少出现k次 题解:先对所有的询问进行 ...

  2. Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)

    题目链接  Tree and Queries 题目大意  给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...

  3. CodeForces 375D Tree and Queries 莫队||DFS序

    Tree and Queries 题意:有一颗以1号节点为根的树,每一个节点有一个自己的颜色,求出节点v的子数上颜色出现次数>=k的颜色种类. 题解:使用莫队处理这个问题,将树转变成DFS序区间 ...

  4. [多校联考2019(Round 5 T1)] [ATCoder3912]Xor Tree(状压dp)

    [多校联考2019(Round 5)] [ATCoder3912]Xor Tree(状压dp) 题面 给出一棵n个点的树,每条边有边权v,每次操作选中两个点,将这两个点之间的路径上的边权全部异或某个值 ...

  5. 「AGC035C」 Skolem XOR Tree

    「AGC035C」 Skolem XOR Tree 感觉有那么一点点上道了? 首先对于一个 \(n\),若 \(n\equiv 3 \pmod 4\),我们很快能够构造出一个合法解如 \(n,n-1, ...

  6. codeforces 375D:Tree and Queries

    Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

  7. CF375D Tree and Queries

    题意翻译 给出一棵 n 个结点的树,每个结点有一个颜色 c i . 询问 q 次,每次询问以 v 结点为根的子树中,出现次数 ≥k 的颜色有多少种.树的根节点是1. 感谢@elijahqi 提供的翻译 ...

  8. CodeForces 376F Tree and Queries(假·树上莫队)

    You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will ass ...

  9. 【思维题 状压dp】APC001F - XOR Tree

    可能算是道中规中矩的套路题吧…… Time limit : 2sec / Memory limit : 256MB Problem Statement You are given a tree wit ...

  10. AtCoder - 3913 XOR Tree

    Problem Statement You are given a tree with N vertices. The vertices are numbered 0 through N−1, and ...

随机推荐

  1. U268603 I Hate This Tree 题解

    传送门 一道纯粹的码力 + 卡常题. 前置 矩阵乘法,线段树. 分析 线段树存矩阵. 构造迭代矩阵: \[\begin{pmatrix}f_i&f_{i-1}\end{pmatrix}\tim ...

  2. Web安全漏洞解决方案

    1.已解密的登录请求 推理: AppScan 识别了不是通过 SSL 发送的登录请求. 测试请求和响应: 1.1.1 产生的原因 登录接口,前端传入的密码参数没有经过md5的加密就直接传给了后端 1. ...

  3. 快手Java一面11问(附参考答案)

    现在已经到了面试招聘比较火热的时候,后续会分享一些面试真题供大家复习参考.准备面试的过程中,一定要多看面经,多自测! 今天分享的是一位贵州大学的同学分享的快手一面面经. 快手一面主要会问一些基础问题, ...

  4. 《SQL与数据库基础》09. 事务

    @ 目录 事务 简介 操作 方式一 方式二 四大特性(ACID) 并发事务问题 事务隔离级别 本文以 MySQL 为例 事务 简介 事务是一组操作的集合,它是一个不可分割的工作单位.事务会把所有的操作 ...

  5. 使用API接口获取商品数据

    ​ 在当今的数字化时代,商品数据的获取对于各种规模的企业来说都至关重要.这些数据可以帮助企业进行市场分析,制定销售策略,优化库存管理,以及实现精准营销.API(应用程序编程接口)是一种便捷的方式来获取 ...

  6. Hugging News #0904:🤗 登陆 AWS Marketplace

    每一周,我们的同事都会向社区的成员们发布一些关于 Hugging Face 相关的更新,包括我们的产品和平台更新.社区活动.学习资源和内容更新.开源库和模型更新等,我们将其称之为「Hugging Ne ...

  7. Redis从入门到放弃(12):pipeline管道技术

    1.引言 在现代应用程序中,高性能和低延迟是至关重要的因素.而在处理大规模数据操作时,Redis作为一种快速.可靠的内存数据库,成为了许多开发人员的首选. 在Redis中,每个操作都需要与服务器进行往 ...

  8. 4.3 IAT Hook 挂钩技术

    IAT(Import Address Table)Hook是一种针对Windows操作系统的API Hooking 技术,用于修改应用程序对动态链接库(DLL)中导入函数的调用.IAT是一个数据结构, ...

  9. Solution -「洛谷 P5659」「CSP-S 2019」树上的数

    Description Link. 联赛原题应该都读过吧-- Solution Part 0 大致思路 主要的思路就是逐个打破,研究特殊的数据得到普通的结论. Part 1 暴力的部分分 暴力的部分分 ...

  10. Teamcenter RAC 开发之《AbstractRendering》

    背景 关于Teamcenter RAC 客制化渲染表单,做一两个有时间做还是可以的,问题是大批量做的时候就会存在很多重复的代码 例如: 1.定义很多 TCProperty,JTextFiled,ite ...