DFS——hdu5682zxa and leaf
一、题目回顾
题目链接:zxa and leaf

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的更多相关文章
- hdu5758 思维,树形dp
/*可以推测从叶子结点传送到叶子节点才能使传送次数最少,如果是偶数个叶子结点,那么传送leaf/2次就是答案,如果是奇数个叶子结点,则还有单独一条链需要覆盖dp[u]表示覆盖完u为根的子树需要走的边数 ...
- [CC-ADJLEAF2]Adjacent Leaves
[CC-ADJLEAF2]Adjacent Leaves 题目大意: 给定一棵有根树,考虑从根开始进行DFS,将所有叶子按照被遍历到的顺序排列得到一个序列. 定义一个叶子集合合法,当且仅当存在一种DF ...
- Codeforces 1103 简要题解(持续更新)
文章目录 A题 B题 C题 D题 传送门 又一场原地爆炸的比赛. A题 传送门 简单思维题 题意:给一个4∗44*44∗4的格子图和一个01串,你要根据01串放1∗21*21∗2的木块,如果是0就竖放 ...
- 清北刷题冲刺 11-03 p.m
三向城 #include<iostream> #include<cstdio> using namespace std; int n,x,y; int main(){ freo ...
- 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第六章 2(Binary Trees)
112 - Tree Summing 题目大意:给出一个数,再给一颗树,每个头节点的子树被包含在头节点之后的括号里,寻找是否有从头节点到叶子的和与给出的数相等,如果有则输出yes,没有输出no! 解题 ...
- [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 ...
- Codeforces1110F Nearest Leaf dfs + 线段树 + 询问离线
Codeforces1110F dfs + 线段树 + 询问离线 F. Nearest Leaf Description: Let's define the Eulerian traversal of ...
- Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)
Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...
- Leetcode之深度优先搜索(DFS)专题-1080. 根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths)
Leetcode之深度优先搜索(DFS)专题-1080. 根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths) 这篇是DFS专题的第一篇,所以我会 ...
随机推荐
- 使用actionerror做失败登录验证
一. 登录页面中放置如下代码: <h4>员工登录</h4> <div style="color:red"> <s:actionerror/ ...
- fjutacm 2492 宠物收养所 : Splay 模板 O(nlogn)
/** problem: http://www.fjutacm.com/Problem.jsp?pid=2492 Splay blog: https://tiger0132.blog.luogu.or ...
- 查看系统PCI设备
# lspci Host bridge:主板 VGA compatible controller:VGA显卡设备 Class 0403:声卡设备 USB Controller:USB接口设备 SATA ...
- ABAP术语-EDI (Electronic Data Interchange)
EDI (Electronic Data Interchange) 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/29/1057386.ht ...
- 【MYSQL笔记3】MYSQL过程式数据库对象之存储过程的调用、删除和修改
mysql从5.0版本开始支持存储过程.存储函数.触发器和事件功能的实现. 我们以一本书中的例题为例:创建xscj数据库的存储过程,判断两个输入的参数哪个更大.并调用该存储过程. (1)调用 首先,创 ...
- Linux 学习第二天
一.常用命令的使用 1.echo echo https://www.cnblogs.com/rise-home/ 输出字符串 2.ps -aux(显示进程信息) ps 进程状态共有5种 A.STAT ...
- Document .load与Document .ready的区别
页面加载完成有两种事件 1.load是当页面所有资源全部加载完成后(包括DOM文档树,css文件,js文件,图片资源等),执行一个函数 问题:如果图片资源较多,加载时间较长,onload后等待执行的函 ...
- 利用phpspreadsheet切割excel大文件
背景: 利用phpspreadsheet可以轻松的解析excel文件,但是phpspreadsheet的内存消耗也是比较大的,我试过解析将近5M的纯文字excel内存使用量就会超过php默认的最大内存 ...
- python七类之列表元组
列表 一.关键字: list lst = [ , , , , , , ,] lst = [1,2,3,4] 二.方法: 1.增加: . append( ) #追加,添加元素进列表最后 ls ...
- Java学习笔记八:Java的流程控制语句之循环语句
Java的流程控制语句之循环语句 一:Java循环语句之while: 生活中,有些时候为了完成任务,需要重复的进行某些动作.如参加 10000 米长跑,需要绕 400 米的赛道反复的跑 25 圈.在 ...