hdu 5682 zxa and leaf(树形DP+二分)
2016-05-17 21:14
351 查看
思路:二分答案之后,树形dp去跑每个节点的取值范围就好了
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
Sample Output
#include<bits/stdc++.h> using namespace std; const int maxn = 5e4+6; int w[maxn],n,k,l[maxn],r[maxn],vis[maxn]; vector<int>E[maxn]; int dfsl(int x,int d) { for(int i=0;i<E[x].size();i++) { if(!vis[E[x][i]]) { vis[E[x][i]]=1; l[x]=max(l[x],dfsl(E[x][i],d)); } } return l[x]-d; } int dfsr(int x,int d) { for(int i=0;i<E[x].size();i++) { if(!vis[E[x][i]]) { vis[E[x][i]]=1; r[x]=min(r[x],dfsr(E[x][i],d)); } } return r[x]+d; } bool check(int mid) { for(int i=1;i<=n;i++) if(w[i])l[i]=r[i]=w[i]; else l[i]=0,r[i]=1e9; memset(vis,0,sizeof(vis)); dfsl(1,mid); memset(vis,0,sizeof(vis)); dfsr(1,mid); for(int i=1;i<=n;i++) if(r[i]<l[i])return false; return true; } void solve() { for(int i=0;i<maxn;i++)E[i].clear(); for(int i=0;i<maxn;i++)w[i]=0; scanf("%d%d",&n,&k); for(int i=1;i<n;i++) { int x,y;scanf("%d%d",&x,&y); E[x].push_back(y); E[y].push_back(x); } for(int i=1;i<=k;i++) { int x,y; scanf("%d%d",&x,&y); w[x]=y; } int L=0,R=1e9,ans=1e9; while(L<=R) { int mid=(L+R)/2; if(check(mid))R=mid-1,ans=mid; else L=mid+1; } cout<<ans<<endl; } int main() { int t;scanf("%d",&t); while(t--)solve(); return 0; }
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++.
相关文章推荐
- Java的多态
- hge source explor 0x1
- [Javascrip] Logging Timing Data to the Console
- cp 强制覆盖不提示
- PHP第三方登录—QQ登录
- yii2批量添加的问题
- Android加载大图片,LRU缓存机制
- poj2823 Sliding Windows(单调队列果题)
- 数据库知识点③
- Git协作流程详解
- S-DES加密
- MSSQL2005数据库显示单一用户模式,无法进行任何操作
- Using jdbc to access sqlite in Android
- java类单继承,接口多继承设计的原因
- 设计模式之模板模式
- Android与JS交互 -----点击js页面复制一条信息到android 剪切板中
- Scrapy
- 【bzoj3631】[JLOI2014]松鼠的新家
- Java编译、反编译、JAR
- POJ3468 A Simple Problem with Integers 线段树成段更新