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. 【搜索】POJ-3050 基础DFS

    一.题目 Description The cows play the child's game of hopscotch in a non-traditional way. Instead of a ...

  2. jquery前端第一讲

    1.bootstrap里面的文件是什么意思: bootstrap.cssbootstrap.min.cssbootstrap-responsive.cssbootstrap-responsive.mi ...

  3. JAVA之路(二)

    学道酬勤,这是第二次学习JAVA,感觉如醍醐灌顶一样,理解很多思想和道理. 本博只是自己对JAVA的一些理解,具体定义以及用法百科里有. 我为什么在博客园内记录自己的学习过程呢,因为我想有人知道我在学 ...

  4. Pytorch相关内容

    ---恢复内容开始--- Pytorch中文官方文档:https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch-nn P ...

  5. 关闭Centos5.5的写磁盘I/O功能

    一个Linux文件默认有3个时间. atime:对此文件的访问时间. ctime:此文件inode发生变化的时间. mtime:此文件的修改时间. 如果有多个小文件(比如Web服务器的页面上有多个小图 ...

  6. HDU 2123 An easy problem

    http://acm.hdu.edu.cn/showproblem.php?pid=2123 Problem Description In this problem you need to make ...

  7. check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 7

    第一种是:解决MySQL的版本问题(我用的是mysql 5.5版本),mysql 5.0版本以后的数据库方言是:org.hibernate.dialect.MySQ5LInnoDBDialect.第二 ...

  8. <context-param> 标签引出的 web.xml 文件的加载顺序 [转]

    代码示例 : <context-param> <param-name>contextConfigLocation</param-name> <param-va ...

  9. Java override 和 overload 的区别和联系

    方法的重写(Overriding)和重载(Overloading)是Java多态性的不同表现.重写(Overriding)是父类与子类之间多态性的一种表现,而重载(Overloading)是一个类中多 ...

  10. ajax极简教程

    一.什么是ajax ajax即异步JavaScript和XML,它是一种用于创建快速动态网页的技术.作用是通过在后台与服务器进行少量数据交换,使网页实现异步更新.这意味着可以在不重新加载整个网页的情况 ...