题目链接 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 (树的点分治)的更多相关文章

  1. SPOJ 1825 Free tour II 树分治

    题意: 给出一颗边带权的数,树上的点有黑色和白色.求一条长度最大且黑色节点不超过k个的最长路径,输出最长的长度. 分析: 说一下题目的坑点: 定义递归函数的前面要加inline,否则会RE.不知道这是 ...

  2. spoj 1825 Free tour II

    http://www.spoj.com/problems/FTOUR2/ After the success of 2nd anniversary (take a look at problem FT ...

  3. [spoj] FTOUR2 FREE TOUR II || 树分治

    原题 给出一颗有n个点的树,其中有M个点是拥挤的,请选出一条最多包含k个拥挤的点的路径使得经过的权值和最大. 正常树分治,每次处理路径,更新答案. 计算每棵子树的deep(本题以经过拥挤节点个数作为d ...

  4. SPOJ:Free tour II (树分治+启发式合并)

    After the success of 2nd anniversary (take a look at problem FTOUR for more details), this 3rd year, ...

  5. 【SPOJ】1825. Free tour II(点分治)

    http://www.spoj.com/problems/FTOUR2/ 先前看了一会题解就自己yy出来了...对拍过后交tle.................. 自己造了下大数据........t ...

  6. SPOJ 1825 Free Tour | 终极之树分治

    求树上最长路径使得经过的拥挤节点个数不超过K //欢迎访问这个博客!http://www.cnblogs.com/luyouqi233/p/8036828.html #include<cstdi ...

  7. SPOJ FTOUR2 - Free tour II

    Description 有些黑点,问你选择不超过 \(k\) 个黑点的路径,路径权值最大是多少. Sol 点分治. 这是qzc的论文题,不过我感觉他的翻译好强啊...我还是选择了自己去看题目... 点 ...

  8. SPOJ1825 FTOUR2 - Free tour II

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  9. 【SPOJ】Highways(矩阵树定理)

    [SPOJ]Highways(矩阵树定理) 题面 Vjudge 洛谷 题解 矩阵树定理模板题 无向图的矩阵树定理: 对于一条边\((u,v)\),给邻接矩阵上\(G[u][v],G[v][u]\)加一 ...

随机推荐

  1. 服务器TIME_WAIT和CLOSE_WAIT分析和解决办法

    先上两张图: 查看TIME_WAIT和CLOSE_WAIT数的命令: netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a ...

  2. phpMyAdmin关于PHP 5.5+ is required. Currently installed version is: 5.4.16问题

    出现这个提示PHP 5.5+ is required. Currently installed version is: 5.4.16原因可能是: phpmyadmin 版本太新,最小需要php5.5. ...

  3. Mysql显示某个数据库的所有表

    显示表名: show tables; //先用use进入要查看表的库 mysql> use mysql; Database changed mysql> show tables; +--- ...

  4. CF 219 D:Choosing Capital for Treeland(树形dp)

    D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D   The country Tre ...

  5. git仓库删除所有提交历史记录

    stackoverflow原问题地址:http://stackoverflow.com/questions/13716658/how-to-delete-all-commit-history-in-g ...

  6. loj2012 「SCOI2016」背单词

    -- #include <algorithm> #include <iostream> #include <cstring> #include <cstdio ...

  7. set的特性和基本用法——python3.6

    特性 无序,不重复的数据组合,用{}表示,eg:{1,2,3,4,5,6} 用途 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之间的交集,差集,并集,对称差集,包含(子集和超集,相交 ...

  8. TOJ3031: Multiple bfs

    3031: Multiple Time Limit(Common/Java):2000MS/6000MS     Memory Limit:65536KByte Total Submit: 60   ...

  9. ubuntu检测到系统错误解决方法

    解决方案: 1.打开终端,输入 sudo gedit /etc/default/apport 2.把里面的enabled=1改成enabled=0,保存

  10. [AtCoderContest015D]A or...or B Problem

    [AtCoderContest015D]A or...or B Problem 试题描述 Nukes has an integer that can be represented as the bit ...