T - zxa and leaf

HDU - 5682

题目大意是:给你一颗树,这棵树有些节点已经设置了它的美丽值,然后剩下一些节点需要我们设置美丽值。

一条边的丑陋程度等于被定义为由这个边缘连接的两个节点之间的美丽水平的绝对差异

一棵树的丑陋程度则是边的丑陋程度的最大值。

这个题目应该是二分这个丑陋值。

然后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的更多相关文章

  1. 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 ...

  2. HDU 5682 zxa and leaf 二分 树形dp

    zxa and leaf 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5682 Description zxa have an unrooted t ...

  3. hdu 5682 zxa and leaf

    zxa and leaf  Accepts: 25  Submissions: 249  Time Limit: 5000/2500 MS (Java/Others)  Memory Limit: 6 ...

  4. 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 ...

  5. hdu 4024 二分

    转自:http://www.cnblogs.com/kuangbin/archive/2012/08/23/2653003.html   一种是直接根据公式计算的,另外一种是二分算出来的.两种方法速度 ...

  6. [BZOJ 1082] [SCOI2005] 栅栏 【二分 + DFS验证(有效剪枝)】

    题目链接:BZOJ - 1082 题目分析 二分 + DFS验证. 二分到一个 mid ,验证能否选 mid 个根木棍,显然要选最小的 mid 根. 使用 DFS 验证,因为贪心地想一下,要尽量先用提 ...

  7. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  8. 51nod1307(暴力树剖/二分&dfs/并查集)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 中文题诶~ 思路: 解法1:暴力树剖 用一个数 ...

  9. (hdu)5652 India and China Origins 二分+dfs

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5652 Problem Description A long time ago there ...

随机推荐

  1. MyBatis通用 Mapper4使用小结

    官网地址: http://www.mybatis.tk/ https://gitee.com/free 1.使用springboot,添加依赖: 使用tk的mybatis后不需要引用官方原生的myba ...

  2. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(二)之Introduction to Objects

    The genesis of the computer revolution was a machine. The genesis of out programming languages thus ...

  3. stand up meeting 1/8/2016 & weekend 1/9/2016~1/10/2016 && sprint2扫尾

    part 组员                工作              工作耗时/h 明日计划 工作耗时/h    UI 冯晓云 跑通打印机功能,尝试与pdf读取部分结合     6 查漏补缺, ...

  4. Performance standard (ALPHA release) 12/17/2015

    ===================ALPHA RELEASE STANDARD====================== 1. Parallel performance test: The Nu ...

  5. Ubuntu安装Elasticsearch6.3

    本文使用的 Ubuntu 版本信息: Distributor ID: Ubuntu Description: Ubuntu LTS Release: 16.04 Codename: xenial 1. ...

  6. .NET Core 发布时去掉多余的语言包文件夹

    用 .NET Core 3.x 作为目标框架时发布完之后,会发现多了很多语言包文件夹,类似于: 有时候,不想要生成这些语言包文件夹,需要稍微配置一下. 在 PropertyGroup 节点中添加如下的 ...

  7. 2. Git-命令行-删除本地和远程分支

    命令行方式 Git Bash: 切换到要操作的项目文件夹 命令行 : $ cd <ProjectPath> 查看项目的分支们(包括本地和远程) 命令行 : $ git branch -a ...

  8. api接口安全

    API接口安全 在做app开发中,如何保证api的接口安全,不被其他app去调用? 接口安全的措施很多,今天记录一个常用的措施 签名: 前台想要调用接口,需要使用几个参数生成签名: 时间戳:当前时间 ...

  9. mysql 使用记录

    修改 mysql 数据库密码 mysqladmin -u username -h host_name password -P <port> "new_password" ...

  10. 一行配置美化 nginx 目录 autoindex.html

    demo