题目

bzoj权限题。。。

Luogu

Sol

点分治辣,边权非负,k>=1,开个\(1e6\)的桶就好辣

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(2e5 + 5); IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
} int n, k, first[_], cnt, mx[_], root, size[_], vis[_];
int tot, ans, t[_ * 5], S[_], deep[_], num[_], tmpS[_];
struct Edge{
int to, w, next;
} edge[_ << 1]; IL void Add(RG int u, RG int v, RG int w){
edge[cnt] = (Edge){v, w, first[u]}, first[u] = cnt++;
} IL void GetRoot(RG int u, RG int ff){
size[u] = 1, mx[u] = 0;
for(RG int e = first[u]; e != -1; e = edge[e].next){
RG int v = edge[e].to;
if(vis[v] || v == ff) continue;
GetRoot(v, u);
size[u] += size[v];
mx[u] = max(mx[u], size[v]);
}
mx[u] = max(mx[u], tot - size[u]);
if(mx[u] < mx[root]) root = u;
} IL void GetDeep(RG int u, RG int ff, RG int ss, RG int nn){
if(ss > k) return;
tmpS[++tmpS[0]] = u, S[++S[0]] = u, deep[u] = ss, num[u] = nn;
for(RG int e = first[u]; e != -1; e = edge[e].next){
RG int v = edge[e].to, w = edge[e].w;
if(vis[v] || v == ff) continue;
GetDeep(v, u, ss + w, nn + 1);
}
} IL void Calc(){
for(RG int i = 1; i <= S[0]; ++i){
RG int d = deep[S[i]];
if(d > k) continue;
if(!t[k - d]) continue;
ans = min(ans, num[S[i]] + t[k - d]);
}
for(; S[0]; --S[0]){
RG int d = deep[S[S[0]]];
if(deep[S[S[0]]] <= k){
if(!t[d]) t[d] = num[S[S[0]]];
else t[d] = min(num[S[S[0]]], t[d]);
}
}
} IL void Solve(RG int u){
vis[u] = 1;
for(RG int e = first[u]; e != -1; e = edge[e].next){
RG int v = edge[e].to, w = edge[e].w;
if(vis[v]) continue;
GetDeep(v, u, w, 1), Calc();
}
if(t[k]) ans = min(ans, t[k]);
for(; tmpS[0]; --tmpS[0]) if(deep[tmpS[tmpS[0]]] <= k) t[deep[tmpS[tmpS[0]]]] = 0;
for(RG int e = first[u]; e != -1; e = edge[e].next){
RG int v = edge[e].to;
if(vis[v]) continue;
root = 0, tot = size[v];
GetRoot(v, u), Solve(root);
}
} int main(RG int argc, RG char* argv[]){
freopen("ioi2011-race.in", "r", stdin);
freopen("ioi2011-race.out", "w", stdout);
tot = n = Input(), k = Input(), Fill(first, -1);
for(RG int i = 1; i < n; ++i){
RG int u = Input() + 1, v = Input() + 1, w = Input();
Add(u, v, w), Add(v, u, w);
}
ans = mx[0] = n + 1, GetRoot(1, 0), Solve(root);
printf("%d\n", ans == n + 1 ? -1 : ans);
return 0;
}

