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 ...
随机推荐
- 【HEVC简介】DB-DeBlock Filter
参考论文:HEVC Deblocking Filter <HEVC标准介绍.HEVC帧间预测论文笔记>系列博客,目录见:http://www.cnblogs.com/DwyaneTalk/ ...
- 如何用Chrome自带的截屏功能截取超过一个屏幕的网页
提升程序员工作效率的工具/技巧推荐系列 推荐一个功能强大的文件搜索工具SearchMyFiles 介绍一个好用的免费流程图和UML绘制软件-Diagram Designer 介绍Windows任务管理 ...
- shift Alt + up(down) copy current line ! ctrl + j show the control # vscode key
shift Alt + up(down) copy current line ! ctrl + j show the control # vscode key
- vscode vue template 下 style 的样式自动提示 #bug 这个搞完vue语法esLint就又不好使了,ERR
网上都是 "*.vue": "vue",改成"*.vue": "html" 就ok了 "files.ass ...
- node节点的部署
master点赋予用户权限 [root@mast-1 k8s]# kubectl create clusterrolebinding kubelet-bootstrap \ > --cluste ...
- 剑指offer8 旋转数组的最小数字
一种错误写法: class Solution { public: int minNumberInRotateArray(vector<int> rotateArray) { int len ...
- ping ip
def ip_and_time(): """ get ip to ping from ip.txt then return two list , each ip that ...
- HTTP初步了解
HTTP协议简介 HTTP(超文本传输协议)是应用层上的一种客户端/服务端模型的通信协议,它由请求和响应构成,且是无状态的.(暂不介绍HTTP2) 协议 协议规定了通信双方必须遵循的数据传输格式,这样 ...
- Liskon替换原则
肯定有不少人跟我刚看到这项原则的时候一样,对这个原则的名字充满疑惑.其实原因就是这项原则最早是在1988年,由麻省理工学院的一位姓里的女士(Barbara Liskov)提出来的. 定义1:如果对每一 ...
- tab bar controller
下面记一下怎样通过代码的方式为选项卡添加视图. 1.创建一个基于Empty Application的项目 2.创建两个新类,基类选择UIViewController,勾选With XIB for us ...