CodeForces 592D Super M
先把没用的边去掉,求出包含m个点的最小树。然后求出最小树的直径就可以得到答案了。
#include <cstdio>
#include <cstring>
#include <vector>
#include<cmath>
using namespace std; const int maxn=;
struct Edge
{
int u,v;
int next;
} e[*maxn];
int tot;
int h[maxn];
bool f[maxn],q[maxn];
int head[maxn];
int n,m; int st1,st2;
int len; void add(int a,int b)
{
e[tot].u=a;
e[tot].v=b;
e[tot].next=head[a];
head[a]=tot;
tot++;
} void dfs(int x)
{
f[x]=;
for(int i=head[x]; i!=-; i=e[i].next)
{
if(f[e[i].v]==)
{
dfs(e[i].v);
if(q[e[i].v]==) q[x]=;
}
}
} void Find(int x,int le,int op)
{
f[x]=;
if(op==)
{
if(le>len) len=le,st1=x;
else if(le==len&&x<st1) st1=x;
}
else if(op==)
{
if(le>len) len=le,st2=x;
else if(le==len&&x<st2) st2=x;
} for(int i=head[x]; i!=-; i=e[i].next)
{
if(q[e[i].u]==) continue;
if(q[e[i].v]==) continue;
if(f[e[i].v]==) Find(e[i].v,le+,op);
}
} int main()
{
scanf("%d%d",&n,&m);
memset(f,,sizeof f);
memset(q,,sizeof q);
memset(head,-,sizeof head);
tot=;
for(int i=; i<n; i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
for(int i=; i<=m; i++)
{
int x;
scanf("%d",&x);
q[x]=;
}
int g;
for(int i=; i<=n; i++) if(q[i]==)
{
g=i;
dfs(g);
break;
} st1=st2=0x7FFFFFFF;
memset(f,,sizeof f);
len=;
Find(g,,);
memset(f,,sizeof f);
len=;
Find(st1,,); int ans1=min(st1,st2); int ans2=;
for(int i=; i<tot; i=i+)
{
if(q[e[i].u]==) continue;
if(q[e[i].v]==) continue;
ans2=ans2+;
}
ans2=ans2-len;
printf("%d\n%d\n",ans1,ans2);
return ;
}
CodeForces 592D Super M的更多相关文章
- 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 DP
Super M 题解: 定义 dp[u][0] 为遍历完u中的所有节点, 但不回到u点的路径花费值. 定义 dp[u][1] 为遍历完u中的所有节点, 且要回到u点的路径花费值. 转移方程. dp[u ...
- CodeForces - 592D Super M 题解
题目大意: 一棵树 n个点 有m个点被标记 求经过所有被标记的点的最短路径的长度以及起点(如有多条输出编号最小的起点). 思路: 1.当且仅当一个点本身或其子树中有点被标记时该点在最短的路径上因此,可 ...
- 【27.66%】【codeforces 592D】Super M
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 算法笔记--树的直径 && 树形dp && 虚树 && 树分治 && 树上差分 && 树链剖分
树的直径: 利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点. 先从任意一顶点a出发,bfs找到离它最远的一个叶子顶点b,然后再从b出发bfs找到离b最远的顶点c,那么b和c ...
- codeforces 几道题目
BZOJ挂了....明天就要出发去GDKOI了....不能弃疗. 于是在cf水了几道题, 写写详(jian)细(dan)题解, 攒攒RP, 希望GDKOI能好好发挥....... 620E. New ...
- CodeForces 219B Special Offer! Super Price 999 Bourles!
Special Offer! Super Price 999 Bourles! Time Limit:1000MS Memory Limit:262144KB 64bit IO For ...
- Codeforces Round #328 (Div. 2) D. Super M 虚树直径
D. Super M Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/592/problem/D ...
随机推荐
- 1 开发一个注重性能的JDBC应用程序不是一件容易的事. 当你的代码运行很慢的时候JDBC驱动程序并不会抛出异常告诉你。 本系列的性能提示将为改善JDBC应用程序的性能介绍一些基本的指导原则,这其中的原则已经被许多现有的JDBC应用程序编译运行并验证过。 这些指导原则包括: 正确的使用数据库MetaData方法 只获取需要的数据 选用最佳性能的功能 管理连
1 开发一个注重性能的JDBC应用程序不是一件容易的事. 当你的代码运行很慢的时候JDBC驱动程序并不会抛出异常告诉你. 本系列的性能提示将为改善JDBC应用程序的性能介绍一些基本的指导原则,这其中的 ...
- java环境变量配置-简易菜鸟版
NO1:Path配置: 用户Path:%JAVA_HOME%\bin 系统Path:%java_home%\bin;%java_home%\jre\bin NO2:CLASSPATH配置: .;%ja ...
- while循环(break、continue)
while循环 流程:判断条件是否为真,如果条件为真,执行代码块,然后再次判断条件是否为真,如果为真,执行代码块,直到条件判断为假,结束循环 格式 while 条件: 代码块(循环体) else:- ...
- Ioc 之 Unity的AOP功能
前面我们介绍了Unity的依赖注入功能,现在来介绍下Unity的AOP功能.AOP是面向切面编程,它能够使我们在不改变现有代码结构的情况下额外的为其添加一些功能. 我们还是使用配置文件来对类型进行注入 ...
- 和为S
2518 和为S 2 秒 262,144 KB 10 分 2 级题 小b有一个01序列A,她想知道A有多少个非空连续子序列和为S. 你能帮帮她吗? 收起 输入 第一行输入一个数n,表示A的长度 ...
- delphi中使用自定义资源的方法
如果要在delphi中使用自定义资源文件*.res文件,比如一个光标,此时可以采用下列步骤: 1,创建包含相应的资源文件,这里是创建一个包含自定义光标的res文件. 2,在主窗体的pas文件中加入编译 ...
- encodeURI()与decodeURI()等转码方法
只针对文本编码 encodeURI() 只针对文本解码 decodeURI()针对文本和特殊字符的编码 encodeURIComponent()针对文本和特殊字符的解码 decodeURIComp ...
- 360 Atlas中间件安装及使用
1.下载Atlas wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm ...
- NTP服务和DNS服务
1.NTP时间服务器 作用:NTP主要用于对计算机的时间同步管理操作 1.1 NTP部署 服务端:192.168.16.6 客户端:192.168.16.7 [root@localhost ~]# ...
- python 简易计算器(只能计算加减乘除和括号)
import re # 格式化字符串函数(消除一些错误的格式) def format_string(string): # 一系列的替换语句 string = string.replace(" ...