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. Linux C++ 网络编程学习系列(7)——mbedtls编译使用

    mbedtls编译使用 环境: Ubuntu18.04 编译器:gcc或clang 编译选项: 静态编译使用 1. mbedtls源码 下载地址: https://github.com/ARMmbed ...

  2. 使用StopWatch类来计时 (perf4j-0.9.16.jar 包里的类)

    public class StopWatch { static public int AN_HOUR = 60 * 60 * 1000; static public int A_MINUTE = 60 ...

  3. CentOS7安装JAVA环境

    安装JAVA环境我常用的有两种形式 1.下载tar包安装 2.下载rpm包直接安装 本篇内容就写这两种形式的安装方法: JAVA程序的下载地址:https://www.oracle.com/java/ ...

  4. Hadoop学习笔记(1)-Hadoop在Ubuntu的安装和使用

    由于小编在本学期有一门课程需要学习hadoop,需要在ubuntu的linux系统下搭建Hadoop环境,在这个过程中遇到一些问题,写下这篇博客来记录这个过程,并把分享给大家. Hadoop的安装方式 ...

  5. R - Cow and Message CodeForces - 1307C

    思路对了,但是不会写. 等差数列长度不是1就是2,所以不是一个字母就是俩字母,一开始写的时候直接枚举两个字母,然后让次数相乘.这样是不对的,比如abaabb,字母ab的个数应该是3+2+2,因该是每一 ...

  6. Pie 杭电1969 二分

    My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N ...

  7. [HTML] <base>链接默认打开方式标签元素

    HTML 超链接(锚文本)默认打开方式与默认链接URL地址标签元素 一.语法与结构 <base target="_blank" href="http://www.l ...

  8. golang实现并发爬虫一(单任务版本爬虫功能)

    目的是写一个golang并发爬虫版本的演化过程. 那么在演化之前,当然是先跑通一下单任务版本的架构. 正如人走路之前是一定要学会爬走一般. 首先看一下单任务版本的爬虫架构,如下: 这是单任务版本爬虫的 ...

  9. OAuth-授权机制

    一.应用场景 有一个"云冲印"的网站,可以将用户储存在Google的照片,冲印出来.用户为了使用该服务,必须让"云冲印"读取自己储存在Google上的照片. 问 ...

  10. 替换字符串sql

    update [表名] set 字段名 = replace(与前面一样的字段名,'原本内容','想要替换成什么') UPDATE `zjl_III_hei_zlj_20151111`.`ctrl_ne ...