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 ...
随机推荐
- php连接到数据库操作
<?php $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { ?> 要写的内容代码,比如说Ht ...
- vlan知识
为什么需要VLAN 1. 什么是VLAN? VLAN(Virtual LAN),翻译成中文是“虚拟局域网”.LAN可以是由少数几台家用计算机构成的网络,也可以是数以百计的计算机构成的企业网络.VLAN ...
- 杭电ACM2092--整数解
杭电ACM2092--整数解 分析 http://acm.hdu.edu.cn/showproblem.php?pid=2092 一个YES,一个Yes.试了10几次..我也是无语了..哪里都不 ...
- GCD Block
GCD (Grand Central Dispatch) 是Apple公司开发的一种技术,它旨在优化多核环境中的并发操作并取代传统多线程的编程模式. 在Mac OS X 10.6和IOS 4.0之后开 ...
- 4.Servlet_Form表单处理
1.建项目"3Servlet_Form",src下建包“com.amaker.servlet”,web-root下建Register.html <!DOCTYPE html& ...
- shell脚本初识
#!/bin/bash(linux脚本环境的声明即解释器,该解释器为bash,位于根目录下的bin目录下) 变量的定义与赋值: 格式:变量名=变量值(无需声明变量类型) 变量的引用: 格式:$变量名 ...
- javascript中for-in的用法
for(var 变量名 in object) alert(变量名[第n个]) : 如果object是josn对象的话,变量名就是属性名 如果object是数组的话,变量名就是数字下标 例子:JOSN对 ...
- OpenSUSE 安装并启动Tomcat
安装.用YsST2搜索tomcat安装就好了.不过版本肯定不如官网的新,笔者用的是7.0.42,最新的已经到8.X了. 启动.在服务管理器中找到tomcat,启动.
- SOAP 介绍
简介 SOAP(Simple Object Access Protoco)简单对象访问协议是在分散或分布式的环境中交换信息的简单的协议,是一个基于 XML 的协议.此协议规范由 IBM.Microso ...
- 加载页面遮挡耗时操作任务页面--第三方开源--AndroidProgressLayout
在Android的开发中,往往有这种需求,比如一个耗时的操作,联网获取网络图片.内容,数据库耗时读写等等,在此耗时操作过程中,开发者也许不希望用户再进行其他操作(其他操作可能会引起逻辑混乱),而此时需 ...