题目大意:

  一棵树 n个点 有m个点被标记 求经过所有被标记的点的最短路径的长度以及起点(如有多条输出编号最小的起点)。

思路:

  1.当且仅当一个点本身或其子树中有点被标记时该点在最短的路径上因此,可以将多余的点删去,得到新的一棵树。
  2.不难发现,新树上的边必定被经过一次或两次,而且当只经过一次的边的集合为树的直径时,路径最短。

反思:

  “如有多条输出编号最小的起点”是个坑点,要在最远的点中找出编号最小的当端点。

代码:

 #include<cstdio>
#define u v[i]
const int M=;
int s,a[M],v[M],hea[M],nex[M];
bool b[M]; int read()
{
int x=; char ch=getchar();
while (ch< || ch>) ch=getchar();
while (ch> && ch<) x=(x<<)+(x<<)+ch-,ch=getchar();
return x;
} void add(int x,int y) { v[++s]=y,nex[s]=hea[x],hea[x]=s; } void dfs(int x,int p)
{
for (int i=hea[x];i;i=nex[i])
if (u^p) dfs(u,x),b[x]|=b[u];
} void DFS(int x,int y,int p)
{
a[x]=y;
for (int i=hea[x];i;i=nex[i])
if (b[u] && u^p) DFS(u,y+,x);
} int main()
{
int n=read(),m=read(),i,x,y;
for (i=;i<n;++i) x=read(),y=read(),add(x,y),add(y,x);
for (i=b[y=read()]=;i<m;++i) b[read()]=;
for (dfs(y,),a[]=s=-,i=;i<=n;++i) s=s+b[i];
for (DFS(y,x=,),i=;i<=n;++i)
if (b[i] && a[x]<a[i]) x=i;
for (DFS(x,y=,),i=;i<=n;++i)
if (b[i] && a[y]<a[i]) y=i;
printf("%d\n%d\n",x<y?x:y,s+s-a[y]);
return ;
}

CodeForces - 592D Super M 题解的更多相关文章

  1. CodeForces 592D Super M DP

    Super M 题解: 定义 dp[u][0] 为遍历完u中的所有节点, 但不回到u点的路径花费值. 定义 dp[u][1] 为遍历完u中的所有节点, 且要回到u点的路径花费值. 转移方程. dp[u ...

  2. Codeforces 592D - Super M - [树的直径][DFS]

    Time limit 2000 ms Memory limit 262144 kB Source Codeforces Round #328 (Div. 2) Ari the monster is n ...

  3. CodeForces - 592D: Super M(虚树+树的直径)

    Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ s ...

  4. CodeForces 592D Super M

    先把没用的边去掉,求出包含m个点的最小树.然后求出最小树的直径就可以得到答案了. #include <cstdio> #include <cstring> #include & ...

  5. Codeforces Round #543 Div1题解(并不全)

    Codeforces Round #543 Div1题解 Codeforces A. Diana and Liana 给定一个长度为\(m\)的序列,你可以从中删去不超过\(m-n*k\)个元素,剩下 ...

  6. Codeforces Round #545 Div1 题解

    Codeforces Round #545 Div1 题解 来写题解啦QwQ 本来想上红的,结果没做出D.... A. Skyscrapers CF1137A 题意 给定一个\(n*m\)的网格,每个 ...

  7. Codeforces Round #539 Div1 题解

    Codeforces Round #539 Div1 题解 听说这场很适合上分QwQ 然而太晚了QaQ A. Sasha and a Bit of Relax 翻译 有一个长度为\(n\)的数组,问有 ...

  8. [Codeforces Round #461 (Div2)] 题解

    [比赛链接] http://codeforces.com/contest/922 [题解] Problem A. Cloning Toys          [算法] 当y = 0 ,   不可以 当 ...

  9. Codeforces 7E - Defining Macros 题解

    目录 Codeforces 7E - Defining Macros 题解 前言 做法 程序 结尾 Codeforces 7E - Defining Macros 题解 前言 开始使用博客园了,很想写 ...

随机推荐

  1. 480 Sliding Window Median 滑动窗口中位数

    详见:https://leetcode.com/problems/sliding-window-median/description/ C++: class Solution { public: ve ...

  2. Oozie的作用

    Oozie的作用: 1.统一调度hadoop系统中常见的mr任务启动. hdfs操作. shell调度. hive操作等 2.使得复杂的依赖关系.时间触发.事件触发使用xml语言进行表达,开发效率提高 ...

  3. 代码文件导到word里

    # 创建并写入word文档 import docx import sys import os dir = "D:\\gitwork\\fm.qimeng.c-class\\src\\main ...

  4. Spring中@Value的使用

  5. qt5.8+vs2015使用Qt5WebEngine搭建环境

    转载请注明出处:http://www.cnblogs.com/dachen408/p/7575094.html 1.项目属性,C/C++,所有选项,附加包含目录新增. $(QTDIR)\include ...

  6. 介绍三款大前端UI框架

    一.蚂蚁金服团队推出的基于React antd (全名:ant.design) 友情跳链:https://ant.design/index-cn:使用antd模板:https://pro.ant.de ...

  7. System.currentTimeMillis()与日期之间的相互转换

    System.currentTimeMillis()与日期 之间是可以相互转换的,大多数Android开发者都知道 通过 SimpleDateFormat dateformat = new Simpl ...

  8. 作为一个iOS Developer 为什么我不用Swift?

    1.开始 在去年这个时候接手了一个iOS项目,项目主用Swift语言进行开发,对于部分第三方Objective C开源库则使用bridge的方式进行调用 当时项目的规模大概是不超过15个页面,功能也比 ...

  9. MySQL存储过程实现分页及变量的定义

    delimiter是MySQL中的命令,这个命令与存储过程没什么关系. 其实就是告诉mysql解释器,该段命令是否已经结束了,mysql是否可以执行了. 即改变输入结束符. 默认情况下,delimit ...

  10. import downloadjs from 'downloadjs' 如果是自己写的函数 没用默认导出 记得加花括号 例如 import { download } from './data.js'

    import downloadjs from 'downloadjs' 如果是自己写的函数 没用默认导出 记得加花括号 例如 import { download } from './data.js'