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的时候,这个根要找好

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cmath>
  6. #include<vector>
  7. #include<map>
  8. #pragma comment(linker, "/STACK:102400000,102400000")
  9. using namespace std;
  10. const int N = +, M = 1e6+, mod = 1e9+,inf = 1e9;
  11. typedef long long ll;
  12.  
  13. int n,m,root,f,H[N],mi[N],mx[N],d[N];
  14. vector<int >G[N];
  15. void dfs(int u,int fa,int k) {
  16. mi[u] = -inf;
  17. mx[u] = inf;
  18. if(H[u]) {
  19. mi[u] = H[u];
  20. mx[u] = H[u];
  21. }
  22. for(int i=;i<G[u].size();i++) {
  23. int to = G[u][i];
  24. if(to==fa) continue;
  25. dfs(to,u,k);
  26. if(mi[to]==-inf) continue;
  27. mi[to] -= k;
  28. mx[to] += k;
  29. mi[u] = max(mi[u],mi[to]);
  30. mx[u] = min(mx[u],mx[to]);
  31. }
  32. if(mi[u]==-inf) {
  33. return ;
  34. }
  35. if(mi[u]>mx[u]) {
  36. f = ;
  37. return ;
  38. }
  39. }
  40. bool check(int k) {
  41. f = ;
  42. dfs(root,-,k);
  43. if(f) return true;
  44. else return false;
  45. }
  46. int main() {
  47. int T;
  48. scanf("%d",&T);
  49. while(T--) {
  50. scanf("%d%d",&n,&m);
  51. for(int i=;i<=n;i++) G[i].clear(),H[i] = ,d[i] = ;
  52. for(int i=;i<n;i++) {
  53. int u,v;
  54. scanf("%d%d",&u,&v);
  55. G[u].push_back(v);
  56. G[v].push_back(u);d[u]++,d[v]++;
  57. }
  58. if(n==) root = ;
  59. else
  60. for(int i=;i<=n;i++) {
  61. if(d[i]!=) {
  62. root = i;
  63. break;
  64. }
  65. }
  66. for(int i=;i<=m;i++) {
  67. int x,y;
  68. scanf("%d%d",&x,&y);
  69. H[x] = y;
  70. }
  71. int l = ,r = 1e9,ans=1e9;
  72. while(l<=r) {
  73. int mid = (l+r)>>;
  74. if(check(mid)) r=mid-,ans = mid;
  75. else l = mid+;
  76. }
  77. printf("%d\n",ans);
  78. }
  79. return ;
  80. }

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. Netbeans连接数据库

    /* Netbeans连接数据库 NetBeans项目的“项目属性”中“库”一栏中.Tab页“编译和运行”中已经加上jdbc的驱动文件 */ Connection conn = null;//连接数据 ...

  2. js Float 精度

    1.加法 //加法 function add(arg1,arg2){ var r1,r2,m; try{r1=arg1.toString().split(".")[1].lengt ...

  3. 转:Java NIO系列教程(四) Scatter/Gather

    Java NIO开始支持scatter/gather,scatter/gather用于描述从Channel(译者注:Channel在中文经常翻译为通道)中读取或者写入到Channel的操作.分散(sc ...

  4. Java面试宝典2015版(绝对值得收藏超长版)

    31.String s = "Hello";s = s + " world!";这两行代码执行后,原始的String对象中的内容到底变了没有? 没有.因为Str ...

  5. Ubuntu如何安装secureCRT

    以前在ubuntu上安装过secureCRT,是自己按照网上的教程安装的. 电脑重装了系统之后,想在电脑上安装一个,又得去网上搜,安装完后,自己总结了一下. 1,下载secureCRT包 根据自己电脑 ...

  6. 读>>>>白帽子讲Web安全<<<<摘要→我推荐的一本书→1

      <白帽子讲Web安全>吴翰清著 刚开始看这本书就被这本书吸引,感觉挺不错,给大家推荐下,最近读这本书,感觉不错的精华就记录下, 俗话说>>>好脑袋不如一个烂笔头< ...

  7. sql中文字符串获取拼音首字母

    SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO )) ) as begin ),) set @PY='' begin ) --如果非汉字字符,返回原字 ...

  8. hMailServer+foxmail配置局域网邮件服务器

    1.下载hMailServer并安装,请参考以下网址 https://www.hmailserver.org 2.安装foxmail,官网如下: http://www.foxmail.com/ 3.配 ...

  9. Coursera台大机器学习技法课程笔记03-Kernel Support Vector Machine

    这一节讲的是核化的SVM,Andrew Ng的那篇讲义也讲过,讲的也不错. 首先讲的是kernel trick,为了简化将低维特征映射高维特征后的计算,使用了核技巧.讲义中还讲了核函数的判定,即什么样 ...

  10. [转载]WiFi有死角? 巧用旧无线路由器扩展覆盖

    怎么了,家里的WiFi有死角?老旧无线路由器的无线覆盖不给力?现在大功率无线产品或双频无线产品的售价并不便宜,而且仅靠一台无线路由器并不能满足多户型家庭的无线覆盖需求.那么,是不是有什么廉价而又实用的 ...