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)

 

D. Edge Weight Assignment
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You have unweighted tree of nn vertices. You have to assign a positive weight to each edge so that the following condition would hold:

  • For every two different leaves v1v1 and v2v2 of this tree, bitwise XOR of weights of all edges on the simple path between v1v1 and v2v2 has to be equal to 00.

Note that you can put very large positive integers (like 10(1010)10(1010)).

It‘s guaranteed that such assignment always exists under given constraints. Now let‘s define ff as the number of distinct weights in assignment.

技术图片In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is 00ff value is 22 here, because there are 22 distinct edge weights(44 and 55).

技术图片In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex 11 and vertex 66 (3,4,5,43,4,5,4) is not 00.

What are the minimum and the maximum possible values of ff for the given tree? Find and print both.

Input

The first line contains integer nn (3n1053≤n≤105) — the number of vertices in given tree.

The ii-th of the next n1n−1 lines contains two integers aiai and bibi (1ai<bin1≤ai<bi≤n) — it means there is an edge between aiai and bibi. It is guaranteed that given graph forms tree of nn vertices.

Output

Print two integers — the minimum and maximum possible value of ff 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

Codeforces 1339D - Edge Weight Assignment (数据结构 - 树)

邻接矩阵

networkx学习笔记