题意:给出一个无根树,点数为10^5,所有边的长度为1。给定其中有一些点是受到攻击的。

现在要求一个人选定一个点作为起点,走遍所有的受攻击点(不用再回到起点)。

需要的最短距离是多少,选定的起点是哪个。

分析:这是一个求树直径的变形题,原版请看这里

求树的直径即树中最远的点对,需要进行两次bfs或者dfs。这里我们用的是dfs。

首先我们假设所有点都受到攻击,那么要走完所有点再回到起点,路程L就是树上所有边的长度和乘以2。

不用回到起点遍历所有点的最短距离就是用这个L减去树上的最远两点之间的距离。

我们要把起点选在最远点对中的点才能保证所得解最小。

但本题中不是所有点都受到攻击,所以在求最远点对的时候要进行一些改变。要找最远的受攻击点对。

首先要以一个受攻击点为根(这个是本题最大难点,不太容易想到),进行第一次dfs,找到最深的受攻击点之后,再以它为根找最深的受攻击点。(注意长度相同时,找编号最小的)

再求出以任意受攻击点为根,遍历所有受攻击点的路程和。两者相减即为解。

#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std; const int MAX_N = (int)(2e5) + ; int n, m;
int root;
vector<int> g[MAX_N];
bool attacked[MAX_N];
int depth[MAX_N];
long long path_len; void input()
{
scanf("%d%d", &n, &m);
for (int i = ; i < n - ; i++)
{
int a, b;
scanf("%d%d", &a, &b);
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
memset(attacked, , sizeof(attacked));
for (int i = ; i < m; i++)
{
int a;
scanf("%d", &a);
a--;
attacked[a] = true;
}
} void dfs(int u, int father)
{
for (int i = ; i < (int)g[u].size(); i++)
{
int v = g[u][i];
if (v == father)
continue;
depth[v] = depth[u] + ;
dfs(v, u);
}
} int find_next()
{
int ret = root;
for (int i = ; i < n; i++)
{
if (!attacked[i])
continue;
if (depth[i] > depth[ret] || (depth[i] == depth[ret] && i < ret))
{
ret = i;
}
}
return ret;
} bool cal_depth(int u, int father)
{
bool ret = false;
for (int i = ; i < (int)g[u].size(); i++)
{
int v = g[u][i];
if (v == father)
continue;
if (!cal_depth(v, u))
continue;
path_len += ;
ret = true;
}
return ret || attacked[u];
} int main()
{
input();
root = find(attacked, attacked + n, true) - attacked; depth[root] = ;
dfs(root, -);
root = find_next(); depth[root] = ;
dfs(root, -); int end = find_next();
int city = min(root, end);
path_len = ;
cal_depth(root, -);
printf("%d\n%I64d\n", city + , path_len - depth[end]);
return ;
}

cf592d的更多相关文章

  1. CF592D Super M

    嘟嘟嘟 首先这题虽然不是很难,但是黄题是不是有点过分了--好歹算个蓝题啊. 手玩样例得知,这哥们儿瞬移到的城市\(A\)一定是这些被攻击的城市构成的树的一个叶子,然后他经过的最后一个城市\(B\)和\ ...

  2. 树的直径-CF592D Super M

    给定一颗n个节点树,边权为1,树上有m个点被标记,问从树上一个点出发,经过所有被标记的点的最短路程(起终点自选).同时输出可能开始的编号最小的那个点.M<=N<=123456. 先想:如果 ...

随机推荐

  1. python 占位符 %s Format

    1.百分号方式 %[(name)][flags][width].[precision]typecode (name)      可选,用于选择指定的key flags          可选,可供选择 ...

  2. MVC下分页的自定义分页一种实现

    1.引言 在MVC开发中我们经常会对数据进行分页的展示.通过分页我们可以从服务端获取指定的数据来进行展示.这样既节约了数据库查询的时间也节约了网络传输的数据量.在MVC开发中使用的比较多的应该是MVC ...

  3. Yii 框架里数据库操作详解-[增加、查询、更新、删除的方法 'AR模式']

    public function getMinLimit () {        $sql = "...";        $result = yii::app()->db-& ...

  4. Linux服务器管理: 系统的进程管理后台进程的切换和相关命令

    1.把进程放入到后台: [root@localhost/]#tar -zcf etc.tar.gz /etc &           //这种方法是在后台运行的 [root@localhost ...

  5. hdu4952 Number Transformation (找规律)

    2014多校 第八题 1008 2014 Multi-University Training Contest 8 4952 Number Transformation Number Transform ...

  6. 利用WCF技术降低系统之间的耦合度

    为了降低本系统各个组件之间的耦合度,本系统将BLL层采用WCF技术发布为Web Service,以供UI层调用. 前面我们已经介绍过,为什么UI层不直接调用BLL层,而是要经过UI->Servi ...

  7. POJ 1850 Code

    组合数学.... Code Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7202 Accepted: 3361 Descrip ...

  8. 机器学习之Hash集合问题

    问题来源与七月学习之 (3.x线性代数与矩阵运算基础)

  9. 【C语言入门教程】2.9 小结

    本章介绍 C 语言的基本组成部分,数据类型.运算符 和 表达式 构成了 C 语言的语法,熟悉和掌握这些信息是学习 C 语言的必经之路.C 语言具备严谨的语法结构,任何细微的差错可导致程序无法通过编译, ...

  10. CentOS编译安装Apache 2.4.x时报错:configure: error: Bundled APR requested but not found at ./srclib/. Download and unpack the corresponding apr and apr-util packages to ./srclib/.

    先前按照这篇文章“CentOS6.x编译安装LAMP(2):编译安装 Apache2.2.22”去编译安装Apache2.2.x版本时,安装得挺顺利,今天换成Apache2.4.x版本,安装方法一样, ...