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]\)加一 ...
随机推荐
- php使用curl访问https返回无结果的问题
最近在做一个微信自动登录,发起验证以后回调页面获取openid时 curl函数返回空. $appid = "appid appid "; $secret = "secre ...
- Linux下的jdk安装
我使用的是CentOS7,jdk使用的是JDK1.8 下载好以后,将jdk传到我自己的目录:/home/tool下,通过 tar -xzvf jdk-8u131-linux-x64.tar.gz解压 ...
- MIP启发式算法:遗传算法 (Genetic algorithm)
*本文主要记录和分享学习到的知识,算不上原创 *参考文献见链接 本文主要讲述启发式算法中的遗传算法.遗传算法也是以local search为核心框架,但在表现形式上和hill climbing, ta ...
- vim 查找替换命令
http://vim.wikia.com/wiki/Search_and_replace
- ie9/8的iframe中jQuery报错
此文章用于对工作中遇到的问题进行记录 jQuery 版本:1.9.1 按照一般的思路,jquery 1.x的是支持ie9及以下的,但是今天发现jquery报错了,代码错误位置在源码版本的第4888行 ...
- SVN 删除所有目录下的“.svn”文件夹,让文件夹脱离SVN控制
SVN 删除所有目录下的“.svn”文件夹,将如下语句拷备到记事本,并保存为 *.reg,双击导入注册表,在文件夹右键中就多了一条“Delete SVN Folders”,点击就可以删处此目录下的所有 ...
- 深入了解SEO
为什么要SEO,SEO的作用是什么?SEO(Search Engine Optimization)是为了让自己的IT产品优先能被搜索引擎找到,通过搜索引擎搜索推荐给网民浏览(特点就是精准找到用户群体) ...
- SCOI 2010 滑雪
题目描述 a180285非常喜欢滑雪.他来到一座雪山,这里分布着 MM 条供滑行的轨道和 NN 个轨道之间的交点(同时也是景点),而且每个景点都有一编号 ii ( 1 \le i \le N1≤i≤N ...
- 配置 MySQL 服务器容器
本文介绍在单一宿主机上如何配置自动备份.建议使用两个容器,其中一个容器作为 MySQL 的服务器,用来处理数据:另一个容器用于自动备份.这样保证隔离,避免备份的容器影响到 MySQL Server 的 ...
- [图论训练]BZOJ 2118: 墨墨的等式 【最短路】
Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在 ...