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表示 ...
随机推荐
- centos 7 双网卡建网桥脚本实现
#!/bin/bash interface1=`ls /sys/class/net|grep en|awk 'NR==1{print}'` interface2=`ls /sys/class/net| ...
- easyui datagrid 右边框被隐藏
问题前: 如下图: 解决思路: 待文档加载完成后再执行dategrid函数 $(function () { $("#tt").datagrid({ //....... }); }) ...
- Sql Server 常用事务处理总结
在数据库操作中,常用事务写法: 1. 通过 @@error 判断一批sql 执行完毕,是否有异常. @@error 为系统变量,每次执行完 sql 都会返回一个数值, 0 表示 执行成功 ,非0 ...
- java常用类————Date类
Date类在Java.util包中. 一.功能介绍:创建Date对象,获取时间,格式化输出的时间. 二.对象创建:1.使用Date类无参数的构造方法创建的对象可以获取本地时间.例如: Date now ...
- Winform下去除MDI窗体边框
做项目中间遇到了MDI窗体内边框的问题,经过苦苦寻找,最终得到了解决方案 在Main窗体中调用API // Win32 Constants ; ; private const int WS_BORDE ...
- 阿里巴巴分布式服务框架 Dubbo 介绍
Dubbo是阿里巴巴内部的SOA服务化治理方案的核心框架,每天为2000+ 个服务提供3,000,000,000+ 次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点.Dubbo自2011年开源后, ...
- C#中ASCII码学习心得
1.利用调用ASCIIEncoding类来实现各种转换.如简单个ACS码和int转换. ***利用(int)ASCIIEncoding类对象.GetBytes(character)[0]得到整数: p ...
- shiro整合ehcache
目标:让Shiro整合ehcache,提供缓存realm数据的功能. 1.引入encache配置文件,配置缓存 <!-- <ehcache xmlns:xsi="http://w ...
- Linux CentOS安装配置MySQL数据库
没什么好说的,直接正面刚吧. 安装mysql数据库 a)下载mysql源安装包:wget http://dev.mysql.com/get/mysql57-community-release-el7- ...
- 安装coreseek cannot find input file: src/Makefile.in 错误解决方法
安装coreseek 出现了cannot find input file: src/Makefile.in 解决方法如下 >autoheader >automake --add-missi ...