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

题意:

  http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=696&pid=1003

题解:

   二分答案LL,检查是否存在答案不超过LL的解,也即对于任意一条边上的两个点uu和vv,有|level_u-level_v|\leq L∣level?u??−level?v??∣≤L,如果能维护出每个点可能的取值区间就可以判断是否有解了。

对于n>1n>1的情况,随便找点做根,做树形dp即可,总的时间复杂度为O(n\log\max(w))O(nlogmax(w))。

  需要注意的就是当n==2的时候,这个根要找好

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include<vector>
#include<map>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N = 50000+20, M = 1e6+10, mod = 1e9+7,inf = 1e9;
typedef long long ll;

int n,m,root,f,H[N],mi[N],mx[N],d[N];
vector<int >G[N];
void dfs(int u,int fa,int k) {
    mi[u] = -inf;
    mx[u] = inf;
    if(H[u]) {
        mi[u] = H[u];
        mx[u] = H[u];
    }
    for(int i=0;i<G[u].size();i++) {
        int to = G[u][i];
        if(to==fa) continue;
        dfs(to,u,k);
        if(mi[to]==-inf) continue;
        mi[to] -= k;
        mx[to] += k;
        mi[u] = max(mi[u],mi[to]);
        mx[u] = min(mx[u],mx[to]);
    }
    if(mi[u]==-inf) {
       return ;
    }
    if(mi[u]>mx[u]) {
        f = 0;
        return ;
    }
}
bool check(int k) {
     f = 1;
     dfs(root,-1,k);
     if(f) return true;
     else return false;
}
int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) G[i].clear(),H[i] = 0,d[i] = 0;
        for(int i=1;i<n;i++) {
            int u,v;
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);d[u]++,d[v]++;
        }
        if(n==2) root = 1;
        else
        for(int i=1;i<=n;i++) {
            if(d[i]!=1) {
                root = i;
                break;
            }
        }
        for(int i=1;i<=m;i++) {
            int x,y;
            scanf("%d%d",&x,&y);
            H[x] = y;
        }
        int l = 0,r = 1e9,ans=1e9;
        while(l<=r) {
            int mid = (l+r)>>1;
            if(check(mid)) r=mid-1,ans = mid;
            else l = mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-08 00:04:19

HDU 5682/BestCoder Round #83 1003 zxa and leaf 二分+树的相关文章

BestCoder Round #29 1003 (hdu 5172) GTY&#39;s gay friends [线段树 判不同 预处理 好题]

传送门 GTY's gay friends Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 264    Accepted Submission(s): 57 Problem Description GTY has n gay friends. To manage them conveniently, every morning he o

从lca到树链剖分 bestcoder round#45 1003

bestcoder round#45 1003 题,给定两个点,要我们求这两个点的树上路径所经过的点的权值是否出现过奇数次.如果是一般人,那么就是用lca求树上路径,然后判断是否出现过奇数次(用异或),高手就不这么做了,直接树链剖分.为什么不能用lca,因为如果有树退化成链,那么每次询问的复杂度是O(n), 那么q次询问的时间复杂度是O(qn) 什么是树链剖分呢? 就是把树的边分成轻链和重链 http://blogsina.com.cn/s/blog_6974c8b20100zc61.htmlh

HDU 5280 BestCoder Round #47 1001:Senior&#39;s Array

Senior's Array Accepts: 199 Submissions: 944 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 某天学姐姐得到了一个数组A,在这个数组的所有非空区间中,她找出了一个区间和最大的,并把这个区间和定义为这个数组的美丽值. 但是她觉得这个数组不够美,于是决定修理一下这个数组. 学姐姐将会进行一次操作,把原数组中的某个数修改为P(必须修改)

HDU 5281 BestCoder Round #47 1002:Senior&#39;s Gun

Senior's Gun Accepts: 235 Submissions: 977 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 学姐姐是一个酷酷的枪手. 她常常会随身携带n把枪,每把枪有一个攻击力a[i]. 有一天她遇到了m只怪兽,每只怪兽有一个防御力b[j].现在她决定用手中的枪消灭这些怪兽. 学姐姐可以用第i把枪消灭第j只怪兽当且仅当b[j]≤a[i],同时她会获

HDU 5806 NanoApe Loves Sequence Ⅱ(尺取+思维)——BestCoder Round #86 1003

传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 514    Accepted Submission(s): 248 Problem Description NanoApe, the Retired Dog, has returned back to prepare for f

HDU 5778 abs(暴力枚举)——BestCoder Round #85 1003

传送门 abs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1474    Accepted Submission(s): 511 Problem Description Given a number x, ask positive integer y≥2, that satisfy the following condition

hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 221    Accepted Submission(s): 52 Problem Description A topological sort or topological ordering of a directed

STL之二分查找:hdu 5178 ( BestCoder Round #31 1001 )

STL包含四种不同的二分查找算法,binary_search    lower_bound  upper_bound   equal_range.他们的作用域是已经排序好的的数组. ★binary_search试图在已排序的[first, last)中寻找元素value.如果找到它会返回true,否则返回false,它不返回查找位置. ★iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素. ★iterat

HDU 5748 BestCoder Round #84 Bellovin (LIS)

Bellovin Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 540    Accepted Submission(s): 254 Problem Description Peter has a sequence a1,a2,...,an and he define a function on the sequence -- F