博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 5682 zxa and leaf 二分 树形dp
阅读量:6711 次
发布时间:2019-06-25

本文共 2373 字,大约阅读时间需要 7 分钟。

zxa and leaf

题目连接:

Description

zxa have an unrooted tree with n nodes, including (n−1) undirected edges, whose nodes are numbered from 1 to n. The degree of each node is defined as the number of the edges connected to it, and each node whose degree is 1 is defined as the leaf node of the tree.

zxa wanna set each node's beautiful level, which must be a positive integer. His unrooted tree has m(1≤m≤n) leaf nodes, k(1≤k≤m) leaf nodes of which have already been setted their beautiful levels, so that zxa only needs to set the other nodes' beautiful levels.

zxa is interested to know, assuming that the ugly level of each edge is defined as the absolute difference of the beautiful levels between two nodes connected by this edge, and the ugly level of the tree is the maximum of the ugly levels of all the edges on this tree, then what is the minimum possible ugly level of the tree, can you help him?

Input

The first line contains an positive integer T, represents there are T test cases.

For each test case:

The first line contains two positive integers n and k, represent the tree has n nodes, k leaf nodes of which have already been setted their beautiful levels.

The next (n−1) lines, each line contains two distinct positive integers u and v, repersent there is an undirected edge between node u and node v.

The next k lines, each lines contains two positive integers u and w, repersent node u is a leaf node, whose beautiful level is w.

There is a blank between each integer with no other extra space in one line.

It's guaranteed that the input edges constitute a tree.

1≤T≤10,2≤n≤5⋅104,1≤k≤n,1≤u,v≤n,1≤w≤109

Output

For each test case, output in one line a non-negative integer, repersents the minimum possible ugly level of the tree.

Sample Input

2

3 2
1 2
1 3
2 4
3 9
6 2
1 2
1 3
1 4
2 5
2 6
3 6
5 9

Sample Output

3

1

Hint

题意

zxa有一棵含有\(n\)个节点的无根树,包含\((n-1)\)条无向边,点从\(1\)\(n\)编号,定义每个点的度数为与这个点相连的边的数量,度数为\(1\)的节点被称作这棵树的叶子节点。

zxa想给每个节点设置它的好看度,好看度必须为正整数。他的无根树有\(m(1\leq m\leq n)\)个叶子节点,其中的\(k(1\leq k\leq m)\)个叶子节点的好看度已经确定,zxa只需要设置其他节点的好看度。

zxa很好奇,如果令每条边的难看度是这条边所连接的两个节点的好看度差值的绝对值,整棵树的难看度是所有边的难看度中的最大值,那么这棵树的难看度最小是多少,你能帮助他吗?

题解:

二分答案之后,树形dp去跑每个节点的取值范围就好了

如果范围是允许的,那就可以直接返回true

否则就返回false

挺好的一道题

代码

#include
using namespace std;const int maxn = 5e4+6;int w[maxn],n,k,l[maxn],r[maxn],vis[maxn];vector
E[maxn];int dfsl(int x,int d){ for(int i=0;i

转载地址:http://awalo.baihongyu.com/

你可能感兴趣的文章
Wpf Binding.Path设置
查看>>
jfinal控制器添加多个拦截器
查看>>
跟着百度学PHP[14]-初识PDO数据库抽象层
查看>>
小豆包的学习之旅:机器人定位
查看>>
好程序猿训练营,让你成为名副事实上的好程序猿
查看>>
有关一道printf 的面试题
查看>>
#AOS应用基础平台# 实现了在用户权限范围内自己定义的快捷菜单的导航展示
查看>>
[转]ssh中如何实现定时任务(spring对quartz的支持)
查看>>
mysql查询表的字符集
查看>>
读研以来的一些感想:名校好在哪里?
查看>>
大数据中心的业务研发路线
查看>>
rtmp推流开源代码备注一下
查看>>
SQLServer 日期函数大全 SQLServer 时间函数大全
查看>>
program与module
查看>>
软件架构师应该知道的97件事
查看>>
jquery操作"元素属性"
查看>>
用Python写一个本地Sogou代理服务器程序
查看>>
ASP.NET内置票据认证
查看>>
大话GC菜鸟系列
查看>>
C# WinForm TextBox猜想输入和历史记录输入(源码)
查看>>