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. coding 注意事项(总结中)

    Uber Go 语言代码风格指南可以参考下:https://www.cnblogs.com/ricklz/p/11670932.html 最近写代码,老是被吐槽,代码写的不好,细节处理的不好. 那么下 ...

  2. Dempster–Shafer theory(D-S证据理论)初探

    1. 证据理论的发展历程 Dempster在1967年的文献<多值映射导致的上下文概率>中提出上.下概率的概念,并在一系列关于上下概率的文献中进行了拓展和应用,其后又在文献<贝叶斯推 ...

  3. Java编程最差实践常见问题详细说明(1)转

    Java编程最差实践常见问题详细说明(1)转     原文地址:http://www.odi.ch/prog/design/newbies.php 每天在写Java程序, 其实里面有一些细节大家可能没 ...

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

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

  5. 一个spring 基本知识的微博(怎么加载多个xml、多个property文件、aop配置、监视器)

    http://blog.sina.com.cn/s/blog_61c5866d0100ev44.html

  6. 使用d3.js的时候,如何用zoom translate scale限制拖拽范围

    红色代表需要改写的代码 1.添加定义图像大小和容器的大小及坐标 d3.behavior.zoom = function () { var moveCanvas={ width: , height: , ...

  7. C#_关键字:Lock的解释和使用

    定义 lock关键字,互斥锁,通过锁住某一对象从而将语句块({})里面的代码设置为临界区. 线程在线性执行代码时若遇到互斥锁,必须先申请互斥锁的访问权,若访问成功,则继续线性访问互斥锁后的临界区代码块 ...

  8. ASE team work proposal

    Hi,我们是Azure Wrapper,欢迎来到我们的blog~我们将在这里记录下ASE课程的滴滴点点,美妙的旅程就要开始啦! 以下是每位队员提交的关于ASE 团队项目的提议: 朱玉影: 随着信息时代 ...

  9. CVE-2019-1388:Windows UAC 本地提权复现

    0x01 简介 用户帐户控制(User Account Control,简写作UAC)是微软公司在其Windows Vista及更高版本操作系统中采用的一种控制机制.其原理是通知用户是否对应用程序使用 ...

  10. 关于JavaEE中Spring模块的学习!

    七大模块,如下: 1. Spring Core: Core封装包是框架的最基础部分,提供IOC和依赖注入特性.这里的基础概念是BeanFactory,它提供对Factory模式的经典实现来消除对程序性 ...