CodeForces - 592D Super M 题解
题目大意:
一棵树 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 题解的更多相关文章
- CodeForces 592D Super M DP
Super M 题解: 定义 dp[u][0] 为遍历完u中的所有节点, 但不回到u点的路径花费值. 定义 dp[u][1] 为遍历完u中的所有节点, 且要回到u点的路径花费值. 转移方程. dp[u ...
- Codeforces 592D - Super M - [树的直径][DFS]
Time limit 2000 ms Memory limit 262144 kB Source Codeforces Round #328 (Div. 2) Ari the monster is n ...
- CodeForces - 592D: Super M(虚树+树的直径)
Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ s ...
- CodeForces 592D Super M
先把没用的边去掉,求出包含m个点的最小树.然后求出最小树的直径就可以得到答案了. #include <cstdio> #include <cstring> #include & ...
- Codeforces Round #543 Div1题解(并不全)
Codeforces Round #543 Div1题解 Codeforces A. Diana and Liana 给定一个长度为\(m\)的序列,你可以从中删去不超过\(m-n*k\)个元素,剩下 ...
- Codeforces Round #545 Div1 题解
Codeforces Round #545 Div1 题解 来写题解啦QwQ 本来想上红的,结果没做出D.... A. Skyscrapers CF1137A 题意 给定一个\(n*m\)的网格,每个 ...
- Codeforces Round #539 Div1 题解
Codeforces Round #539 Div1 题解 听说这场很适合上分QwQ 然而太晚了QaQ A. Sasha and a Bit of Relax 翻译 有一个长度为\(n\)的数组,问有 ...
- [Codeforces Round #461 (Div2)] 题解
[比赛链接] http://codeforces.com/contest/922 [题解] Problem A. Cloning Toys [算法] 当y = 0 , 不可以 当 ...
- Codeforces 7E - Defining Macros 题解
目录 Codeforces 7E - Defining Macros 题解 前言 做法 程序 结尾 Codeforces 7E - Defining Macros 题解 前言 开始使用博客园了,很想写 ...
随机推荐
- [已读]web性能实践日志
书是在今年5月份出版,但是书中的内容是发表于11年到12年之间的,如果知道这一点,我一定不会买~ 列举一下大致内容: YSlow localStroage读取数据最佳策略 性能优化各种策略(图片精灵 ...
- AJPFX总结List的三个子类的特点
ArrayList: 底层数据结构是数组,查询快,增删慢. 线程不安全,效率高. ...
- html调用js提示方法名 is not defined处理方法
解决办法(方法名 is not defined): dosave=function(){ alert("方法名在前"); } 下面这种写法有时候会出现错误: function do ...
- Qt窗口-仅显示关闭按钮
环境: Qt5.3.1, mac os x 10.10.1 setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowH ...
- android 图片叠加效果——两种方法的简介与内容 ,带解决Immutable bitmap passed to Canvas constructor错误
第一种是通过canvas画出来的效果: public void first(View v) { // 防止出现Immutable bitmap passed to Canvas constructor ...
- [Github筆記] 清除所有 Commit 紀錄
# 把原來的 git 移除掉 sudo rm .git -r # 初始化 git init git remote add origin https://github.com/username/repo ...
- select2宽度占比100%,导致无法实现浮动效果
- find、filter、map的区别
1.find 查询数组中符合条件的第一个元素,如果没有符合条件的元素则返回空数组 ,,,,,,] ) var men=[ {name:',sex:'女'}, {name:',sex:'nan'}, { ...
- 天梯赛L1 题解
L1-001 Hello World (5 分) 这道超级简单的题目没有任何输入. 你只需要在一行中输出著名短句“Hello World!”就可以了. AC代码:(直接输出记性) #include & ...
- [JOYOI] 1415 西瓜种植
题目描述 笨笨种了一块西瓜地,但这块西瓜地的种植范围是一条直线的-- 笨笨在一番研究过后,得出了m个结论,这m个结论可以使他收获的西瓜最多. 笨笨的结论是这样的: 从西瓜地B处到E处至少要种植T个西瓜 ...