Codeforces Round #328 (Div. 2) D. Super M
题目链接:
http://codeforces.com/contest/592/problem/D
题意:
给你一颗树,树上有一些必须访问的节点,你可以任选一个起点,依次访问所有的必须访问的节点,使总路程最短。
题解:
由于是树,任意两点间路径唯一,是确定的。
首先我们要先建一颗树:包括所有必须访问的节点,已经它们之间的路径。
我们选的起点一定是某个必须访问的节点,如果我们把所有的必须访问的节点访问一遍并且回到起点,那么我们用的最小花费就是边数*2(这个可以用反证法证明),所以只要我们找出新树的直径,答案就是边数*2-直径。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std; const int maxn = 2e5 + ;
const int INF = 0x3f3f3f3f; int n, m;
vector<int> G[maxn];
int ans,ans1,ans2,dis;
bool tag[maxn]; //两次dfs求最远顶点对
void dfs(int u,int fa,int d,int& res) {
if (dis < d&&tag[u]) {
dis = max(dis, d);
res = u;
}
if (dis == d&&tag[u]) res = min(res, u);
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i];
if (v == fa) continue;
dfs(v, u, d + , res);
}
} bool used[maxn];
int _cnt;
//求虚拟树的节点数
bool dfs2(int u, int fa) {
if (tag[u]) used[u]=;
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i];
if (v == fa) continue;
used[u] |= dfs2(v, u);
}
if (used[u]) _cnt++;
return used[u];
} int main() {
memset(tag, , sizeof(tag));
scanf("%d%d", &n, &m);
for (int i = ; i < n - ; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
ans = INF;
for (int i = ; i < m; i++) {
int v;
scanf("%d", &v);
tag[v] = ;
ans = min(ans, v);
}
if (m == ) {
printf("%d\n%d\n", ans, );
return ;
}
dis = -, ans1 = INF;
dfs(ans, -, ,ans1);
dis = -, ans2 = INF;
dfs(ans1, -, , ans2); memset(used, , sizeof(used));
_cnt = ;
dfs2(ans1, -);
int res = (_cnt - ) * - dis;
printf("%d\n%d\n", min(ans1, ans2), res);
return ;
}
Codeforces Round #328 (Div. 2) D. Super M的更多相关文章
- 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 ...
- Codeforces Round #328 (Div. 2)
这场CF,准备充足,回寝室洗了澡,睡了一觉,可结果... 水 A - PawnChess 第一次忘记判断相等时A先走算A赢,hack掉.后来才知道自己的代码写错了(摔 for (int i=1; ...
- Codeforces Round #328 (Div. 2) C. The Big Race 数学.lcm
C. The Big Race Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/592/probl ...
- Codeforces Round #328 (Div. 2) B. The Monster and the Squirrel 打表数学
B. The Monster and the Squirrel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/c ...
- Codeforces Round #328 (Div. 2) A. PawnChess 暴力
A. PawnChess Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/592/problem/ ...
- Codeforces Round #328 (Div. 2)_B. The Monster and the Squirrel
B. The Monster and the Squirrel time limit per test 1 second memory limit per test 256 megabytes inp ...
- Codeforces Round #328 (Div. 2)_A. PawnChess
A. PawnChess time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #328 (Div. 2) A
A. PawnChess time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #328 (Div. 2) C 数学
C. The Big Race time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
随机推荐
- SQL SERVER 2008 R2 错误代码 17000 - 17999
错误 严重性 是否记录事件 说明(消息正文) 17000 10 否 用法: sp_autostats <table_name> [, {ON|OFF} [, <index_name& ...
- WCF学习笔记(2)——使用IIS承载WCF服务
通过前面的笔记我们知道WCF服务是不能独立存在,必须“寄宿”于其他的应用程序中,承载WCF服务的应用程序我们称之为“宿主”.WCF的多种可选宿主,其中比较常见的就是承载于IIS服务中,在这里我们来学习 ...
- tar命令: 对某目录文件打tar包时,排除指定的目录或文件
如某当前目录存在以下文件或目录: 1.txt2.txt3.txtdir1dir2my2015.tarmy2016.tar 若要对当前目录除1.txt 和dir1.tar外,打包tar 步骤一.建立e ...
- 深入探析koa之异步回调处理篇
在上一篇中我们梳理了koa当中中间件的洋葱模型执行原理,并实现了一个可以让洋葱模型自动跑起来的流程管理函数.这一篇,我们再来研究一下koa当中异步回调同步化写法的原理,同样的,我们也会实现一个管理函数 ...
- hdu 2602 Bone Collector 背包入门题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 题目分析:0-1背包 注意dp数组的清空, 二维转化为一维后的公式变化 /*Bone Coll ...
- 1 . Robberies (hdu 2955)
The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually g ...
- mysql快速上手3
上一章给大家说的是数据库的视图,存储过程等等操作,这章主要讲索引,以及索引注意事项,如果想看前面的文章,url如下: mysql快速上手1 mysql快速上手2 索引简介 索引是对数据库表中一个或多个 ...
- System Generator入门笔记
System Generator入门笔记 [CPLD/FPGA] 发布时间:2010-04-08 23:02:09 System Generator是Xilinx公司进行数字信号处理开发的一种设计 ...
- SecureCRT远程控制ubuntu
如果你拥有两台电脑一台是ubuntu,另一台是笔记本电脑,而你又想在远程控制你的ubuntu,那么SecureCRT就可以用了. 1:首先在你的ubuntu电脑上安装SSH服务 :apt-get i ...
- asp.net mvc常用的数据注解和验证以及entity framework数据映射
终于有时间整理一下asp.net mvc 和 entity framework 方面的素材了. 闲话少说,步入正题: 下面是model层的管理员信息表,也是大伙比较常用到的,看看下面的代码大伙应该不会 ...