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 ...
随机推荐
- B树、B+树、红黑树、AVL树
定义及概念 B树 二叉树的深度较大,在查找时会造成I/O读写频繁,查询效率低下,所以引入了多叉树的结构,也就是B树.阶为M的B树具有以下性质: 1.根节点在不为叶子节点的情况下儿子数为 2 ~ M2. ...
- 使用libsvm实现文本分类
@Hcy(黄灿奕) 文本分类,首先它是分类问题,应该对应着分类过程的两个重要的步骤,一个是使用训练数据集训练分类器,另一个就是使用测试数据集来评价分类器的分类精度.然而,作为文本分类,它还具有文本这样 ...
- 自定义Jquery分页插件
/** * 功能说明:jPager 分页插件 * 参数说明:pages:[] 分页的控件个数 @id:显示分页的div ID,@showSelectPage: 是否显示当前分页的条目过滤下拉框 * @ ...
- H3C S5024P交换机互连实验
第一次周二网络管理实验报告 交换机互联实验 实验接线图: 交换机全貌: 可以通过超级终端和telnet来配置交换机 控制电缆连交换机console口与计算机主机(只可以传送命令不可以通信, ...
- SQLite -创建数据库
SQLite -创建数据库 SQLite sqlite3命令用于创建新的SQLite数据库.你不需要有任何特权来创建一个数据库. 语法: sqlite3命令的基本语法如下: $sqlite3 Data ...
- 迅为4412开发平台Zigbee模块在物联网智能家居中的应用
物联网智能家居的发展物联网随着互联网的发展,可以通过互联网实现物和物的互联,就有了物联网的概念.传统家居电器 有了物联网之后,在家居电器范围中,就是通过物联网技术将家中的各种设备连接到一起,家居中 ...
- CentOS 7上修改主机名
如何在CentOS 7上修改主机名 在Cent ...
- 页面定制CSS代码
博客皮肤:SimpleMemory .catListTitle { margin-top: 21px; margin-bottom: 10.5px; text-align: left; border- ...
- Node + Express + MySQL 接口开发完整案例
https://blog.csdn.net/u013216976/article/details/85273770 https://github.com/Apache-Ra/node-express- ...
- 找回Settings Sync的gist id和token
方法一:如果你本地有缓存参考:https://www.cnblogs.com/zhang1028/p/9514471.html 方法二:如果你电脑重装系统了 1.找回gist id 登陆你的githu ...