一、题目回顾

题目链接:zxa and leaf

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

If you need a larger stack size, please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++.

题意:一棵树n个点,其中有一些点已经有权值,现在给剩下的点安排权值,使得树中相邻两点的之差绝对值的最大值最小。

二、解题思路

  • 二分+树形dp(也可以二分+dfs)

思路:如果我们首先就想到了二分,那后面很好想了。。

直接二分答案,之后check中,我们随便取1个点为根节点,然后从下向上按拓扑序做树型dp。设SL[u]和SR[u]表示节点u能填的数字的范围

我们从下往上,然后只要判断是否有交集,即有解,我们就能知道当前答案是否可以使用了。

三、代码

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout<<"["<<x<<"]";
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII; const int MX = 1e5 + 5;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; struct Edge {
int nxt, v;
} E[MX];
int Head[MX], erear;
void edge_init() {
erear = 0;
memset(Head, -1, sizeof(Head));
}
void edge_add(int u, int v) {
E[erear].v = v;
E[erear].nxt = Head[u];
Head[u] = erear++;
} int is[MX], val[MX];
LL SL[MX], SR[MX]; bool DFS(int u, int f, int x) {
if(is[u]) SL[u] = SR[u] = val[u];
else SL[u] = -INF, SR[u] = INF;
for(int i = Head[u]; ~i; i = E[i].nxt) {
int v = E[i].v;
if(v == f) continue;
if(!DFS(v, u, x)) return false;
if(SL[v] != INF) SL[u] = max(SL[u], SL[v] - x);
if(SR[v] != INF) SR[u] = min(SR[u], SR[v] + x);
}
if(SL[u] > SR[u]) return false;
return true;
}
int solve() {
int l = 0, r = 1e9, m;
while(l <= r) {
m = (l + r) >> 1;
if(DFS(1, -1, m)) r = m - 1;
else l = m + 1;
}
return r + 1;
} int main() {
int T, n, k; //FIN;
scanf("%d", &T);
while(T--) {
edge_init();
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++) is[i] = 0;
for(int i = 1; i <= n - 1; i++) {
int u, v;
scanf("%d%d", &u, &v);
edge_add(u, v); edge_add(v, u);
}
for(int i = 1; i <= k; i++) {
int u, w;
scanf("%d%d", &u, &w);
is[u] = 1; val[u] = w;
}
printf("%d\n", solve());
}
return 0;
}

DFS——hdu5682zxa and leaf的更多相关文章

  1. hdu5758 思维,树形dp

    /*可以推测从叶子结点传送到叶子节点才能使传送次数最少,如果是偶数个叶子结点,那么传送leaf/2次就是答案,如果是奇数个叶子结点,则还有单独一条链需要覆盖dp[u]表示覆盖完u为根的子树需要走的边数 ...

  2. [CC-ADJLEAF2]Adjacent Leaves

    [CC-ADJLEAF2]Adjacent Leaves 题目大意: 给定一棵有根树,考虑从根开始进行DFS,将所有叶子按照被遍历到的顺序排列得到一个序列. 定义一个叶子集合合法,当且仅当存在一种DF ...

  3. Codeforces 1103 简要题解(持续更新)

    文章目录 A题 B题 C题 D题 传送门 又一场原地爆炸的比赛. A题 传送门 简单思维题 题意:给一个4∗44*44∗4的格子图和一个01串,你要根据01串放1∗21*21∗2的木块,如果是0就竖放 ...

  4. 清北刷题冲刺 11-03 p.m

    三向城 #include<iostream> #include<cstdio> using namespace std; int n,x,y; int main(){ freo ...

  5. 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第六章 2(Binary Trees)

    112 - Tree Summing 题目大意:给出一个数,再给一颗树,每个头节点的子树被包含在头节点之后的括号里,寻找是否有从头节点到叶子的和与给出的数相等,如果有则输出yes,没有输出no! 解题 ...

  6. [LeetCode] 129. Sum Root to Leaf Numbers_Medium tag: DFS

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  7. Codeforces1110F Nearest Leaf dfs + 线段树 + 询问离线

    Codeforces1110F dfs + 线段树 + 询问离线 F. Nearest Leaf Description: Let's define the Eulerian traversal of ...

  8. Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)

    Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...

  9. Leetcode之深度优先搜索(DFS)专题-1080. 根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths)

    Leetcode之深度优先搜索(DFS)专题-1080. 根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths) 这篇是DFS专题的第一篇,所以我会 ...

随机推荐

  1. idea开启自动编译

    springboot+thymeleaf+idea   idea默认是不自动编译的. 首先热部署的jar包肯定是要加进去的 2,快捷键 ctrl+shift+alt+/       点击registr ...

  2. mysql if...else 的使用

    select case when tca.id = '3' then 'vw' else epc_code end as epccode,tfp.product_id, tfp.vender, tfp ...

  3. vector基础操作

    //vector< T> vec; //构造一个名为vec的储存数据类型为T的动态数组.其中T为需要储存的数据类型 //初始时vec为空 //push_back 末尾添加一个元素 //po ...

  4. 云监控自定义HTTP状态码说明

    您在使用站点监控时,返回的6XX状态码均为云监控自定义HTTP状态码,具体含义如下表所示: 状态码      含义     备注  610  HTTP连接超时      监测点探测您的网站时出现连接超 ...

  5. java的动态验证码单线设计

    1.java的动态验证码我这里将介绍两种方法: 一:根据java本身提供的一种验证码的写法,这种呢只限于大家了解就可以了,因为java自带的模式编写的在实际开发中是没有意义的,所以只供学习一下就可以了 ...

  6. Maven命令参数

    命令参数 备注 mvn -v --version 显示版本信息; mvn -V --show-version 显示版本信息后继续执行Maven其他目标; mvn -h --help 显示帮助信息; m ...

  7. 【mysql学习笔记整理】

    /*mysql学习笔记整理*/ /*常用的数据库操作对象*/ #库的操作#创建#数据库的创建USE mysql;CREATE DATABASE db_x;#删除#删除数据库DROP DATABASE ...

  8. cors(Cross-origin resource sharing)跨域资源共享

    阮一峰老师的文章(http://www.ruanyifeng.com/blog/2016/04/cors.html)跨域资源共享详解和https://developer.mozilla.org/zh- ...

  9. 【tp5.1】七牛云上传图片

    composer安装: composer require qiniu/php-sdk 配置使用: 在tp5.1的配置文件app.php中配置七牛云的参数 'qiniu' => [ 'access ...

  10. Scrapy框架的初步使用

    Scrapy scrapy框架是一个非常全面的爬虫框架,可以说是爬虫界的django了,里面有相当多的组件,格式化组件item,持久化组件pipeline,爬虫组件spider 首先我们要先和djan ...