题意:求树上的一条费用不超过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 (树分治)的更多相关文章

  1. UVA 12161 Ironman Race in Treeland

    题目大意: 每一条边都有两个权值,val和路径长度d,要保证在val<=m 的条件下,求最长的d. 解题报告: 一开始想错了,后来发现还不如直接暴力点分,思想很套路.. 平时我们统计时,都会用合 ...

  2. hdu-5977 Garden of Eden(树分治)

    题目链接: Garden of Eden Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  3. 【BZOJ-1468】Tree 树分治

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 534[Submit][Status][Discuss] ...

  4. HDU 4812 D Tree 树分治+逆元处理

    D Tree Problem Description   There is a skyscraping tree standing on the playground of Nanjing Unive ...

  5. BZOJ 2152: 聪聪可可 树分治

    2152: 聪聪可可 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...

  6. POJ 1741 Tree 树分治

    Tree     Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...

  7. UVALive 7148 LRIP【树分治+线段树】

    题意就是要求一棵树上的最长不下降序列,同时不下降序列的最小值与最大值不超过D. 做法是树分治+线段树,假设树根是x,y是其当前需要处理的子树,对于子树y,需要处理出两个数组MN,MX,MN[i]表示以 ...

  8. BZOJ 2566 xmastree(树分治+multiset)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2566 题意:一棵有边权的树.结点有颜色.每次修改一个点的颜色.求每次修改后所有同色 ...

  9. 树分治&树链剖分相关题目讨论

    预备知识 树分治,树链剖分   poj1741 •一棵有n个节点的树,节点之间的边有长度.方方方想知道,有多少个点对距离不超过m 题解 点分治模板题.详见我早上写的http://www.cnblogs ...

随机推荐

  1. PHP连接 redis

    <?php //连接本地的 Redis 服务 $redis = new Redis(); //连接redis 地址 端口 连接超时时间 连接成功返回true 失败返回false $redis-& ...

  2. [Xcode 实际操作]四、常用控件-(4)UILabel文本标签的自动换行

    目录:[Swift]Xcode实际操作 本文将演示标签控件的换行功能, 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit class Vie ...

  3. 根据T-Code查看用户出口的代码

    在此非常非常感谢源作者,这段代码真的非常非常有用好用! REPORT  YLBTEST. TABLES :  tstc,     "SAP Transaction Codes(SAP 事务代 ...

  4. iOS客户端与网页交互文档

    很少和客户端打交道,这次由于做会活动,要和客户端配合做个分享的功能 这里总结下基本的流程,就是前端在H5 里调用客户端的方法即可 第一部分 客户端提供需求文档 网页请求设置 客户端发起请求时在HTTP ...

  5. LDAP相关操作注意事项

    lc.Modify(entry.DN, new LdapModification(LdapModification.REPLACE, new LdapAttribute("mDBUseDef ...

  6. Silverlight 创建 ImageButton

    这几天一直在折腾怎么在silverlight 按钮上添加图片,直接向imagebutton那样设置成属性可以直接更改,最后到处查找资料终于搞出一个imagebutton了. <Style x:K ...

  7. Shell变量赋值语句不能有空格

    a = 1是错的!!!!!只有 a=1才是正确的.

  8. arch安装问题总结

    安装 archLinux 的时候遇到的一些问题,记录下来方便以后安装. 1.fcitx 在设置/etc/locale.conf文件时,中文不能写成zh_CN.utf-8,而是要写成zh_CN.utf8 ...

  9. 【C#】=>符号的使用

    Lambda表达式.Lambda表达式是C#3.0的新内容,如果您之前学习的C#2.0,不认识也就不奇怪了.给您举个例子.例如,我定义一个委托:delegate int Method(int a, i ...

  10. IO缓冲流

    目录 IO缓冲流 缓冲流 基本原理 字节缓冲流 字符缓冲流 IO缓冲流 缓冲流也叫高效流,能够更高效的进行读取: 转换流:能够进行编码转换 序列化流:持久化存储对象 缓冲流 缓冲流--就是对应4个Fi ...