HDU 5682 zxa and leaf
来源:互联网 发布:农村淘宝加盟费 编辑:程序博客网 时间:2024/05/19 07:28
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 hasm(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?
zxa wanna set each node's beautiful level, which must be a positive integer. His unrooted tree has
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 integersn 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 nextk 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
For each test case:
The first line contains two positive integers
The next
The next
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.
Output
For each test case, output in one line a non-negative integer, repersents the minimum possible ugly level of the tree.
Sample Input
23 21 21 32 43 96 21 21 31 42 52 63 65 9
Sample Output
31HintIf you need a larger stack size, please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++.要保证最大的边最小,直接去做一般是很难的,这里通过二分答案然后用拓扑排序的方式验证答案。#include<map>#include<set>#include<queue>#include<stack>#include<cmath>#include<cstdio>#include<bitset>#include<string>#include<vector>#include<cstring>#include<iostream>#include<algorithm>#include<functional>using namespace std;typedef long long LL;const int low(int x) { return x&-x; }const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int maxn = 1e5 + 10;int T, n, m;int x, y, l, r;int q[maxn], h[maxn];int ft[maxn], nt[maxn], u[maxn], ct[maxn], sz, f[maxn];int mx[maxn], vis[maxn];bool check(int x){ queue<int> p; for (int i = 1; i <= n; i++) { if ((mx[i] = ct[i]) == 1) p.push(i); vis[i] = 0; if (f[i]) q[i] = h[i] = f[i]; else q[i] = l, h[i] = r; } while (!p.empty()) { int pp = p.front(); p.pop(); vis[pp] = 1; int L = max(l, q[pp] - x), R = min(r, h[pp] + x); for (int i = ft[pp]; i != -1; i = nt[i]) { if (vis[u[i]]) continue; if (--mx[u[i]] == 1) p.push(u[i]); q[u[i]] = max(q[u[i]], L); h[u[i]] = min(h[u[i]], R); if (q[u[i]] > h[u[i]]) return false; } } return true;}int main(){ scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); sz = 0; for (int i = 1; i <= n; i++) ft[i] = -1, f[i] = ct[i] = 0; for (int i = 1; i < n; i++) { scanf("%d%d", &x, &y); u[sz] = y; nt[sz] = ft[x]; ft[x] = sz++; u[sz] = x; nt[sz] = ft[y]; ft[y] = sz++; ct[x]++; ct[y]++; } l = INF; r = 0; for (int i = 1; i <= m; i++) { scanf("%d%d", &x, &y); f[x] = y; l = min(l, y); r = max(y, r); } if (n == 2) { if (m == 2) printf("%d\n", abs(f[1] - f[2])); else printf("0\n"); continue; } int a = 0, b = r - l; while (a <= b) { int mid = a + b >> 1; if (check(mid)) b = mid - 1; else a = mid + 1; } printf("%d\n", a); } return 0;}
0 0
- HDU 5682 zxa and leaf
- HDU 5682 zxa and leaf
- hdu 5682 zxa and leaf 二分答案
- HDU 5682:zxa and leaf 二分
- hdu 5682 zxa and leaf (二分+搜索)
- hdu 5682 zxa and leaf(树形DP+二分)
- HDU 5682 zxa and leaf 二分 树形dp
- 二分+树型 dp hdu5682 zxa and leaf
- hdu5682 zxa and leaf【dfs+树形dp】
- HDU 5680 zxa and set
- HDU 5681 zxa and wifi
- HDU 5683 zxa and xor
- HDU 5680 - zxa and set
- hdu 5680 zxa and set
- HDU 5680 zxa and set
- HDU 5681:zxa and wifi
- BestCoder #83 1003 zxa and leaf(二分查找/BFS)
- hdu hdu 5683 zxa and xor
- Web应用中Log4j与JNDI结合2 -- JNDI指定日志路径
- HDU 5681 zxa and wifi
- 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)
- 写在入职之前
- jsp使用jquery通过ajax方式访问servlet
- HDU 5682 zxa and leaf
- Codeforces-55D Beautiful numbers (数位DP)
- 179079552016-05-14 17:17:32bissA - Summer CampGNU C++11Accepted15 ms2200 KB
- 前置缓冲渲染
- hdu_5683_zxa and xor(非正解的暴力)
- amazon: 大于非负整数N的第一个回文数
- HDU 5612 Baby Ming and Matrix games(dfs暴力)
- 离职总结:大公司与小公司的个人体验
- 设置主页_加入收藏JQuery代码