[spoj] FTOUR2 FREE TOUR II || 树分治
原题
给出一颗有n个点的树,其中有M个点是拥挤的,请选出一条最多包含k个拥挤的点的路径使得经过的权值和最大。
正常树分治,每次处理路径,更新答案。
计算每棵子树的deep(本题以经过拥挤节点个数作为deep),然后记录mx[i]为当前为止经过i个拥挤节点所达到的最大价值,tmp[i]为当前所在树中经过i个拥挤节点所达到的最大价值,用于更新答案即可。
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#define N 200010
using namespace std;
int ans,n,K,m,cnt,head[N],f[N];
vector < pair<int,int> > v;
struct hhh
{
int to,next,w;
}edge[2*N];
int read()
{
int ans=0,fu=1;
char j=getchar();
for (;j<'0' || j>'9';j=getchar()) if (j=='-') fu=-1;
for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
return ans*fu;
}
void add(int u,int v,int w)
{
edge[cnt].to=v;edge[cnt].next=head[u];edge[cnt].w=w;head[u]=cnt++;
edge[cnt].to=u;edge[cnt].next=head[v];edge[cnt].w=w;head[v]=cnt++;
}
void getroot(int x,int fa)
{
sze[x]=1;
son[x]=0;
for (int i=head[x];i;i=edge[i].next)
if (!vis[edge[i].to] && edge[i].to!=fa)
{
getroot(edge[i].to,x);
son[x]=max(son[x],sze[edge[i].to]);
sze[x]+=sze[edge[i].to];
}
son[x]=max(son[x],sum-sze[x]);
if (son[x]<son[rt]) rt=x;
}
void getdis(int x,int fa)
{
deep_mx=max(deep_mx,deep[x]);
for (int i=head[x];i;i=edge[i].next)
if (!vis[edge[i].to] && edfe[i].to!=fa)
{
deep[edge[i].to]=deep[x]+color[edge[i].to];
dis[edge[i].to]=dis[x]+edge[i].w;
getdis(edge[i].to,x);
}
}
void getmx(int x,int fa)
{
tmp[deep[x]]=max(tmp[deep[x]],dis[x]);
for (int i=head[x];i;i=edge[i].to)
if (!vis[edge[i].to] && edge[i].to!=fa)
getmx(edge[i].to,x);
}
void solve(int x)
{
vis[x]=1;
v.clear();
for (int i=head[x];i;i=edge[i].next)
if (!vis[edge[i].to])
{
deep_mx=0;
deep[edge[i].to]=color[edge[i].to];
dis[edge[i].to]=edge[i].ww;
getdis(edge[i].to,x);
v.push_back(make_pair(deep_mx,edge[i].to));
}
sort(v.begin(),v.end());
int s=v.size();
for (int i=0;i<s;i++)
{
getmx(st[i].second,x);
int now=0;
if (i!=0)
for (int j=v[i].first;j>=0;j--)
{
while (now+j<K && now<st[i-1].first)
now++,mx[now]=max(mx[now],mx[now-1]);
if (now+j<=K) ans=max(mx[now]+tmp[j]);
}
if (i!=s-1)
for (int j=0;j<=v[i].first;j++)
mx[j]=max(mx[j],tmp[j]),tmp[j]=0;
else
for (int j=0;j<=v[i].first;j++)
{
if (j<=K) ans=max(ans,max(tmp[j],mx[j]));
tmp[j]=mx[j]=0;
}
}
}
int main()
{
n=read();
K=read();
m=read();
for (int i=1;i<=m;i++)
{
int x=read();
color[x]=1;
}
for (int i=1,u,v,w;i<n;i++)
{
u=read();v=read();w=read();
add(u,v,w);
}
sum=n;
f[0]=n;
getroot(1,0);
solve(rt);
printf("%d",ans);
return 0;
}
[spoj] FTOUR2 FREE TOUR II || 树分治的更多相关文章
- SPOJ 1825 Free tour II 树分治
题意: 给出一颗边带权的数,树上的点有黑色和白色.求一条长度最大且黑色节点不超过k个的最长路径,输出最长的长度. 分析: 说一下题目的坑点: 定义递归函数的前面要加inline,否则会RE.不知道这是 ...
- SPOJ FTOUR2 - Free tour II
Description 有些黑点,问你选择不超过 \(k\) 个黑点的路径,路径权值最大是多少. Sol 点分治. 这是qzc的论文题,不过我感觉他的翻译好强啊...我还是选择了自己去看题目... 点 ...
- SP1825 FTOUR2 - Free tour II 点分治+启发式合并+未调完
题意翻译 给定一棵n个点的树,树上有m个黑点,求出一条路径,使得这条路径经过的黑点数小于等于k,且路径长度最大 Code: #include <bits/stdc++.h> using n ...
- SPOJ 1825 Free tour II (树的点分治)
题目链接 Free tour II 题意:有$N$个顶点的树,节点间有权值, 节点分为黑点和白点. 找一条最长路径使得 路径上黑点数量不超过K个 这是树的点分治比较基本的题,涉及树上启发式合并……仰望 ...
- SPOJ1825 FTOUR2 - Free tour II
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 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/ After the success of 2nd anniversary (take a look at problem FT ...
- FTOUR2 - Free tour II
传送门 题目翻译的很清楚……似乎点分治的题题目描述都非常简洁. 还是那个操作,一条路径要么全部在一棵子树中,要么经过当前的重心,所以考虑点分治. 首先dfs求出重心的每一棵子树中,有i个黑点的最长路径 ...
- SP1825 【FTOUR2 - Free tour II】
# \(SP1825\) 看到没有人用老师的办法,于是自己写一下思路 思路第一步:排除旧方法 首先这道题和\(4178\)不一样,因为那道题是计数,而这道题是求最值,最值有个坏处,就是对于来自相同子树 ...
随机推荐
- Decrypt.java
import java.io.PrintStream;import weblogic.security.internal.*;import weblogic.security.internal.enc ...
- WIN10使用安装包安装Mysql5.6+JDBC
很多教程教的是安装绿色版mysql或者是安装zip版的mysql,没什么不好,各有千秋,今天要教大家的是使用mysql-installer-community-5.6.43.0.msi安装mysql5 ...
- linux定时任务及练习
第1章 定时任务 1.1 什么是定时任务 相当于闹钟每天叫你起床 设定一个时间去做某件事 1.2 系统定时任务 [root@zeq ~]# ll -d /etc/cron* drwxr-xr-x. 2 ...
- 【牛客 错题集】Linux系统方面错题合集
前言:牛客Linux322道全部刷完,有些题目较老,甚至考核5系统,现在7都出来了几年了 = = 还有些题目解析的很好部分也摘录了进来.很多涉及嵌入式开发的选择题同样的摘录的作为了解使用 ------ ...
- SQL-批量插入和批量更新
批量插入 表结构一样或类似 如果两张表的结构一样,例如一个表的结构和另一个表的结构一样,只是其中一张是临时表,而另一张表是存储数据的表,我们需要进行一次表的迁移的话,我们可以这样. insert in ...
- Robots Gym - 101915G
传送门 The Robotics Olympiad teams were competing in a contest. There was a tree drawn on the floor, co ...
- Codeforces Round #449 (Div. 2) C. DFS
C. Nephren gives a riddle time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- cocos2d-x 3.0的入门程序:helloworld
看过了这么多不同方向的应用,发现很多程序入门都是helloworldhelloworld是所有程序员的绝对初恋 先看一下程序的运行结果吧 然后就是他的工程代码 工程的目录有两个 Classes:程序中 ...
- 缓存(CDN缓存,浏览器(客户端)缓存)
1.什么是缓存? 缓存是一种数据结构,用于快速查找以及执行的操作结果.因此,如果一个操作执行起来很慢,对于常用的输入数据就可以将操作的结果缓存,并在下次调用该操作时使用缓存的数据. 缓存是一个到处都存 ...
- 二分查找iOS
二分查找(也称折半查找)是很常见的一种在数组中查找数据的算法,作为一名程序员是应该必须会的.它的基础思想:获取数组的中间值,将数组分割成两份,利用查找的值跟中间值进行比较,如果查找的值大于中间值,就在 ...