HDU 5682 zxa and leaf 二分 树形dp
zxa and leaf
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5682
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
题意
zxa有一棵含有\(n\)个节点的无根树,包含\((n-1)\)条无向边,点从\(1\)到\(n\)编号,定义每个点的度数为与这个点相连的边的数量,度数为\(1\)的节点被称作这棵树的叶子节点。
zxa想给每个节点设置它的好看度,好看度必须为正整数。他的无根树有\(m(1\leq m\leq n)\)个叶子节点,其中的\(k(1\leq k\leq m)\)个叶子节点的好看度已经确定,zxa只需要设置其他节点的好看度。
zxa很好奇,如果令每条边的难看度是这条边所连接的两个节点的好看度差值的绝对值,整棵树的难看度是所有边的难看度中的最大值,那么这棵树的难看度最小是多少,你能帮助他吗?
题解:
二分答案之后,树形dp去跑每个节点的取值范围就好了
如果范围是允许的,那就可以直接返回true
否则就返回false
挺好的一道题
代码
#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;
}
HDU 5682 zxa and leaf 二分 树形dp的更多相关文章
- HDU 3586 Information Disturbing(二分+树形dp)
http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超 ...
- hdu 5682 zxa and leaf
zxa and leaf Accepts: 25 Submissions: 249 Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 6 ...
- 【题解】hdu 3586 Information Disturbing 二分 树形dp
题目描述 Information DisturbingTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java ...
- 【bzoj5174】[Jsoi2013]哈利波特与死亡圣器 二分+树形dp
题目描述 给你一棵以1为根的有根树,初始除了1号点为黑色外其余点均为白色.Bob初始在1号点.每次Alice将其中至多k个点染黑,然后Bob移动到任意一个相邻节点,重复这个过程.求最小的k,使得无论B ...
- 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 ...
- HDU 1520.Anniversary party 基础的树形dp
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Codeforces 627D Preorder Test(二分+树形DP)
题意:给出一棵无根树,每个节点有一个权值,现在要让dfs序的前k个结点的最小值最大,求出这个值. 考虑二分答案,把>=答案的点标记为1,<答案的点标记为0,现在的任务时使得dfs序的前k个 ...
- HDU 6201 2017沈阳网络赛 树形DP或者SPFA最长路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6201 题意:给出一棵树,每个点有一个权值,代表商品的售价,树上每一条边上也有一个权值,代表从这条边经过 ...
- bzoj 2067: [Poi2004]SZN【贪心+二分+树形dp】
第一问就是Σ(deg[u]-1)/2+1 第二问是二分,判断的时候考虑第一问的贪心规则,对于奇度数的点,两两配对之后一条延伸到上面:对于欧度数的点,两两配对或者deg[u]-2的点配对,然后一条断在这 ...
随机推荐
- python进阶之类常用魔法方法和魔法属性
前言 前面我们总结过了python的关键字.运算符.内置函数.语法糖等与python魔法方法之间的关系,现在我们更细一点,看看python的面向对象编程有哪些常用的魔法属性和魔法方法. 魔法属性 对于 ...
- mysql insert锁机制【转】
最近再找一些MySQL锁表原因,整理出来一部分sql语句会锁表的,方便查阅,整理的不是很全,都是工作中碰到的,会持续更新 笔者能力有限,如果有不正确的,或者不到位的地方,还请大家指出来,方便你我,方便 ...
- Java不为人知的小秘密
Java中的main方法必须有一个外壳类,而且必须是静态的! Java中的所有函数都属于某个类的方法,所以main方法也不例外,必须放在一个类中才能编译运行. 例如: public class tex ...
- linux limits研究
---------------------------------------------------------------------------------------------------- ...
- 排序算法——Shell排序
二.Shell排序 Shell排序也叫“缩减增量排序”(disminishing increment sort),基于插入排序进行. Shell建议的序列是一种常用但不理想的增量序列:1,...,N/ ...
- 多线程 or I/O复用select/epoll
1:多线程模型适用于处理短连接,且连接的打开关闭非常频繁的情形,但不适合处理长连接.线程模型默认情况下,在Linux下每个线程会开8M的栈空间,在TCP长连接的情况下,以2000/分钟的请求为例,几乎 ...
- Merkle Tree(默克尔树)算法解析
Merkle Tree概念 Merkle Tree,通常也被称作Hash Tree,顾名思义,就是存储hash值的一棵树.Merkle树的叶子是数据块(例如,文件或者文件的集合)的hash值.非叶节点 ...
- python 比timedelta强大的多的 relativedelta
datetime包中的timedelta功能有限,比如,一个月的delta都没法表示.dateutil包中的relativedelta要强大很多. 年月日周的delta都能支持,还有weekday, ...
- html meta标签使用总结(转)
之前学习前端中,对meta标签的了解仅仅只是这一句. <meta charset="UTF-8"> 但是打开任意的网站,其head标签内都有一列的meta标签.比如我博 ...
- Spark介绍及安装部署
一.Spark介绍 1.1 Apache Spark Apache Spark是一个围绕速度.易用性和复杂分析构建的大数据处理框架(没有数据存储).最初在2009年由加州大学伯克利分校的AMPLab开 ...