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 ...
随机推荐
- Java之累加和
所谓累加算法,就是数学中数列求的算法,这都是司空见惯了的.下面我们用java求: package com.cdp.leijiahe; import java.util.Scanner; public ...
- .NET判断某一年的所有放假的日期
由于工作需求写的一个程序,判断某一年所有的放假日期,根据国家的法定假日和补休日期进行的判断. protected void Button1_Click(object sender, EventArgs ...
- 转:C#写的WEB服务器
转:http://www.cnblogs.com/x369/articles/79245.html 这只是一个简单的用C#写的WEB服务器,只实现了get方式的对html文件的请求,有兴趣的朋友可以在 ...
- JQuery、js判断复选框是否选中状态
JQuery: var $isChecked = $("#id").is(":checked"); alert($isChecked); JS: var $id ...
- .NET打印功能实现 PrintDocument
//打印按钮 private void btnPrint_Click(object sender, EventArgs e) { if (this.printDialog1.ShowDialog() ...
- 20141009---Visual Studio 2012 预定义数据类型
预定义数据类型 一.值类型 整型:(整数) 有符号整型和无符号整形,区别是有符号的有负数无符号的都是正数, 2x+1 常用int 有符号: 带有正负数,范围为按所写依次增大 ...
- 【风马一族_Java】在某个范围内,找出具有水仙花特征的数字
打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如: 153是一个"水仙花数",因为153=1的三 ...
- PHP实现链式操作的原理
在一个类中有多个方法,当你实例化这个类,并调用方法时只能一个一个调用,类似: db.php <?php class db{ public function where() { //code he ...
- iOS实现图片的缩放和居中显示
直接上代码 // // MoveScaleImageController.h // MoveScaleImage // // Created by on 12-4-24. // Copyright ( ...
- Eclipse导入android包错误
错误提示:Invalid project description… 解决方案:假设你的工作空间是workshop,那么你可以在你的workshop下新建一个文件夹,然后放入你的包,再在Eclipse中 ...