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]\)加一 ...
随机推荐
- Library setup
- Python 简单购物程序
# Author:Eric Zhao# -*- coding:utf-8 -*-'''需求:启动程序后,让用户输入工资,然后打印商品列表允许用户根据商品编号购买商品用户选择商品后,检测余额是否够,够就 ...
- leetcode-9-basic-binary search
278. First Bad Version You are a product manager and currently leading a team to develop a new produ ...
- 带权并查集:CF-2015 ACM Arabella Collegiate Programming Contest(F题)
F. Palindrome Problem Description A string is palindrome if it can be read the same way in either di ...
- 数学基础:HUD1406-完数
完数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Descript ...
- C++ 虚函数&纯虚函数&抽象类&接口&虚基类(转)
http://www.cnblogs.com/fly1988happy/archive/2012/09/25/2701237.html 1. 多态 在面向对象语言中,接口的多种不同实现方式即为多态.多 ...
- Nginx配置语法和日志
nginx配置 配置文件 重启服务 http请求 nginx日志 一共有两个日志文件 在配置文件中添加这个,就可以在日志文件中看到请求的userAgent 配置语法的检查 nginx重新加载配置 发送 ...
- MongoDB学习-->Spring Data Mongodb-->MongodbTemplate
配置文件application-dev.yml: server: port: 8888 mongo: host: localhost port: 27017 timeout: 60000 db: ma ...
- pycharm安装包
pycharm的纯净版本 链接: https://pan.baidu.com/s/15fLsO_GCO8uaYNQjLVdNaw 密码: ef22
- Selenium WebDriver- 操作JavaScript的confirm弹窗
#encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...