UVA 12161 Ironman Race in Treeland (树分治)
题意:求树上的一条费用不超过m的路径,使得总长度尽量大。
人参第一发树分治,紫书上思路讲得比较清晰,这里不再赘述。
实现的时候,用一个类似时间戟的东西,记录结点首次访问的时间,并保存结点序列。
合并的时候用map组织有序表。和Defense Lines类似
复杂度O(nlog^2n)
#include<bits/stdc++.h>
using namespace std; const int maxn = 3e4+;
int n,m;
int hd[maxn], nx[maxn<<], to[maxn<<], dam[maxn<<], len[maxn<<], ec; inline void add(int u,int v,int D,int L)
{
to[ec] = v;
dam[ec] = D;
len[ec] = L;
nx[ec] = hd[u];
hd[u] = ec++;
} int root, best;
int ct[maxn];
//best = n
void GetBaryCenter(int u,int f)
{
ct[u] = ;
int Mx = ;
for(int i = hd[u]; ~i; i = nx[i]){
if(to[i] != f){
GetBaryCenter(to[i], u);
ct[u] += ct[to[i]];
Mx = max(Mx,ct[to[i]]);
}
}
Mx = max(Mx,n-ct[u]);
if(Mx < best){
best = Mx;
root = u;
}
} #define MP make_pair
#define fi first
#define se second
int C[maxn], L[maxn]; map<int,int> mp;
map<int,int>::iterator it; int ans;
int path[maxn];
int pre[maxn], dfs_clk;
//dfs_clk = 0, ans = 0;
void dfs(int u = root,int f = )
{
path[dfs_clk] = u;
pre[u] = dfs_clk++;
C[u] = ; L[u] = ;
for(int i = hd[u]; ~i; i = nx[i]){
if(to[i] != f){
int v = to[i];
dfs(v,u);
for(int j = pre[v]; j < dfs_clk ; j++){
int x = path[j];
C[x] += dam[i];
L[x] += len[i];
}
}
}
//子树都访问完里以后统一处理以保证互不干扰,这样mp就可以设置为全局变量
mp.clear();
for(int i = hd[u]; ~i; i = nx[i]){
int nex = nx[i];
int lim = ~nex? pre[to[nex]] : dfs_clk;
if(to[i] != f){
int v = to[i];
for(int j = pre[v]; j < lim ; j++){
int x = path[j];
if(C[x] <= m){
ans = max(ans,L[x]);
it = mp.upper_bound(m-C[x]);//找到一个key <= C[x]
if(it != mp.begin()){
ans = max(ans,(--it)->second+L[x]);
}
}
}
for(int j = pre[v]; j < lim ; j++){
int x = path[j];
if(C[x] <= m){
it = mp.lower_bound(C[x]);
bool swc = false;
if(it == mp.begin() || (swc = true , L[x] > (--it)->se) ){
if(swc) it++;
while(it != mp.end() && it->se <= L[x]) mp.erase(it++);
if(it == mp.end() || it->fi > C[x]) {//lower_bound可能使得it指向key == C[x] 而 val >= L[x]
mp.insert(MP(C[x],L[x]));
}
}
}
}
}
}
} //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
int T, ks = ; cin>>T;
while(T--){
scanf("%d%d",&n,&m);
memset(hd+,-,sizeof(int)*n); ec = ;
for(int i = n; --i;){
int a,b,D,L; scanf("%d%d%d%d",&a,&b,&D,&L);
add(a,b,D,L); add(b,a,D,L);
}
best = n+;
GetBaryCenter(,);
dfs_clk = ans = ;
dfs();
printf("Case %d: %d\n",++ks,ans);
}
return ;
}
UVA 12161 Ironman Race in Treeland (树分治)的更多相关文章
- UVA 12161 Ironman Race in Treeland
题目大意: 每一条边都有两个权值,val和路径长度d,要保证在val<=m 的条件下,求最长的d. 解题报告: 一开始想错了,后来发现还不如直接暴力点分,思想很套路.. 平时我们统计时,都会用合 ...
- hdu-5977 Garden of Eden(树分治)
题目链接: Garden of Eden Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- 【BZOJ-1468】Tree 树分治
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1025 Solved: 534[Submit][Status][Discuss] ...
- HDU 4812 D Tree 树分治+逆元处理
D Tree Problem Description There is a skyscraping tree standing on the playground of Nanjing Unive ...
- BZOJ 2152: 聪聪可可 树分治
2152: 聪聪可可 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- UVALive 7148 LRIP【树分治+线段树】
题意就是要求一棵树上的最长不下降序列,同时不下降序列的最小值与最大值不超过D. 做法是树分治+线段树,假设树根是x,y是其当前需要处理的子树,对于子树y,需要处理出两个数组MN,MX,MN[i]表示以 ...
- BZOJ 2566 xmastree(树分治+multiset)
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2566 题意:一棵有边权的树.结点有颜色.每次修改一个点的颜色.求每次修改后所有同色 ...
- 树分治&树链剖分相关题目讨论
预备知识 树分治,树链剖分 poj1741 •一棵有n个节点的树,节点之间的边有长度.方方方想知道,有多少个点对距离不超过m 题解 点分治模板题.详见我早上写的http://www.cnblogs ...
随机推荐
- (linux)安装redis---简装
redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcached类似,但很大程度补偿了memcached的不足,它支持存储的value类型相对更多,包括strin ...
- Spark Task 概述
Task的执行流程: 1. Driver端中的 CoarseGrainSchedulerBackend 给 CoarseGrainExecutorBacken 发送 LaunchTask 消息 2. ...
- 使用poi导出Excel表格,jar包冲突
HTTP Status 500 – Internal Server Error Type Exception Report Message Handler processing failed; nes ...
- JSP-用网页输出乘法表 三角形及菱形
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- little w and Soda(思维题)
链接:https://ac.nowcoder.com/acm/contest/297/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...
- Knight Tournament (set)
Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the me ...
- mycat1.6.5分片(字符串拆分hash)
https://blog.csdn.net/webnum/article/details/78313525 分片规则:字符串拆分hash 一.conf/schema.xml文件 <?xm ...
- leetcode 91. 解码方法
题目描述: 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数 ...
- Unity (反向动力学)IK动画
- EasyTouch3.16 初步使用