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. Alpha版本冲刺(八)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  2. ssh结合使用

    springxml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=& ...

  3. /etc/tolmcat/Server.xml 实例说明

      # 这是service类 <Service name="Catalina">   # 这是http连接器,响应用户请求 <Connector port=&qu ...

  4. Sprint 3.0

    3.0----------------------------------------------------- SCRUM 流程的步骤2: Spring 计划 1. 确保product backlo ...

  5. [二十六]SpringBoot 之 整合log4j

    1.引入log4j依赖 在创建Spring Boot工程时,我们引入了spring-boot-starter,其中包含了spring-boot-starter-logging,该依赖内容就是Sprin ...

  6. poj 2185

    http://poj.org/problem?id=2185 题意:求最小的模式块,使其无限扩展后包含给你的矩阵块(看别人题解才懂的题意): 分析:假设存在一个模式块可以满足上述条件,那么必然存在一个 ...

  7. python selenium wait方法

    遇到一个网站运行很慢,所以要等待某个元素显示出来之后再进行操作,自己手上的书上没有例子可以直接用 发现一篇文章:http://www.cnblogs.com/yoyoketang/p/6517477. ...

  8. MT【152】不患寡而患不均

    ((清华2017.4.29标准学术能力测试1) $a_1,a_2,\cdots,a_9$ 是数字$1$到$9$ 的一个排列,则 $a_1a_2a_3+a_4a_5a_6+a_7a_8a_9$ 的最小值 ...

  9. AtCoder Grand Contest 005

    AtCoder Grand Contest 005 A - STring 翻译 给定一个只包含\(ST\)的字符串,如果出现了连续的\(ST\),就把他删去,然后所有位置前移.问最后剩下的串长. 题解 ...

  10. Linux中的防火墙----iptables

    防火墙,它是一种位于内部网络与外部网络之间的网络安全系统.一项信息安全的防护系统,依照特定的规则,允许或是限制传输的数据通过. 防火墙根据主要的功能可分为网络层防火墙.应用层防火墙.数据库防火墙. 网 ...