SPOJ1825:Free tour II
题意
luogu的翻译
给定一棵n个点的树,树上有m个黑点,求出一条路径,使得这条路径经过的黑点数小于等于k,且路径长度最大
Sol
点分治辣
如果是等于\(k\)的话,开个桶取\(max\)就好了
而小于等于\(k\),就可以把桶换成树状数组,求前缀\(max\)
很慢能过
# 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, m, k, mx[_], root, size[_], vis[_], tot, first[_], cnt;
struct Edge{
int to, w, next;
} edge[_ << 1];
int ans, black[_], pass[_], dis[_], t[_], S[_], tk, G[_];
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 dd, RG int pp){
if((pp += black[u]) > tk) return;
pass[u] = pp, dis[u] = dd, S[++S[0]] = u, G[++G[0]] = u;
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, dd + w, pp);
}
}
IL void Cls(RG int x){
if(!x) t[x] = 0;
for(; x && x <= m; x += x & -x) t[x] = 0;
}
IL void Modify(RG int x, RG int v){
if(!x) t[x] = max(t[x], v);
for(; x && x <= m; x += x & -x) t[x] = max(t[x], v);
}
IL int Query(RG int x){
RG int ret = t[0];
for(; x; x -= x & -x) ret = max(ret, t[x]);
return ret;
}
IL void Calc(RG int u){
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, 0);
for(RG int i = 1; i <= S[0]; ++i){
RG int dd = dis[S[i]], pp = tk - pass[S[i]], tt = Query(pp);
ans = max(ans, dd + tt);
}
for(; S[0]; --S[0]){
RG int dd = dis[S[S[0]]], pp = pass[S[S[0]]];
Modify(pp, dd);
}
}
for(; G[0]; --G[0]) Cls(pass[G[G[0]]]);
}
IL void Solve(RG int u){
vis[u] = 1, tk = k - black[u];
if(tk >= 0) Calc(u);
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[]){
tot = n = Input(), k = Input(), m = Input();
Fill(first, -1), ans = 0;
for(RG int i = 1; i <= m; ++i) black[Input()] = 1;
for(RG int i = 1; i < n; ++i){
RG int u = Input(), v = Input(), w = Input();
Add(u, v, w), Add(v, u, w);
}
mx[0] = n + 1, GetRoot(1, 0), Solve(root);
printf("%d\n", ans);
return 0;
}
SPOJ1825:Free tour II的更多相关文章
- SPOJ1825/FTOUR2:Free tour II——包看得懂/看不懂题解
http://www.spoj.com/problems/FTOUR2/en/ 题目大意:给一棵黑白染色的树,求边权和最大且经过黑点不超过K的路径. ———————————————————— 前排膜拜 ...
- SPOJ1825 FTOUR2 - Free tour II
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 【SPOJ1825】Free tour II (点分治,启发式)
题意: 边权可能为负 思路: 感觉我自己写的还是太过僵硬了,可以灵活一点,比如可以多写几个不同的dfs求出不同的信息,而不是压到同一个dfs里 #include<cstdio> #incl ...
- SPOJ:Free tour II (树分治+启发式合并)
After the success of 2nd anniversary (take a look at problem FTOUR for more details), this 3rd year, ...
- SPOJ 1825 Free tour II (树的点分治)
题目链接 Free tour II 题意:有$N$个顶点的树,节点间有权值, 节点分为黑点和白点. 找一条最长路径使得 路径上黑点数量不超过K个 这是树的点分治比较基本的题,涉及树上启发式合并……仰望 ...
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- FPGA回忆记事(一):基于Nios II的LED实验
实验一:基于Nios II的LED实验 一. 创建Quartus II工程 1.打开Quartus II环境.开始->程序->Altera->Quartus II 9.1. 2 ...
- 1436:数列分段II
1436:数列分段II 题解 二分答案 我们最终答案的取值区间是[ max(a[i]) , ∑a[i] ] 设定 l=max(a[i]) , r=∑a[i] , mid不断二分 mid表示 ...
随机推荐
- window.location的路径
1 相对路径 window.location.href='add_affiche.php'; 或 window.location.href='./add_affiche.php'; 2 绝对路径 wi ...
- 微信小程序 sha1 实现密码加密
在utils中的util.js 文件中增加 函数 实现 字符串转换为16进制加密后的字符串 function encodeUTF8(s) { var i, r = [], c, x; for (i = ...
- 原码,反码,补码 与(&) 或(|) 非(~) 异或(^) 左移 << 右移 >> 无符号右移 >>>
原码 数字在计算机中以二进制表示,8位的字长,最高位是符号位, 正数为0,负数为1.比如,3为0000 0011: -3为1000 0011. 注意,Java中int为32位.3的16进制表示为3,- ...
- HTML/CSS 知识点
整个前端开发的工作流程 产品经理提出项目需求 UI出设计稿 前端人员负责开发静态页面(跟前端同步的后台人员在准备数据) 前后台的交互 测试 产品上线(后期项目维护) 互联网原理 当用户在浏览器输入网址 ...
- wired-wireless_priority
有线网卡和无线网卡同时上网,优先级切换的设置方法 默认有线网卡优先权高 Q:如果你想改为无线高,How do? A: ①进入网络属性的有线网卡的连接属性,选择TCP/IP属性,点"高级... ...
- web自动化一(selenium+python+pycharm环境搭建)
年前公司刚刚搭起了web自动化测试框架的环境,趁着过完年还没全部忘掉,准备把如何搭建环境的方法和大家分享下,有哪里不对的地方,请批评指正,共同进步,共勉! 为此我把搭建环境所需的软件打包上传到百度云, ...
- Java注释用处
1.Java注释: import cn.lonecloud.Doc; /** * Created by lonecloud on 2017/8/17. * 测试注释类型 {@link Doc#test ...
- springboot2.0(一):【重磅】Spring Boot 2.0权威发布
就在昨天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误, ...
- HDU - 1175 bfs
思路:d[x][y][z]表示以z方向走到(x, y)的转弯次数. 如果用优先队列会超时,因为加入队列的节点太多,无用的节点不能及时出队,会造成MLE,用单调队列即可. AC代码 #include & ...
- HDU - 1430 魔板 (bfs预处理 + 康托)
对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...