SPOJ 1825 Free tour II (树的点分治)
题目链接 Free tour II
题意:有$N$个顶点的树,节点间有权值, 节点分为黑点和白点。 找一条最长路径使得 路径上黑点数量不超过K个
这是树的点分治比较基本的题,涉及树上启发式合并……仰望了黄学长的博客之后稍微有点明白了(还没有很深入地理解)
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) const int N = 200010; int ans, n, k, m, cnt, root, sum, deep_mx, x;
int sz[N], f[N], deep[N], dis[N], tmp[N], mx[N];
bool color[N], vis[N];
vector <pair <int, int > > v[N];
vector <pair <int, int > > st; void getroot(int x, int fa){
sz[x] = 1; f[x] = 0;
for (auto u : v[x]){
int to = u.first;
if (vis[to] || to == fa) continue;
getroot(to, x);
f[x] = max(f[x], sz[to]);
sz[x] += sz[to];
} f[x] = max(f[x], sum - sz[x]);
if (f[x] < f[root]) root = x;
} void getdis(int x, int fa){
deep_mx = max(deep_mx, deep[x]);
for (auto u : v[x]){
int to = u.first;
if (vis[to] || to == fa) continue;
deep[to] = deep[x] + color[to];
dis[to] = dis[x] + u.second;
getdis(to, x);
}
} void getmx(int x, int fa){
tmp[deep[x]] = max(tmp[deep[x]], dis[x]);
for (auto u : v[x]){
int to = u.first;
if (vis[to] || to == fa) continue;
getmx(to, x);
}
} void solve(int x){
vis[x] = 1;
st.clear();
if (color[x]) --k;
for (auto u : v[x]){
int to = u.first;
if (vis[to]) continue;
deep_mx = 0;
deep[to] = color[to];
dis[to] = u.second;
getdis(to, x);
st.push_back({deep_mx, to});
} sort(st.begin(), st.end());
for (int i = 0; i < (int)st.size(); ++i){
getmx(st[i].second, x);
int now = 0;
if (i != 0)
dec(j, st[i].first, 0){
while (now + j < k && now < st[i - 1].first)
++now, mx[now] = max(mx[now], mx[now - 1]);
if (now + j <= k) ans = max(ans, mx[now] + tmp[j]);
}
if (i != (int)st.size() - 1)
rep(j, 0, (int)st[i].first)
mx[j] = max(mx[j], tmp[j]), tmp[j] = 0;
else
rep(j, 0, (int)st[i].first){
if (j <= k) ans = max(ans, max(tmp[j], mx[j]));
tmp[j] = mx[j] = 0;
}
} if (color[x]) ++k;
for (auto u : v[x]){
int to = u.first;
if (vis[to]) continue;
root = 0;
sum = sz[to];
getroot(to, x);
solve(root);
}
} int main(){ scanf("%d%d%d", &n, &k, &m);
rep(i, 1, m){
scanf("%d", &x);
color[x] = 1;
} rep(i, 1, n - 1){
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
v[x].push_back({y, z});
v[y].push_back({x, z});
} sum = n; f[0] = n;
getroot(1, 0);
solve(root);
printf("%d\n", ans);
return 0;
}
SPOJ 1825 Free tour II (树的点分治)的更多相关文章
- SPOJ 1825 Free tour II 树分治
题意: 给出一颗边带权的数,树上的点有黑色和白色.求一条长度最大且黑色节点不超过k个的最长路径,输出最长的长度. 分析: 说一下题目的坑点: 定义递归函数的前面要加inline,否则会RE.不知道这是 ...
- spoj 1825 Free tour II
http://www.spoj.com/problems/FTOUR2/ After the success of 2nd anniversary (take a look at problem FT ...
- [spoj] FTOUR2 FREE TOUR II || 树分治
原题 给出一颗有n个点的树,其中有M个点是拥挤的,请选出一条最多包含k个拥挤的点的路径使得经过的权值和最大. 正常树分治,每次处理路径,更新答案. 计算每棵子树的deep(本题以经过拥挤节点个数作为d ...
- 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(点分治)
http://www.spoj.com/problems/FTOUR2/ 先前看了一会题解就自己yy出来了...对拍过后交tle.................. 自己造了下大数据........t ...
- SPOJ 1825 Free Tour | 终极之树分治
求树上最长路径使得经过的拥挤节点个数不超过K //欢迎访问这个博客!http://www.cnblogs.com/luyouqi233/p/8036828.html #include<cstdi ...
- SPOJ FTOUR2 - Free tour II
Description 有些黑点,问你选择不超过 \(k\) 个黑点的路径,路径权值最大是多少. Sol 点分治. 这是qzc的论文题,不过我感觉他的翻译好强啊...我还是选择了自己去看题目... 点 ...
- SPOJ1825 FTOUR2 - Free tour II
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 【SPOJ】Highways(矩阵树定理)
[SPOJ]Highways(矩阵树定理) 题面 Vjudge 洛谷 题解 矩阵树定理模板题 无向图的矩阵树定理: 对于一条边\((u,v)\),给邻接矩阵上\(G[u][v],G[v][u]\)加一 ...
随机推荐
- iPhone如何设置自定义铃声?无需连接电脑,轻松几步就搞定!
转载自: https://baijiahao.baidu.com/s?id=1594988016778457969&wfr=spider&for=pc 受够了iPhone自带的千篇一律 ...
- mysql 的 case when 用法
正确的格式: case when condition then result when condition then result when condition then result else re ...
- destoon 给超级管理员系统权限(管理员管理,日志管理等)
destoon 后台某些系统权限除了网站创始人之外其他超管事没有权限的,现需要给其他超级管理员添加普通管理员的权限. 1.首先 admin/global.func.php admin_check函 ...
- poj 2531 分权问题 dfs算法
题意:一个集合(矩阵) m[i][j]=m[j][i]权值,分成两个集合,使其权值最大.注:在同一个集合中权值只能算一个. 思路:dfs 假设都在集合0 遍历 id 的时候拿到集合1 如果与 id 相 ...
- jnative 使用
下载地址: JNative_1.4RC2_src.zip : http://jaist.dl.sourceforge.net/sourceforge/jnative/JNative_1.4RC2_sr ...
- SDOJ 3740 Graph
8.9 t3 [描述] 给你一个图,一共有 N 个点,2*N-2 条有向边. 边目录按两部分给出 1. 开始的 n-1 条边描述了一颗以 1 号点为根的生成树,即每个点都可以由 1 号点 到达. 2. ...
- LiveScript 操作符
The LiveScript Book The LiveScript Book 操作符 数字 标准的数学操作符: 1.1 + 2 # => 32.3 - 4 # => -13.6 ...
- python面向对象、模块讲解
(1)模块的介绍: 1.什么是模块 模块是一系列功能的集合体 常见的模块形式(自定义模块.第三方模块.内置模块): 1.一个module.py文件就是一个模块,文件名是module.py,而模 ...
- [笔记]Docker解决了什么问题?
Docker的优势: 环境依赖问题 更轻量的虚拟化,节省了虚拟机的性能损耗 Docker应用场景: 程序分发,gitlab的安装很恶心吧,所以有人做了gitlab的image 部署发布,这点对运维的同 ...
- C#发送邮件异常,返回信息乱码
发邮件时出现了异常: 在 System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) 在 ...