[BZOJ2599]Race
Description
给一棵树,每条边有权.求一条简单路径,权值和等于K,且边的数量最小.N <= 200000, K <= 1000000
Input
第一行 两个整数 n, k
第二..n行 每行三个整数 表示一条无向边的两端和权值 (注意点的编号从0开始)
Output
一个整数 表示最小边数量 如果不存在这样的路径 输出-1
Sample Input
0 1 1
1 2 2
1 3 4
Sample Output
HINT
2018.1.3新加数据一组,未重测
Source
很简单的一道点分治,注意一下维护的顺序,先更新答案再更新桶数组
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#define M 200010
using namespace std;
int n,m,num,rt,ans,k,S;
int maxn[M],head[M],dis[M],d[M],size[M],t[M<<];
bool vis[M];
struct point{int to,next,dis;}e[M<<];
void add(int from,int to,int dis) {
e[++num].next=head[from];
e[num].to=to;
e[num].dis=dis;
head[from]=num;
}
void getroot(int x,int fa) {
size[x]=;maxn[x]=;
for(int i=head[x];i;i=e[i].next) {
int to=e[i].to;
if(to==fa||vis[to]) continue;
getroot(to,x),size[x]+=size[to];
maxn[x]=max(maxn[x],size[to]);
}
maxn[x]=max(maxn[x],S-size[x]);
if(maxn[x]<maxn[rt]) rt=x;
}
void cal(int x,int fa) {
if(dis[x]>k) return;
ans=min(ans,d[x]+t[k-dis[x]]);
for(int i=head[x];i;i=e[i].next) {
int to=e[i].to;
if(vis[to]||to==fa) continue;
d[to]=d[x]+,dis[to]=dis[x]+e[i].dis;
cal(to,x);
}
}
void insert(int x,int fa) {
if(dis[x]<=k) {
t[dis[x]]=min(t[dis[x]],d[x]);
for(int i=head[x];i;i=e[i].next)
if(!vis[e[i].to]&&e[i].to!=fa)
insert(e[i].to,x);
}
}
void del(int x,int fa) {
if(dis[x]<=k) {
t[dis[x]]=1e9;
for(int i=head[x];i;i=e[i].next)
if(!vis[e[i].to]&&e[i].to!=fa)
del(e[i].to,x);
}
}
void solve(int x) {
vis[x]=true;t[]=;
for(int i=head[x];i;i=e[i].next) {
int to=e[i].to;
if(vis[to]) continue;
d[to]=,dis[to]=e[i].dis,cal(to,);
insert(to,x);
}
for(int i=head[x];i;i=e[i].next)
if(!vis[e[i].to])
del(e[i].to,x);
for(int i=head[x];i;i=e[i].next) {
int to=e[i].to;
if(vis[to]) continue;
rt=,S=size[to],getroot(to,);
solve(rt);
}
}
int main() {
scanf("%d%d",&n,&k);ans=n+;
memset(t,,sizeof(t));
for(int i=;i<n;i++) {
int a,b,c;scanf("%d%d%d",&a,&b,&c);
a++,b++;
add(a,b,c),add(b,a,c);
}
S=maxn[]=n;getroot(,);
solve(rt);
if(ans>n) puts("-1");
else printf("%d\n",ans);
return ;
}
[BZOJ2599]Race的更多相关文章
- [BZOJ2599][Race][IOI2011]点分治
这是为了真正去学一下点分治..然后看了迪克李的ppt 又是一道写(改)了很久的题..终于ac了 1354799 orzliyicheng 2599 Accepted 31936 kb 23584 ms ...
- 【BZOJ2599】Race(点分治)
[BZOJ2599]Race(点分治) 题面 BZOJ权限题,洛谷 题解 好久没写过点分治了... 在ppl的帮助下终于想起来了 orz ppl 首先回忆一下怎么求有没有正好是\(K\)的路径 维护一 ...
- 【BZOJ2599】[IOI2011]Race 树的点分治
[BZOJ2599][IOI2011]Race Description 给一棵树,每条边有权.求一条简单路径,权值和等于K,且边的数量最小.N <= 200000, K <= 100000 ...
- [bzoj2599][IOI2011]Race——点分治
Brief Description 给定一棵带权树,你需要找到一个点对,他们之间的距离为k,且路径中间的边的个数最少. Algorithm Analyse 我们考虑点分治. 对于子树,我们递归处理,所 ...
- 【BZOJ-2599】Race 点分治
2599: [IOI2011]Race Time Limit: 70 Sec Memory Limit: 128 MBSubmit: 2590 Solved: 769[Submit][Status ...
- bzoj1758 [Wc2010]重建计划 & bzoj2599 [IOI2011]Race
两题都是树分治. 1758这题可以二分答案avgvalue,因为avgvalue=Σv(e)/s,因此二分后只需要判断Σv(e)-s*avgvalue是否大于等于0,若大于等于0则调整二分下界,否则调 ...
- bzoj2599: [IOI2011]Race(点分治)
写了四五道点分治的题目了,算是比较理解点分治是什么东西了吧= = 点分治主要用来解决点对之间的问题的,比如距离为不大于K的点有多少对. 这道题要求距离等于K的点对中连接两点的最小边数. 那么其实道理是 ...
- BZOJ2599 [IOI2011]Race
传送门 点分治,黄学长的选根方法会T掉,换了这个人的选根方法就可以了. 当然,你也可以选择黄学长的奇淫优化 //BZOJ 2599 //by Cydiater //2016.9.23 #include ...
- BZOJ2599——[IOI2011]Race
0.题意:给一棵树,每条边有权.求一条路径,权值和等于K,且边的数量最小. 1.分析:水题一道,一波树分治就好 我们可以发现这个题的K是比较小的,才100w,那么我们可以树分治一下,在遍历每一棵子树的 ...
随机推荐
- window.location下的属性说明
属性 说明 window.location.href 完整的url window.location.protocol 协议 window.location.hostname 主机名 window.lo ...
- uva656 Optimal Programs
Optimal Programs As you know, writing programs is often far from being easy. Things become even hard ...
- 微信公众号获取用户openId How to use cURL to get jSON data and decode the data?
w http://stackoverflow.com/questions/16700960/how-to-use-curl-to-get-json-data-and-decode-the-data
- Linux系统CPU核数等信息查看
版权声明:本文为原创文章,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明. https://blog.csdn.net/fgf00/article/details/52584 ...
- UVA11426 GCD - Extreme (II)---欧拉函数的运用
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- CF#301 D:Bad Luck Island (概率dp)
D:Bad Luck Island 一个岛上有r个石头,s个剪子,p个布,他们之间随机挑出两个相遇,如果不是相同物种,就会有一个消失,分别求出最后这座岛上只剩下一个物种的概率. 我们用dp[i][j] ...
- c++中字符串反转的3种方法
第一种:使用algorithm中的reverse函数 #include <iostream> #include <string> #include <algorithm& ...
- Mock Server 之 moco-runner 使用指南一
文章出处http://ju.outofmemory.cn/entry/96866 用以下命令可以启动moco-runner 服务 java -jar moco-runner-<version&g ...
- Linux系统——shell脚本
shell脚本编程 作用:通过命令行解析的方式,自动执行设定好的程序或命令代码.(若将脚本挂到定时任务中,就会自动在非工作时间里自动触发执行程序) Shell脚本文件以“.sh”结尾 规范的Shell ...
- Linux系统——inode和block
Linux文件属性 磁盘被分区并格式化为ext4文件系统后,会生成一定数量的inode和block Inode 索引节点 作用:存放文件的属性信息以及作为文件的索引(指向文件的实体block) Blo ...