一、题目回顾

题目链接: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. LeetCode4.寻找两个有序数组的中位数 JavaScript

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2  ...

  2. Unity 游戏框架搭建 (十四) 优雅的QSignleton(零) QuickStart

      好久不见 !之前想着让各位直接用QFramework,但是后来想想,如果正在进行的项目直接使用QFramework,这样风险太高了,要改的代码太多,所以打算陆续独立出来一些工具和模块,允许各位一个 ...

  3. 【oracle使用笔记3】sql查询遇到的若干问题总结

    在整个开发过程中,sql查询操作的频率比较高,在不同的业务场景下会出现不同的查询需求,以下是我在项目中遇到的查询需求,总结一下. [查询一]:取查询出的第一条数据 select * from (sel ...

  4. 微信小程序学习笔记(一)

    1.目录及文件构成 1.1 根目录下 ** app.js 是小程序的脚本代码,用来监听并处理小程序的生命周期函数.声明全局变量. ** app.json 是对整个小程序的全局配置,配置小程序是由哪些页 ...

  5. 2018/7/19 考试(tower,work,holes)

    noip模拟赛,挺良心的题,考的贼烂(膜一下@来日方长大佬(sdfz rank1)) 不多说了,看题吧 1.tower 题面: 铁塔(tower.pas/c/cpp) 题目描述 Rainbow和Fre ...

  6. 最长公共子序列Lcs (51Nod - 1006)

    20180604   11:28   给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的).   比如两个串为:   abcicba abdkscab   ab是两个串的子序列,ab ...

  7. ABAP术语-Function Group

    Function Group 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/13/1067699.html Group of logical ...

  8. ABAP术语-Company Code

    Company Code 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/16/1040816.html The smallest organ ...

  9. 关于ECharts内存泄漏问题

    最近使用websocket加ECharts做了一个实时监控的功能,发现了一个比较严重的问题,就是浏览器运行一段时间就会非常卡,之前在ECharts官网运行官方实例“动态数据 + 时间坐标轴”时,也遇到 ...

  10. sbt打包error(sbt.librarymanagement.ResolveException: unresolved dependency: org.apache.spark#spark-streaming;2.3.1: not found)

    解决方法: 修改simple.sbt文件: cd /usr/local/spark/myapp/TestStream vim simple.sbt 切记:中间相连部分两个百分号一定要写上