题目

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. PHP判断SQL语句是否合法:mysqli_error()

    假设现在有条update语句,有时候update语句正确,但是受影响的行数是0. 那么怎么判断这条SQL语句到底是否正确?使用 mysqli_error($Conn); create table us ...

  2. vagrant启动报错The following SSH command responded with a no

    vagrant package打包生成box,以这个box为基础模板,打造vagrant环境,启动vagrant报错 angel:vagrant $ vagrant up Bringing machi ...

  3. 手机APP中使用history.back()返回没有效果的解决

    样式是一个超链接A标签,通过点击事件来达到返回上一页的效果. 所以通常做饭是把A标签的href写成#,然后onClick事件,刚开始我只是当成一个普通点击事件,然后使用JS进行返回. 写法如下: &l ...

  4. JavaScript面向对象学习笔记

    JavaScript 常被描述为一种基于原型的语言 (prototype-based language)--每个对象拥有一个原型对象,对象以其原型为模板.从原型继承方法和属性.原型对象也可能拥有原型, ...

  5. C语言链表的建立、插入和删除

    先看下向链表中插入节点 下面这个是删除链表节点

  6. Ubuntu忘记root密码怎么办?

    http://www.linuxidc.com/Linux/2016-05/131256.htm

  7. 安装git,gitlab和TortoiseGit

    全部都是默认配置安装 需注册用户:用户名尽量好认 测试用户: 注册成功: 生成密钥: 1.首先使用TortoiseGit自带的Puttygen创建本地的公/私钥对 2.点击Generate按钮,在窗口 ...

  8. 【剑指offer】04替换空格,C++实现

    0.前言 # 替换空格为字符串部分的题目,剑指offer中字符串系列的文章地址,剑指offer全系列文章地址 1.题目 # 请实现一个函数,将一个字符串中的空格替换成"%20".例 ...

  9. PAT乙级1065 map

    思路:检查某个客人是否有伴侣,如果有,伴侣是否也出现即可. 注意:0个单身狗的时候,不要输出多余的'\n', 否则会出现格式错误. AC代码 #include <stdio.h> #inc ...

  10. css线性渐变兼容

    css线性渐变兼容 background: linear-gradient(top, rgba(54, 77, 127, 0.8), rgba(54, 77, 127, 0.8)); backgrou ...