题意

luogu的翻译

给定一棵n个点的树,树上有m个黑点,求出一条路径,使得这条路径经过的黑点数小于等于k,且路径长度最大

Sol

点分治辣

如果是等于\(k\)的话,开个桶取\(max\)就好了

而小于等于\(k\),就可以把桶换成树状数组,求前缀\(max\)

很慢能过

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(2e5 + 5); IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
} int n, m, k, mx[_], root, size[_], vis[_], tot, first[_], cnt;
struct Edge{
int to, w, next;
} edge[_ << 1];
int ans, black[_], pass[_], dis[_], t[_], S[_], tk, G[_]; IL void Add(RG int u, RG int v, RG int w){
edge[cnt] = (Edge){v, w, first[u]}, first[u] = cnt++;
} IL void GetRoot(RG int u, RG int ff){
size[u] = 1, mx[u] = 0;
for(RG int e = first[u]; e != -1; e = edge[e].next){
RG int v = edge[e].to;
if(vis[v] || v == ff) continue;
GetRoot(v, u);
size[u] += size[v];
mx[u] = max(mx[u], size[v]);
}
mx[u] = max(mx[u], tot - size[u]);
if(mx[u] < mx[root]) root = u;
} IL void GetDeep(RG int u, RG int ff, RG int dd, RG int pp){
if((pp += black[u]) > tk) return;
pass[u] = pp, dis[u] = dd, S[++S[0]] = u, G[++G[0]] = u;
for(RG int e = first[u]; e != -1; e = edge[e].next){
RG int v = edge[e].to, w = edge[e].w;
if(vis[v] || v == ff) continue;
GetDeep(v, u, dd + w, pp);
}
} IL void Cls(RG int x){
if(!x) t[x] = 0;
for(; x && x <= m; x += x & -x) t[x] = 0;
} IL void Modify(RG int x, RG int v){
if(!x) t[x] = max(t[x], v);
for(; x && x <= m; x += x & -x) t[x] = max(t[x], v);
} IL int Query(RG int x){
RG int ret = t[0];
for(; x; x -= x & -x) ret = max(ret, t[x]);
return ret;
} IL void Calc(RG int u){
for(RG int e = first[u]; e != -1; e = edge[e].next){
RG int v = edge[e].to, w = edge[e].w;
if(vis[v]) continue;
GetDeep(v, u, w, 0);
for(RG int i = 1; i <= S[0]; ++i){
RG int dd = dis[S[i]], pp = tk - pass[S[i]], tt = Query(pp);
ans = max(ans, dd + tt);
}
for(; S[0]; --S[0]){
RG int dd = dis[S[S[0]]], pp = pass[S[S[0]]];
Modify(pp, dd);
}
}
for(; G[0]; --G[0]) Cls(pass[G[G[0]]]);
} IL void Solve(RG int u){
vis[u] = 1, tk = k - black[u];
if(tk >= 0) Calc(u);
for(RG int e = first[u]; e != -1; e = edge[e].next){
RG int v = edge[e].to;
if(vis[v]) continue;
root = 0, tot = size[v];
GetRoot(v, u), Solve(root);
}
} int main(RG int argc, RG char* argv[]){
tot = n = Input(), k = Input(), m = Input();
Fill(first, -1), ans = 0;
for(RG int i = 1; i <= m; ++i) black[Input()] = 1;
for(RG int i = 1; i < n; ++i){
RG int u = Input(), v = Input(), w = Input();
Add(u, v, w), Add(v, u, w);
}
mx[0] = n + 1, GetRoot(1, 0), Solve(root);
printf("%d\n", ans);
return 0;
}

SPOJ1825:Free tour II的更多相关文章

  1. SPOJ1825/FTOUR2:Free tour II——包看得懂/看不懂题解

    http://www.spoj.com/problems/FTOUR2/en/ 题目大意:给一棵黑白染色的树,求边权和最大且经过黑点不超过K的路径. ———————————————————— 前排膜拜 ...

  2. SPOJ1825 FTOUR2 - Free tour II

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

  3. 【SPOJ1825】Free tour II (点分治,启发式)

    题意: 边权可能为负 思路: 感觉我自己写的还是太过僵硬了,可以灵活一点,比如可以多写几个不同的dfs求出不同的信息,而不是压到同一个dfs里 #include<cstdio> #incl ...

  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 (树的点分治)

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

  6. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

  7. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

  8. FPGA回忆记事(一):基于Nios II的LED实验

    实验一:基于Nios II的LED实验 一.    创建Quartus II工程 1.打开Quartus II环境.开始->程序->Altera->Quartus II 9.1. 2 ...

  9. 1436:数列分段II

    1436:数列分段II 题解 二分答案 我们最终答案的取值区间是[  max(a[i])  ,   ∑a[i]  ] 设定 l=max(a[i]) , r=∑a[i]  , mid不断二分 mid表示 ...

随机推荐

  1. APACHE服务器出现No input file specified.解决方案

    thinkcmf程序默认的.htaccess里面的规则: <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_F ...

  2. 置换群、Burnside引理与等价类计数问题

    置换群.Burnside引理与等价类计数问题 标签: 置换群 Burnside引理 置换 说说我对置换的理解,其实就是把一个排列变成另外一个排列.简单来说就是一一映射.而置换群就是置换的集合. 比如\ ...

  3. 导入sass文件

    4导入sass文件 sass的@import规则在生成css文件时就把相关文件导入进来.这意味着所有相关的样式被归纳到了同一个css文件中,而无需发起额外的下载请求. 1 sass局部文件的文件名以下 ...

  4. hibhibernate中hql中的语句where语句查询List出现空

    1.java.sql.Date 与 java.util.Date java.sql.Date是从java.util.Date中继承而来 假设 dates1(java.sql.Date)要赋值给date ...

  5. Delphi 添加外部Form单元的方法!

    我用到的环境是 RAD Studio 10.2.2 有时候,需要把某个Form单元  添加到其他的工程!  此时,如果直接添加或者拖拉 .pas单元到目标工程,是无法把.pas包含的Form添加进去的 ...

  6. Linux Centos下编译安装Redis

    需要安装 tcl 8.5 wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz //直接下载 sudo tar xzvf tcl8 ...

  7. Office 365 共享链接直接进入编辑

    首先在Word online共享文档(不多赘述) 但这个链接打开的是预览视图,要点击右上角的"在浏览器中编辑"才能真正编辑. 但是很多情况都是没必要进入这个预览界面再编辑的.这多点 ...

  8. SmileyFace——基于OpenCV的人脸人眼检测、面部识别程序

    项目地址 https://github.com/guoyaohua/SmileyFace 开发环境 Visual Studio 2010 MFC + OpenCV 功能描述 静态图像人脸检测 视频人脸 ...

  9. BZOJ2820 - 巧克力王国

    原题链接 Description 给出个二维平面上的点,第个点为,权值为.接下来次询问,给出,求所有满足的点的权值和. Solution 对于这个点建一棵k-d树,子树维护一个子树和. 如果子树所代表 ...

  10. OpenCV 之 空间滤波

    1  空间滤波 1.1  基本概念 空间域,在图像处理中,指的是像平面本身: 空间滤波,则是在像平面内,对像素值所进行的滤波处理. 如上图所示,假设点 (x, y) 为图像 f 中的任意点,中间正方形 ...