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 = +, M = 1e6+, mod = 1e9+,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=;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 = ;
return ;
}
}
bool check(int k) {
f = ;
dfs(root,-,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=;i<=n;i++) G[i].clear(),H[i] = ,d[i] = ;
for(int i=;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==) root = ;
else
for(int i=;i<=n;i++) {
if(d[i]!=) {
root = i;
break;
}
}
for(int i=;i<=m;i++) {
int x,y;
scanf("%d%d",&x,&y);
H[x] = y;
}
int l = ,r = 1e9,ans=1e9;
while(l<=r) {
int mid = (l+r)>>;
if(check(mid)) r=mid-,ans = mid;
else l = mid+;
}
printf("%d\n",ans);
}
return ;
}

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

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

    bestcoder round#45 1003 题,给定两个点,要我们求这两个点的树上路径所经过的点的权值是否出现过奇数次.如果是一般人,那么就是用lca求树上路径,然后判断是否出现过奇数次(用异或) ...

  2. hdu 5667 BestCoder Round #80 矩阵快速幂

    Sequence  Accepts: 59  Submissions: 650  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536 ...

  3. hdu 5643 BestCoder Round #75

    King's Game  Accepts: 249  Submissions: 671  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 6 ...

  4. hdu 5641 BestCoder Round #75

    King's Phone  Accepts: 310  Submissions: 2980  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: ...

  5. HDU 5682 zxa and leaf 二分 树形dp

    zxa and leaf 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5682 Description zxa have an unrooted t ...

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

    传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131 ...

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

    传送门 GTY's gay friends Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  8. HDU 5506 - BestCoder Round #60 - GT and set

    题目链接 : http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=641&pid=1003 题意 : 给N集 ...

  9. HDU 5505 - BestCoder Round #60 - GT and numbers

    题目链接 : http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=641&pid=1002 思路 : N有若 ...

随机推荐

  1. php-fpm.conf两个至关重要的参数

    这里规定了PHP-CGI的连接.发送和读取的时间,300秒足够用了,因此我的服务器很少出现504 Gateway Time-out这个错误.最关键的是php-fpm.conf的设置,这个会直接导致50 ...

  2. LiLinux系统下如何修改主机名

    1,用root用户登录,或者切换root用户,先查看当前的主机名:hostname  (如果之前没有修改过,一般默认为localhost.localdomain): 2,vi /etc/sysconf ...

  3. 【VNC】Linux环境VNC服务安装、配置与使用

     [VNC]Linux环境VNC服务安装.配置与使用 2009-06-25 15:55:31 分类: Linux   前言:作为一名DBA,在创建Oracle数据库的过程中一般要使用dbca和netc ...

  4. javascript 快速排序

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Windows主机里利用VMware安装Linux(CentOS)虚拟机,Host-only连接上网方式详解

    关于Host-only指的是主机与虚拟机之间的互联,因此虚拟机是不能连网的,若需要连网则需要使用NAT模式: Host-only模式实现联网得考虑如下配置过程: 附:VMware虚拟机三种网络模式(B ...

  6. 敲点JavaScript代码

    1. DOM DEMO-表格的行排序 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  7. 淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(六)

    Service和Thread的关系 不少初学者都可能会有这样的疑惑,Service和Thread到底有什么关系呢?什么时候应该用Service,什么时候又应该用Thread? 答案是Service和T ...

  8. Xen虚拟机克隆实战

    导读 在我们使用Xen虚拟化的时候,会经常创建虚拟机(VM),每次安装创建步骤比较繁琐,本文介绍通过virt-clone命令克隆xen虚拟机实战. 查看virt-clone命令是否存在 rpm -qa ...

  9. JAVA-- M选N的组合算法

      M选N的组合算法 只要每个数字出现一次就可以   举例 :也就是说123与321和213属于重复 只算一组  此算法已经排除了重复数据  应用--彩票的注数算法 本程序的思路是开一个数组b,其长度 ...

  10. aspx注入靶机源码

    ASPX:   <%@ Page language="c#" validateRequest=false %> <!DOCTYPE HTML PUBLIC &qu ...