http://www.lydsy.com/JudgeOnline/problem.php?id=2599

点分治

mi[i] 记录边权和为i时的最少边数

先更新答案,再更新mi数组,换根时清空mi

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib> using namespace std; #define N 200001
#define K 1000001 int m; int front[N],nxt[N<<],to[N<<],tot,val[N<<]; bool vis[N]; int f[N],siz[N]; int root,all;
int ans=N; int mi[K]; void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} void add(int u,int v,int w)
{
to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; val[tot]=w;
to[++tot]=u; nxt[tot]=front[v]; front[v]=tot; val[tot]=w;
} void getroot(int x,int y)
{
//printf("%d\n",x);
siz[x]=; f[x]=;
for(int i=front[x];i;i=nxt[i])
if(to[i]!=y && !vis[to[i]])
{
getroot(to[i],x);
siz[x]+=siz[to[i]];
f[x]=max(f[x],siz[to[i]]);
}
f[x]=max(f[x],all-siz[x]);
if(f[x]<f[root]) root=x;
} void dfs(int x,int y,int dis,int sum,int ty)
{
if(ty== && dis==m) ans=min(ans,sum);
if(dis>=m) return;
if(ty==)
{
if(mi[m-dis]) ans=min(ans,sum+mi[m-dis]);
}
else if(ty==) mi[dis]=mi[dis] ? min(mi[dis],sum) : sum;
else mi[dis]=;
for(int i=front[x];i;i=nxt[i])
if(to[i]!=y && !vis[to[i]]) dfs(to[i],x,dis+val[i],sum+,ty);
} void cal(int x)
{
for(int i=front[x];i;i=nxt[i])
if(!vis[to[i]])
{
dfs(to[i],x,val[i],,);
dfs(to[i],x,val[i],,);
}
for(int i=front[x];i;i=nxt[i])
if(!vis[to[i]]) dfs(to[i],x,val[i],,);
} void work(int x)
{
// printf("%d\n",x);
cal(x);
vis[x]=true;
for(int i=front[x];i;i=nxt[i])
if(!vis[to[i]])
{
all=siz[to[i]];
root=;
getroot(to[i],);
work(root);
}
} int main()
{
int size = << ;
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p));
freopen("ioi2011-race.in","r",stdin);
freopen("ioi2011-race.out","w",stdout);
int n;
read(n);
read(m);
int u,v,w;
for(int i=;i<n;++i)
{
read(u); u++;
read(v); v++;
read(w);
add(u,v,w);
}
all=n;
f[]=n;
getroot(,);
work(root);
printf("%d",ans==N ? - : ans);
}

bzoj千题计划160:bzoj2599: [IOI2011]Race的更多相关文章

  1. bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块

    http://www.lydsy.com/JudgeOnline/problem.php?id=4823 讨厌的形状就是四联通图 且左右各连一个方块 那么破坏所有满足条件的四联通就好了 按上图方式染色 ...

  2. bzoj千题计划196:bzoj4826: [Hnoi2017]影魔

    http://www.lydsy.com/JudgeOnline/problem.php?id=4826 吐槽一下bzoj这道题的排版是真丑... 我还是粘洛谷的题面吧... 提供p1的攻击力:i,j ...

  3. bzoj千题计划280:bzoj4592: [Shoi2015]脑洞治疗仪

    http://www.lydsy.com/JudgeOnline/problem.php?id=4592 注意操作1 先挖再补,就是补的范围可以包含挖的范围 SHOI2015 的题 略水啊(逃) #i ...

  4. bzoj千题计划177:bzoj1858: [Scoi2010]序列操作

    http://www.lydsy.com/JudgeOnline/problem.php?id=1858 2018 自己写的第1题,一遍过 ^_^ 元旦快乐 #include<cstdio> ...

  5. bzoj千题计划317:bzoj4650: [Noi2016]优秀的拆分(后缀数组+差分)

    https://www.lydsy.com/JudgeOnline/problem.php?id=4650 如果能够预处理出 suf[i] 以i结尾的形式为AA的子串个数 pre[i] 以i开头的形式 ...

  6. bzoj千题计划304:bzoj3676: [Apio2014]回文串(回文自动机)

    https://www.lydsy.com/JudgeOnline/problem.php?id=3676 回文自动机模板题 4年前的APIO如今竟沦为模板,,,╮(╯▽╰)╭,唉 #include& ...

  7. bzoj千题计划292:bzoj2244: [SDOI2011]拦截导弹

    http://www.lydsy.com/JudgeOnline/problem.php?id=2244 每枚导弹成功拦截的概率 = 包含它的最长上升子序列个数/最长上升子序列总个数 pre_len ...

  8. bzoj千题计划278:bzoj4590: [Shoi2015]自动刷题机

    http://www.lydsy.com/JudgeOnline/problem.php?id=4590 二分 这么道水题 没long long WA了两发,没判-1WA了一发,二分写错WA了一发 最 ...

  9. bzoj千题计划250:bzoj3670: [Noi2014]动物园

    http://www.lydsy.com/JudgeOnline/problem.php?id=3670 法一:KMP+st表 抽离nxt数组,构成一棵树 若nxt[i]=j,则i作为j的子节点 那么 ...

随机推荐

  1. 消息队列第二篇:MessageQueue实战(课程订单)

    上一篇:消息队列介绍 本篇一开始就上代码,主要演练MessageQueue的实际应用.用户提交订单(消息发送),系统将订单发送到订单队列(Order Queue)中:订单管理系统(消息接收)端,监听消 ...

  2. redis简介及增删改查

    redis 是一个文档(nosql)数据库,工作与内存,主要用做高速缓存 缓存经常会查到的数据 存入的值默认是字符串 使用步骤: 1 从redis.io下载 2 点击redis-server.exe启 ...

  3. JS计算两个日期之间的天数,时间差计算

    1.日期之间的天数计算 //计算天数差的函数,通用 function DateDiff(sDate1, sDate2) { //sDate1和sDate2是2017-9-25格式 var aDate, ...

  4. selenium异常问题汇总(持续更新版)

    webdriver启动firefox时如果遇到以下错误,则说明selenium的版本和firefox不兼容了,升级selenium版本就好 org.openqa.selenium.firefox.No ...

  5. PHP-时间函数

    1.时间格式化函数date(format,timestamp) format 时间格式 timestamp 时间戳 下面列出了一些常用于日期的字符: d - 表示月里的某天(01-31) m - 表示 ...

  6. 利用userData实现客户端保存表单数据

    对于多数网页制作的朋友,实现在客户端保存在网页表单上的信息,比较多的是采用Cookie技术来实现,这些功能例如:下拉列表框选择的选项,文本框输入的数据等.事实上,我们可以利用微软DHTML默认行为中的 ...

  7. hbase batch批处理

    hbase的put(List<Put> puts),delete(List<Delete> deletes),get(List<Get> gets)都是基于batc ...

  8. 【刷题】BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊

    Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...

  9. Spring点滴二:Spring Bean

    Spring Bean: 被称作bean的对象是构成应用程序的支柱,是由Spring Ioc容器管理.bean是一个被实例化,配置.组装并由Spring Ioc容器管理对象. 官网API:A Spri ...

  10. BZOJ 4408: [Fjoi 2016]神秘数

    4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 464  Solved: 281[Submit][Status ...