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 ...
随机推荐
- (转)Android之常用功能方法大集合
这些,都是Andorid中比较常用的方法和功能,在网上搜集整理一下记录之,以备不时之需.由于经过多次转载,源文作者不确凿,在此申明,敬请见谅.不得不赞,非常实用. 1.判断sd卡是否存在 boolea ...
- Android之开源项目view篇
本文转自:http://www.trinea.cn/android/android-open-source-projects-view/ 主要介绍Android上那些不错个性化的View,包括List ...
- C#中Messagebox.Show()常用参数用法详解
声明:IWin32Window owner , HelpNavigator navigator , string keyword 上面的三个参数类型不是很了解.没有做讨论. 等以后了解多了 ...
- SERVER 2012 R2 core域环境下批量创建用户
Write by xiaoyang 转载请注明出处 步骤一:创建域 基本配置 1. 输入命令进入配置 2. 输入8进入网络配置 3. 选择要配置的网 ...
- iOS开发那些事-iOS应用本地化-资源文件本地化
资源文件包括:图片文件.音频文件以及前文提到的Localizable.strings等文件,它们的特点是都是随着应用一起打包发布.但就本地化而言无论是图片文件还是音频文件都必须实现的步骤都是类似的,因 ...
- css笔记——关于css中写上charset “utf-8”
当css文件中写上 charset "utf-8" 时需要将body和html的样式分开写 例如: html,body{margin:0;padding:0;font-family ...
- 自定义textbox加入左右晃动效果
应用开发过程中经常会要求用户在textbox进行输入.例如:登陆,发布. 而一般没进行输入的时候我们都会简单的进行弹窗提示用户输入. 前阵子ios的同学搞了一个左右晃动的效果,觉得还不错,于是也搞了个 ...
- Linux C 程序 线程(18)
线程控制 1.线程与进程的关系 线程:计算机中独立运行的最小单位. 在用户角度:多个线程是同时执行的. 操作系统角度:各个线程交替执行 以上只针对单核CPU的情况 在多核CPU主机上,多个线程可以同时 ...
- js设计模式(10)---观察者模式
0.前言 最近好多烦心事,由于自己的拖延懒惰造成事情堆积如山,看来最近得勤快些了,不然真的会死的很惨. 1.观察者模式是什么 又叫做发布者订阅者模式(publish/Subscribe),用来确定对象 ...
- C# 网卡IP(网上资料整理)
//设置对外访问所使用网卡的IP string sendingIp = "192.168.0.1"; //设置对外访问所使用的端口 ; Uri uri = new Uri(&quo ...