1338B - Edge Weight Assignment
Posted curezero233
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1338B - Edge Weight Assignment相关的知识,希望对你有一定的参考价值。
题意:给出一棵树,要求给树上的每条边赋权值,使得任意两个叶子节点的路径上所有权值异或之后为0,边数n范围为[3,1e5],求使用的不同权值数的最小值和最大值。
题解:
任取一个叶子节点为树根建树。则题意可转化为
首先考虑最小值
由异或的性质(a^b^b=a,总存在c使得对任意a,b有a^b^c=0)知:
当链长为2n+1的时候,我们总可以用唯一的数构造出一条路径使其与lca到根的值相等(设这个值为x,只需填充这条链的值全为x)
当链长为2n的时候
-如果lca到根的异或值为0,都填充1即可让这条链异或值为0
-否则:填充m组(1,2,3),n组(1,1),使3m+2n+1=(链长),最后一个位置填充lca到根的值,不难看出这个值同样不大于3(因为依照前面的构造过程,不存在大于3的边,则异或结果也不大于3)
You have unweighted tree of n vertices. You have to assign a positive weight to each edge so that the following condition would hold:
- For every two different leaves v1 and v2 of this tree, bitwise XOR of weights of all edges on the simple path between v1 and v2 has to be equal to 0.
Note that you can put very large positive integers (like 10(1010)).
It‘s guaranteed that such assignment always exists under given constraints. Now let‘s define f as the number of distinct weights in assignment.
In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex 1 and vertex 6 (3,4,5,4) is not 0.
What are the minimum and the maximum possible values of f for the given tree? Find and print both.
The first line contains integer n (3≤n≤105) — the number of vertices in given tree.
The i-th of the next n−1 lines contains two integers ai and bi (1≤ai<bi≤n) — it means there is an edge between ai and bi. It is guaranteed that given graph forms tree of n vertices.
Print two integers — the minimum and maximum possible value of f can be made from valid assignment of given tree. Note that it‘s always possible to make an assignment under given constraints.
以上是关于1338B - Edge Weight Assignment的主要内容,如果未能解决你的问题,请参考以下文章
Edge Weight Assignment CodeForces
Codeforces827D. Best Edge Weight
「CF827D Best Edge Weight」 - LCT