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 ...
随机推荐
- 收藏的js学习小例子
1.js模拟java里的Map function Map(){ var obj = {} ; this.put = function(key , value){ obj[key] = value ; ...
- windows下mysql增量备份与全备份批处理
win下的全备批处理 批处理用于游戏服务器,经过严格测试,且正式使用,主要用来完全备份数据库,当然.这只是将数备份出来 ,至于如何将备份出来的数据远程传送的远程服务器上可以调用ftp的功能,此脚本并未 ...
- MVC自定义错误页404静态页
昨天公司要求给所有项目添加自定义404错误页,具体的要求实现的有以下几点: 1.实现自定义错误(如各种error,404等)跳转到指定的页面 2.所指定的页面输出的http状态值必须是404或其他指定 ...
- highcharts 图表库的简单使用
Highcharts简介: Highcharts是一款纯javascript编写的图表库,能够很简单便捷的在Web网站或Web应用中添加交互性的图表,Highcharts目前支持直线图.曲线图.面积图 ...
- UINavigationController与UITabbarController的样式
之前虽然也手写过这两中视图控制器,但是更多的还是使用SB来创建,最近发现了一些问题,现在总结一下. 1.改变UINavigationBar的颜色 在UINavigationController中,之前 ...
- js、expression表达式解析
首先理解一下下面的表达式:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeigh ...
- startup.bat闪退---tomcat环境变量配置中遇到的问题
常用的方法: 1.在已解压的tomcat的bin文件夹下找到startup.bat,右击->编辑.在文件头加入下面两行: SET JAVA_HOME=D:\Java\jdk1.6.0_10 ...
- 一些常用的字符串hash函数
unsigned int RSHash(const std::string& str) { unsigned int b = 378551; unsigned int a = 63689; u ...
- zedboard - 轻量级以太网控制器LWIP
ipconfig/all route print 显示本机所有的网络 网关是什么 那么网关到底是什么呢?网关实质上是一个网络通向其他网络的IP地址.比如有网络A和网络B,网络A的IP地址范围为&qu ...
- php关联不上mysql解决办法
## php无法关联mysql php关联不上mysql解决办法: 尝试N多方法,需要的dll文件都复制了,依旧是不断提示: Fatal error: Call to undefined fun ...