DFS——hdu5682zxa and leaf

一、题目回顾

题目链接:zxa and leaf

Problem 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

If you need a larger stack size, please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++.

题意:一棵树n个点,其中有一些点已经有权值,现在给剩下的点安排权值,使得树中相邻两点的之差绝对值的最大值最小。

二、解题思路

  • 二分+树形dp(也可以二分+dfs)
时间: 2024-10-10 23:34:23

DFS——hdu5682zxa and leaf的相关文章

清北刷题冲刺 11-03 p.m

三向城 #include<iostream> #include<cstdio> using namespace std; int n,x,y; int main(){ freopen("city.in","r",stdin);freopen("city.out","w",stdout); // freopen("Cola.txt","r",stdin); scan

Leetcode dfs Sum Root to Leaf Numbers

Sum Root to Leaf Numbers Total Accepted: 20237 Total Submissions: 67979My Submissions Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents

LeetCode :: Sum Root to Leaf Numbers [tree、dfs]

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

leetcode_129题——Sum Root to Leaf Numbers(基于DFS的递归)

Sum Root to Leaf Numbers Total Accepted: 45133 Total Submissions: 148808My Submissions Question Solution Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->

129. Sum Root to Leaf Numbers(Tree; DFS)

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

[LeetCode] Sum Root to Leaf Numbers dfs,深度搜索

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

[email&#160;protected] [129] Sum Root to Leaf Numbers (DFS)

https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total

hdu 5682 zxa and leaf

zxa and leaf Accepts: 25 Submissions: 249 Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 zxa有一棵含有nn个节点的无根树,包含(n-1)(n−1)条无向边,点从11到nn编号,定义每个点的度数为与这个点相连的边的数量,度数为11的节点被称作这棵树的叶子节点. zxa想给每个节点设置它的好看度,好看度必须为正整数.他的无根树有m(

HDU 5682/BestCoder Round #83 1003 zxa and leaf 二分+树

zxa and leaf Problem 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