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. *** error 65: access violation at C:0x001B : no 'execute/read' permission

    转自:http://blog.csdn.net/chenqiai0/article/details/7827071 很多人在进行串口调试的时候会遇到这个问题,请大家略看我的代码,解决方法在其中 //实 ...

  2. PROFIBUS-DP

    PROFIBUS – DP的DP即Decentralized Periphery.它具有高速低成本,用于设备级控制系统与分散式I/O的通信.它与PROFIBUS-PA(Process Automati ...

  3. JPEG图像压缩算法流程详解

    JPEG图像压缩算法流程详解 JPEG代表Joint Photographic Experts Group(联合图像专家小组).此团队创立于1986年,1992年发布了JPEG的标准而在1994年获得 ...

  4. 服务 在初始化安装时发生异常:System.IO.FileNotFoundException: 未能加载文件或******

    这个问题是在用installutil.exe安装服务时候碰到的 解决方法就是把installutil.exe文件直接复制到要安装的目录下 installutil.exe的所在位置 windows/mi ...

  5. NULL,"",String.Empty三者在C#中的区别

    (1)NULLnull 关键字是表示不引用任何对象的空引用的文字值.null 是引用类型变量的默认值.那么也只有引用型的变量可以为NULL,如果int i=null,的话,是不可以的,因为Int是值类 ...

  6. 初入码田--ASP.NET MVC4 Web应用开发之二 实现简单的增删改查

    初入码田--ASP.NET MVC4 Web应用之创建一个空白的MVC应用程序 初入码田--ASP.NET MVC4 Web应用开发之一 实现简单的登录 2016-07-29  一.创建M002Adm ...

  7. Finding LCM LightOJ - 1215 (水题)

    这题和这题一样......只不过多了个数... Finding LCM LightOJ - 1215 https://www.cnblogs.com/WTSRUVF/p/9316412.html #i ...

  8. Rabbit and Grass HDU - 1849 (Bash+Nim)

    就是Bash 和 Nim 博弈的结合  可以直接 res ^= (Li + 1) % Mi 也可以 sg打个表  我打了个表 #include <iostream> #include &l ...

  9. 【比赛】NOIP2017 逛公园

    考试的时候灵光一闪,瞬间推出DP方程,但是不知道怎么判-1,然后?然后就炸了. 后来发现,我只要把拓扑和DP分开,中间加一个判断,就AC了,可惜. 看这道题,我们首先来想有哪些情况是-1:只要有零环在 ...

  10. div + css 样式连接

    外部文件连接:<link rel ="stylesheet" type=""text/css" href="demo.css" ...