Luogu4149:[IOI2011]Race的更多相关文章

  1. BZOJ2599:[IOI2011]Race

    浅谈树分治:https://www.cnblogs.com/AKMer/p/10014803.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem. ...

  2. [luogu4149][bzoj2599][IOI2011]Race【点分治】

    题目描述 给一棵树,每条边有权.求一条简单路径,权值和等于 K,且边的数量最小. 题解 比较明显需要用到点分治,我们定义\(d\)数组表示当前节点到根节点\(rt\)之间有多少个节点,也可以表示有多少 ...

  3. [IOI2011]Race

    2599: [IOI2011]Race Time Limit: 70 Sec  Memory Limit: 128 MBhttp://www.lydsy.com/JudgeOnline/problem ...

  4. 【BZOJ2599】[IOI2011]Race 树的点分治

    [BZOJ2599][IOI2011]Race Description 给一棵树,每条边有权.求一条简单路径,权值和等于K,且边的数量最小.N <= 200000, K <= 100000 ...

  5. [bzoj2599][IOI2011]Race——点分治

    Brief Description 给定一棵带权树,你需要找到一个点对,他们之间的距离为k,且路径中间的边的个数最少. Algorithm Analyse 我们考虑点分治. 对于子树,我们递归处理,所 ...

  6. 模板—点分治B(合并子树)(洛谷P4149 [IOI2011]Race)

    洛谷P4149 [IOI2011]Race 点分治作用(目前只知道这个): 求一棵树上满足条件的节点二元组(u,v)个数,比较典型的是求dis(u,v)(dis表示距离)满足条件的(u,v)个数. 算 ...

  7. BZOJ 2599: [IOI2011]Race( 点分治 )

    数据范围是N:20w, K100w. 点分治, 我们只需考虑经过当前树根的方案. K最大只有100w, 直接开个数组CNT[x]表示与当前树根距离为x的最少边数, 然后就可以对根的子树依次dfs并更新 ...

  8. [IOI2011]Race 点分治

    [IOI2011]Race LG传送门 点分治板子题. 直接点分治统计,统计的时候开个桶维护下就好了. 注(tiao)意(le)细(hen)节(jiu). #include<cstdio> ...

  9. bzoj 2599 [IOI2011]Race 点分

    [IOI2011]Race Time Limit: 70 Sec  Memory Limit: 128 MBSubmit: 4768  Solved: 1393[Submit][Status][Dis ...

随机推荐

  1. CentOS 7 安装Java 1.8

    携程的Apollo配置中心服务端[https://github.com/ctripcorp/apollo/wiki]推荐的Java版本是:1.8+, 本文介绍如何在CentOS上安装java 1.8. ...

  2. No new migrations found. Your system is up-to-date.处理

    显然是migrations表中存储的相关操作记录了,删除就好了!!!

  3. python_crawler,批量下载文件

    这个第一个python3网络爬虫,参考书籍是<python网络数据采集>.该爬虫的主要功能是爬取某个网站,并将.rar,.doc,.docx,.zip文件批量下载. 后期将要改进的是,用后 ...

  4. 业余草分享 Spring Boot 2.0 正式发布的新特性

    就在昨天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误, ...

  5. React——共享state

    通常,一些组件需要反映相同的数据更改,这种情况推荐将共享state移动到它们最近的公共祖先上. 在这里有一个例子:有一个温度计算器计算在给定温度是否能让水沸腾,用户可以输入华氏温度也能输入摄氏温度,当 ...

  6. git 命令和使用场景总结

    资料地址:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000    http://w ...

  7. JavaScript函数的柯里化(currying)

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/currying.html 什么是js函数的currying /柯里化? 说到js的柯里化,相信很多朋友都会头大.或 ...

  8. 5.3 存储器、I/O和配置读写请求TLP

    本节讲述PCIe总线定义的各类TLP,并详细介绍这些TLP的格式.在这些TLP中,有些格式对于初学者来说较难理解.读者需要建立PCIe总线中与TLP相关的一些基本概念,特别是存储器读写相关的报文格式. ...

  9. dojo省份地市级联之地市Dao接口类(四)

    dojo省份地市级联之地市Dao接口类 CityDao.java: /** * 地市 */ package com.you.dao; import java.util.List; import com ...

  10. SQL注入攻击三部曲之进阶篇

    SQL注入攻击三部曲之进阶篇 通过入门篇的学习,我们知道了SQL注入攻击的判断方法,但是如果想侵入网站,获取网站的机密内容,那么仅靠入门篇的知识是无法达到的.本篇文章我们将进一步的分析SQL注入攻击. ...