原题

给出一颗有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 || 树分治的更多相关文章

  1. SPOJ 1825 Free tour II 树分治

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

  2. SPOJ FTOUR2 - Free tour II

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

  3. SP1825 FTOUR2 - Free tour II 点分治+启发式合并+未调完

    题意翻译 给定一棵n个点的树,树上有m个黑点,求出一条路径,使得这条路径经过的黑点数小于等于k,且路径长度最大 Code: #include <bits/stdc++.h> using n ...

  4. SPOJ 1825 Free tour II (树的点分治)

    题目链接 Free tour II 题意:有$N$个顶点的树,节点间有权值, 节点分为黑点和白点. 找一条最长路径使得 路径上黑点数量不超过K个 这是树的点分治比较基本的题,涉及树上启发式合并……仰望 ...

  5. SPOJ1825 FTOUR2 - Free tour II

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

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

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

  7. spoj 1825 Free tour II

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

  8. FTOUR2 - Free tour II

    传送门 题目翻译的很清楚……似乎点分治的题题目描述都非常简洁. 还是那个操作,一条路径要么全部在一棵子树中,要么经过当前的重心,所以考虑点分治. 首先dfs求出重心的每一棵子树中,有i个黑点的最长路径 ...

  9. SP1825 【FTOUR2 - Free tour II】

    # \(SP1825\) 看到没有人用老师的办法,于是自己写一下思路 思路第一步:排除旧方法 首先这道题和\(4178\)不一样,因为那道题是计数,而这道题是求最值,最值有个坏处,就是对于来自相同子树 ...

随机推荐

  1. springboot+Druid+mybatis整合

    一.添加Druid.MySQL连接池.mybatis依赖 <!--整合Druid--> <dependency> <groupId>com.alibaba</ ...

  2. docker简介以及优缺点

    1.docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制, ...

  3. LeetCode970. 强整数

    问题:970. 强整数 用户通过次数0 用户尝试次数0 通过次数0 提交次数0 题目难度Easy 给定两个非负整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且  ...

  4. java 动态生成SQL

    代码如下: /** * 动态生成SQ及SQL参数L * @param ve 接收到的消息的CHGLIST * @param paramList MQ消息中的SQL参数 * @param t 泛型对象 ...

  5. PHP常用180函数总结

    数学函数 1.abs(): 求绝对值 <span style="font-size: 14px;">$abs = abs(-4.2); //4.2<br>& ...

  6. javascript 运行机制 事件循环 浏览器缓存 (慕课网 前段跳槽面试必备 4-1,4-2,4-3)

    4-1 渲染机制:-1-,什么是DOCTYPE及其作用?DTD(document type definition,文档类型定义)是一系列的语法规则,用来定义XML或(X)HTML的文件类型,浏览器会使 ...

  7. 时间轮算法的定时器(Delphi)

    源码下载 http://files.cnblogs.com/lwm8246/uTimeWheel.rar D7,XE2 编译测试OK //时间轮算法的定时器 //-- : QQ unit uTimeW ...

  8. POJ:2739-Sum of Consecutive Prime Numbers(尺取)

    Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27853 Ac ...

  9. Docker构建nginx+uwsgi+flask镜像(二)

    Dockerfile搭建环境并打包应用 在上一章Docker构建nginx+uwsgi+flask镜像(一)的学习中,我们学会用命令行一句一句在alpine环境中搭建nginx+uwsgi+flask ...

  10. 获取ubuntu中软件包的有用地址

    http://us.archive.ubuntu.com/ubuntu/pool/main/g/gettext/