首页 > 其他分享 >[CF1788F] XOR, Tree, and Queries

[CF1788F] XOR, Tree, and Queries

时间:2023-03-15 16:34:33浏览次数:58  
标签:CF1788F given le XOR 样例 Tree 异或 oplus conditions

题目描述

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,given,le,XOR,样例,Tree,异或,oplus,conditions
From: https://www.cnblogs.com/mekoszc/p/17219025.html

相关文章

  • delphi 再说TcxDBTreeList
    1.当我们绑定好数据库之后,默认是全部折叠的,只显示  +全部    cxDBTreeList1.Root.getFirstChild.Expand(False);//只展开第一层目录cxDBTreeList1.FullE......
  • [LeetCode] 958. Check Completeness of a Binary Tree
    Giventhe root ofabinarytree,determineifitisa completebinarytree.Ina completebinarytree,everylevel,exceptpossiblythelast,iscompletely......
  • Solidity实现默克尔树 Merkle Tree
    ​​MerkleTree​​​,也叫默克尔树或哈希树,是区块链的底层加密技术,被BTC和Ethereum区块链广泛采用。​​MerkleTree​​​是一种自下而上构建的加密树,每个叶子是对应数据......
  • 【题解】P6071 『MdOI R1』Treequery
    题目描述给定一棵\(n\)个点的无根树,边有边权。令\(E(x,y)\)表示树上\(x,y\)之间的简单路径上的所有边的集合,特别地,当\(x=y\)时,\(E(x,y)=\varnothing\)。你需......
  • B+Tree/Hash_Map/STL Map
    1、Hash操作能根据散列值直接定位数据的存储地址,设计良好的hash表能在常数级时间下找到需要的数据,但是更适合于内存中的查找。2、B+树是一种是一种树状的数据结构,适合做索......
  • 看了还不懂b+tree的本质就来打我
    看了还不懂b+tree的本质就来打我大家好,我是蓝胖子。今天我们来看看b+tree这种数据结构,我们知道数据库的索引就是由b+tree实现,那么这种结构究竟为什么适合磁盘呢,它又有哪......
  • 为什么commonjs不能treeshaking
    因为只有模块是静态导入时,treeshaking才有效果,commonjs可以有如下写法if(flag){require('./a.js')}else{require('./b.js')}我是这样理解的,在代码没有运行之前,......
  • CF375D Tree and Queries - 树上莫队 -
    题目链接:https://codeforces.com/contest/375/problem/D题解:询问的子树可以看成求出dfs序之后的一段连续序列,因此可以使用树上莫队。首先将dfs序求出来,对于每个点,计......
  • DevExpress的TreeList实现显示本地文件目录并自定义右键实现删除与重命名文件
    场景使用DevExpress的TreeList显示本磁盘下文件目录并在树节点上右键实现删除与添加文件。效果 自定义右键效果  实现首先在包含Treelist的窗体的load方法中对treelist进......
  • Winforn中DevExpress的TreeList中显示某路径下的所有目录和文件(附源码下载)
    场景Winform中DevExpress的TreeList的入门使用教程(附源码下载):在上面实现给TreeList赋值的基础上,将其数据源更改为本地某路径下的所有文件和目录。效果实现在原来的节点类中......