T - zxa and leaf HDU - 5682 二分+dfs
T - zxa and leaf
题目大意是:给你一颗树,这棵树有些节点已经设置了它的美丽值,然后剩下一些节点需要我们设置美丽值。
一条边的丑陋程度等于被定义为由这个边缘连接的两个节点之间的美丽水平的绝对差异
一棵树的丑陋程度则是边的丑陋程度的最大值。
这个题目应该是二分这个丑陋值。
然后dfs来判断这个丑陋值是不是合理。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + ;
vector<int>G[maxn];
ll lc[maxn], rc[maxn], el[maxn], er[maxn]; bool dfs(int u,int pre,int x)
{
//printf("u=%d pre=%d x=%d\n", u, pre, x);
int flag = ;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if (v == pre) continue;
if (lc[v] == && rc[v] == ) {
lc[v] = max(1ll * , lc[u] - x);
rc[v] = rc[u] + x;
}
else {
ll val = lc[v];
if ((val <= rc[u] && val >= lc[u]))
{
rc[u] = min(rc[u], val + x);
lc[u] = max(val - x, lc[u]);
}
else if (abs(val - lc[u]) <= x || abs(val - rc[u]) <= x)
{
if (val >= rc[u]) lc[u] = max(lc[u], val - x);
if (val <= lc[u]) rc[u] = min(rc[u], val + x);
if (lc[u] > rc[u]) return false;
}
else return false;
}
//printf("lc[%d]=%lld rc[%d]=%lld\n", v, lc[v], v, rc[v]);
if (dfs(v, u, x) == ) flag = ;
}
return flag;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, k;
memset(el, , sizeof(el));
memset(er, , sizeof(er));
scanf("%d%d", &n, &k);
for (int i = ; i <= n; i++) G[i].clear();
for (int i = ; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
int mark = ;
for (int i = ; i <= k; i++) {
int u, w;
scanf("%d%d", &u, &w);
mark = u;
el[u] = er[u] = w;
}
//printf("mark=%d\n", mark);
int l = , r = 1e9, ans = ;
while(l<=r)
{
memcpy(lc, el, sizeof(el));
memcpy(rc, er, sizeof(er));
int mid = (l + r) >> ;
if (dfs(mark,-,mid)) ans = mid, r = mid - ;
else l = mid + ;
//printf("l=%d r=%d mid=%d ans=%d\n\n", l, r, mid, ans);
}
printf("%d\n", ans);
}
}
/*
2
10 4
7 2
1 5
10 2
1 9
5 7
1 8
3 5
4 7
6 8
9 39
6 9
2 26
4 10
*/
T - zxa and leaf HDU - 5682 二分+dfs的更多相关文章
- 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 5682 zxa and leaf 二分 树形dp
zxa and leaf 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5682 Description zxa have an unrooted t ...
- hdu 5682 zxa and leaf
zxa and leaf Accepts: 25 Submissions: 249 Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 6 ...
- Codeforces Round #381 (Div. 2)D. Alyona and a tree(树+二分+dfs)
D. Alyona and a tree Problem Description: Alyona has a tree with n vertices. The root of the tree is ...
- hdu 4024 二分
转自:http://www.cnblogs.com/kuangbin/archive/2012/08/23/2653003.html 一种是直接根据公式计算的,另外一种是二分算出来的.两种方法速度 ...
- [BZOJ 1082] [SCOI2005] 栅栏 【二分 + DFS验证(有效剪枝)】
题目链接:BZOJ - 1082 题目分析 二分 + DFS验证. 二分到一个 mid ,验证能否选 mid 个根木棍,显然要选最小的 mid 根. 使用 DFS 验证,因为贪心地想一下,要尽量先用提 ...
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...
- 51nod1307(暴力树剖/二分&dfs/并查集)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 中文题诶~ 思路: 解法1:暴力树剖 用一个数 ...
- (hdu)5652 India and China Origins 二分+dfs
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5652 Problem Description A long time ago there ...
随机推荐
- 经常使用到的vim命令
常用的vim命令 总结了我经常用到的vim命令,虽然不是很全,但是日常的开发足够了 启动vim vim 直接启动vim vim filename 打开vim并创建名为filename的文件 插入命令 ...
- [原创] 在C++中实现打字机效果
如题. void pout(string str,int t)//随便取的,不要介意,str是待输出字符串,t是每两个字的间隔时间. { ;i<str.length();i++) { cout& ...
- 收集免费的接口服务,做一个api的搬运工
hello, 大家好,今天给大家推荐的开源项目在某种程度上极大的方便了广大的开发者,这个开源项目统计了网上诸多的免费API,为广大开发者收集免费的接口服务,专心致志做一个API的搬运工,每月定时更新新 ...
- Jenkins 批量创建任务的三种方法
最近,要搭建多套测试环境,需要把 Jenkins 中 dev 视图下的所有任务批量复制到 sit 等视图下. 说明 Jenkins 任务名称规则为:[测试环境标识]-[工程名称],如:dev-daod ...
- L8梯度消失、梯度爆炸
houseprices数据下载: 链接:https://pan.baidu.com/s/1-szkkAALzzJJmCLlJ1aXGQ 提取码:9n9k 梯度消失.梯度爆炸以及Kaggle房价预测 代 ...
- stand up meeting 12-3
因为前后端在参数传递定义不清晰的原因,今天士杰和国庆采用了pair programming的方法,在一台电脑前工作了四十分钟,明确了请求questionpool,请求question,请求rank d ...
- Daily Scrum 12/16/2015
Process: Dong&Minlong : 继续对Oxford Speech 接口进行调试,并且完成了相应工作的转接. Yandong@Zhaoyang: 完成了对一些Bug的修复工作,程 ...
- 谈谈MySQL的索引
目录 索引 前言 是什么 B树 B+树 B树和B+树结构上异同 有什么用 怎么用 索引 前言 总所周知,数据库查询是数据库的最主要功能之一.我们都希望查询数据的速度能尽可能的快.而支撑这一快速的背后就 ...
- python里的内置函数你知道有多少个吗?
Python 内置函数最全汇总: 1 abs() 绝对值或复数的模 2 all() 接受一个迭代器,如果迭代器的所有元素都为真,那么返回True,否则返回False 3 any() 接受一个迭代器,如 ...
- 【翻译】TensorFlow卷积神经网络识别CIFAR 10Convolutional Neural Network (CNN)| CIFAR 10 TensorFlow
原网址:https://data-flair.training/blogs/cnn-tensorflow-cifar-10/ by DataFlair Team · Published May 21, ...