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 ...
随机推荐
- SSM框架的集成
一 : SpringMVC 接收参数 基本数据类型+String JavaBean 一对一 一对多 Map List 注解 @RequestMapping("/user/add") ...
- DNS解析工具--nslookup和dig使用
1.nslookup使用 [root@master ~]# nslookup> server 8.8.8.8 #指定域名服务器Default server: 8.8.8.8Address: ...
- 「模拟赛20180306」回忆树 memory LCA+KMP+AC自动机+树状数组
题目描述 回忆树是一棵树,树边上有小写字母. 一次回忆是这样的:你想起过往,触及心底--唔,不对,我们要说题目. 这题中我们认为回忆是这样的:给定 \(2\) 个点 \(u,v\) (\(u\) 可能 ...
- linux下python3的安装(已安装python2的情况下)
前段时间想自学一下python,就在虚拟机里已安装python2.7的情况下又安装了最新版python3.6.4.于是问题来了..只要一打开终端就出现一大段错误代码(忘记截图了),当时看到是ros和p ...
- C++ | char* 在类中实践笔记
在C++中,当类中定义有char * 变量时,在传参,构造函数,复制构造函数如何创建及赋值,来一个简单的例子就明了: #include<iostream> #include<stri ...
- STP-18-Port-Channl上的负载均衡
Ether Channel通过在多条链路上传输多个数据帧,增加了可用带宽.一个以太网帧总是通过一个Ether Channel中的一条链路传输.针对数据帧地址字段执行散列计算能够产生一个编号,标识这个数 ...
- 解决spark-shell输出日志信息过多
import org.apache.log4j.Logger import org.apache.log4j.Level Logger.getLogger("org").setLe ...
- Android Studio、adb
Android Studio:一个超级全的教程,mark~ https://www.cnblogs.com/laughingQing/p/5848425.html adb命令: https://blo ...
- SPA 介绍
SQL 性能分析器(SPA)工具概览 作为 Oracle Real Application Testing 选件/特性,这篇文章将提供一个关于 SQL 性能分析器(SPA)工具的简要概览.这是此系列的 ...
- ubuntu搭建mediawiki
1.搭建lamp环境,lamp指的是: Linux+Apache+Mysql/MariaDB+Perl/PHP/Python (我们安装的是Linux+apache2+postgresql+php) ...