zxa and leaf

zxa and leaf

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 502    Accepted Submission(s): 198

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++.

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=1e5+10;
struct node{
     int to,nx;
}p[maxn];
int T,n,k,head[maxn],vis[maxn],tot,d[maxn];
ll small[maxn],big[maxn],val[maxn];
void addedge(int u,int v){
       p[++tot].to=v,p[tot].nx=head[u],head[u]=tot;
}
bool dfs(int now,int fa,int w){
      for(int i=head[now];i;i=p[i].nx){
            int to=p[i].to;
            if(to==fa)continue;
            if(!dfs(to,now,w))return false;
            ll tempsmall=small[to]-w;
            ll tempbig=big[to]+w;
            if(tempsmall>big[now]||tempbig<small[now])return false;
            small[now]=max(small[now],tempsmall);
            big[now]=min(big[now],tempbig);
      }
      return true;
}
bool ok(int cur,int root){
      for(int i=1;i<=n;i++){
           if(vis[i])continue;
           small[i]=-0x3f3f3f3f;
           big[i]=0x3f3f3f3f;
      }
      if(dfs(root,-1,cur))return true;
      else return false;
}
int main()
{
    scanf("%d",&T);
    while(T--){
        tot=0;
        scanf("%d%d",&n,&k);
        memset(d,0,sizeof(d));
        memset(head,0,sizeof(head));
        memset(vis,0,sizeof(vis));
        memset(small,0,sizeof(small));
        memset(big,0,sizeof(big));
        for(int i=1;i<=n-1;i++){
            int a,b;
            scanf("%d%d",&a,&b);
            addedge(a,b);
            addedge(b,a);
            d[a]++,d[b]++;
        }
        while(k--){
            int a,b;
            scanf("%d%d",&a,&b);
            small[a]=big[a]=b;
            vis[a]=1;
        }
        int root=1;
        for(int i=1;i<=n;i++){
            if(!d[i]){
                root=i;
                break;
            }
        }
        int l=0,r=0x3f3f3f3f;
        while(l<=r){
            int mid=l+r>>1;
            if(ok(mid,root))r=mid-1;
            else l=mid+1;
        }
        cout<<l<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/czy-power/p/10562375.html

时间: 2024-10-06 11:31:11

zxa and leaf的相关文章

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

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

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

【leetcode刷题笔记】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 sum of all root-to-leaf numbers. For example, 1 / 2 3 T

Sum Root to Leaf Numbers

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 sum of all root-to-leaf number

[leetcode]_Sum Root to Leaf Numbers

题目:计算一棵二叉树所有路径组成的数的总和. 思考:也是DFS的基础应用.虽然还是套着别人的DFS框架写的,但是学习通常会经历先模拟,再创新的过程. 代码: 1 private int sum = 0; 2 public int sumNumbers(TreeNode root) { 3 dfs(root , 0); 4 return sum; 5 } 6 public void dfs(TreeNode node , int tempSum){ 7 if(node == null) retur

leetcode --day12 Surrounded Regions &amp; Sum Root to Leaf Numbers &amp; Longest Consecutive Sequence

1.  Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your fu

LeetCode_Sum Root to Leaf Numbers

一.题目 Sum Root to Leaf Numbers Total Accepted: 47437 Total Submissions: 156443My 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 repre