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. C++自学随笔(2)

    引用 就像人的别名,人不能只有别名,变量也不能只有引用. 指针类型的引用:*&指针引用名 = 指针. 如int a = 10;int *p =&a;int *&q =p1 co ...

  2. POJ 3597 Polygon Division 多边形剖分

    题目链接: http://poj.org/problem?id=3597 Polygon Division Time Limit: 2000MSMemory Limit: 131072K 问题描述 G ...

  3. 团队作业之404 Note Found Team

    如果记忆是一个罐头的话,我希望这一罐罐头不会过期----<重庆森林> 404 Note Found Team 如果记忆是一个备忘录的话,别说了,它不会过期----<404 Note ...

  4. java实现hash一致性算法

    import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.StringUtils; import jav ...

  5. Windows平台下在服务中添加MySQL

    widows下查看服务 1.桌面计算机-->右键-->管理-->计算机管理(本地)--->服务和应用程序-->服务 2.运行 中输入 services.msc 在服务中添 ...

  6. mysql中LIKE和REGEXP

    mysql中LIKE和REGEXP都可以用来字符匹配 正则表达式REGEXP是为复杂搜索指定模式的强大方式. like用法   LIKE一般与通配符(%)和(_)两个使用 如例 SELECT prod ...

  7. laravel4 「时间戳」问题

    默认 Eloquent 会自动维护数据库表的 created_at 和 updated_at 字段.只要把这两个「时间戳」字段加到数据库表, Eloquent 就会处理剩下的工作.如果不想让 Eloq ...

  8. http请求头、响应头文件详解

    HTTP Request Header 请求头   解释 示例 Accept 指定客户端能够接收的内容类型 Accept: text/plain, text/html Accept-Charset 浏 ...

  9. Mininet 系列实验(一)

    关于SDN的第一个实验,似乎实验室里的前辈们也都是从这里开始的. 实验内容 使用源码安装Mininet 参考 Mininet使用源码安装 实验环境 虚拟机:Oracle VM VirtualBox U ...

  10. 【Learning】积性函数前缀和——洲阁筛(min_25写法)

    问题描述 洲阁筛解决的问题主要是\(n\)范围较大的积性函数前缀和. ​ 已知一积性函数\(f(i)\),求\(\sum_{i=1}^nf(i)\). \(n\leq10^{12}\). 求解方法 如 ...