题目链接:

http://codeforces.com/contest/592/problem/D

题意:

给你一颗树,树上有一些必须访问的节点,你可以任选一个起点,依次访问所有的必须访问的节点,使总路程最短。

题解:

由于是树,任意两点间路径唯一,是确定的。

首先我们要先建一颗树:包括所有必须访问的节点,已经它们之间的路径。

我们选的起点一定是某个必须访问的节点,如果我们把所有的必须访问的节点访问一遍并且回到起点,那么我们用的最小花费就是边数*2(这个可以用反证法证明),所以只要我们找出新树的直径,答案就是边数*2-直径。

(官方题解)

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<vector>
  5. #include<algorithm>
  6. using namespace std;
  7.  
  8. const int maxn = 2e5 + ;
  9. const int INF = 0x3f3f3f3f;
  10.  
  11. int n, m;
  12. vector<int> G[maxn];
  13. int ans,ans1,ans2,dis;
  14. bool tag[maxn];
  15.  
  16. //两次dfs求最远顶点对
  17. void dfs(int u,int fa,int d,int& res) {
  18. if (dis < d&&tag[u]) {
  19. dis = max(dis, d);
  20. res = u;
  21. }
  22. if (dis == d&&tag[u]) res = min(res, u);
  23. for (int i = ; i < G[u].size(); i++) {
  24. int v = G[u][i];
  25. if (v == fa) continue;
  26. dfs(v, u, d + , res);
  27. }
  28. }
  29.  
  30. bool used[maxn];
  31. int _cnt;
  32. //求虚拟树的节点数
  33. bool dfs2(int u, int fa) {
  34. if (tag[u]) used[u]=;
  35. for (int i = ; i < G[u].size(); i++) {
  36. int v = G[u][i];
  37. if (v == fa) continue;
  38. used[u] |= dfs2(v, u);
  39. }
  40. if (used[u]) _cnt++;
  41. return used[u];
  42. }
  43.  
  44. int main() {
  45. memset(tag, , sizeof(tag));
  46. scanf("%d%d", &n, &m);
  47. for (int i = ; i < n - ; i++) {
  48. int u, v;
  49. scanf("%d%d", &u, &v);
  50. G[u].push_back(v);
  51. G[v].push_back(u);
  52. }
  53. ans = INF;
  54. for (int i = ; i < m; i++) {
  55. int v;
  56. scanf("%d", &v);
  57. tag[v] = ;
  58. ans = min(ans, v);
  59. }
  60. if (m == ) {
  61. printf("%d\n%d\n", ans, );
  62. return ;
  63. }
  64. dis = -, ans1 = INF;
  65. dfs(ans, -, ,ans1);
  66. dis = -, ans2 = INF;
  67. dfs(ans1, -, , ans2);
  68.  
  69. memset(used, , sizeof(used));
  70. _cnt = ;
  71. dfs2(ans1, -);
  72. int res = (_cnt - ) * - dis;
  73. printf("%d\n%d\n", min(ans1, ans2), res);
  74. return ;
  75. }

Codeforces Round #328 (Div. 2) D. Super M的更多相关文章

  1. 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 ...

  2. Codeforces Round #328 (Div. 2)

    这场CF,准备充足,回寝室洗了澡,睡了一觉,可结果...   水 A - PawnChess 第一次忘记判断相等时A先走算A赢,hack掉.后来才知道自己的代码写错了(摔 for (int i=1; ...

  3. 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 ...

  4. 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 ...

  5. Codeforces Round #328 (Div. 2) A. PawnChess 暴力

    A. PawnChess Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/592/problem/ ...

  6. 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 ...

  7. 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 ...

  8. Codeforces Round #328 (Div. 2) A

    A. PawnChess time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  9. 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 ...

随机推荐

  1. 收藏的js学习小例子

    1.js模拟java里的Map function Map(){ var obj = {} ; this.put = function(key , value){ obj[key] = value ; ...

  2. windows下mysql增量备份与全备份批处理

    win下的全备批处理 批处理用于游戏服务器,经过严格测试,且正式使用,主要用来完全备份数据库,当然.这只是将数备份出来 ,至于如何将备份出来的数据远程传送的远程服务器上可以调用ftp的功能,此脚本并未 ...

  3. MVC自定义错误页404静态页

    昨天公司要求给所有项目添加自定义404错误页,具体的要求实现的有以下几点: 1.实现自定义错误(如各种error,404等)跳转到指定的页面 2.所指定的页面输出的http状态值必须是404或其他指定 ...

  4. highcharts 图表库的简单使用

    Highcharts简介: Highcharts是一款纯javascript编写的图表库,能够很简单便捷的在Web网站或Web应用中添加交互性的图表,Highcharts目前支持直线图.曲线图.面积图 ...

  5. UINavigationController与UITabbarController的样式

    之前虽然也手写过这两中视图控制器,但是更多的还是使用SB来创建,最近发现了一些问题,现在总结一下. 1.改变UINavigationBar的颜色 在UINavigationController中,之前 ...

  6. js、expression表达式解析

    首先理解一下下面的表达式:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeigh ...

  7. startup.bat闪退---tomcat环境变量配置中遇到的问题

    常用的方法: 1.在已解压的tomcat的bin文件夹下找到startup.bat,右击->编辑.在文件头加入下面两行: SET JAVA_HOME=D:\Java\jdk1.6.0_10   ...

  8. 一些常用的字符串hash函数

    unsigned int RSHash(const std::string& str) { unsigned int b = 378551; unsigned int a = 63689; u ...

  9. zedboard - 轻量级以太网控制器LWIP

    ipconfig/all route print  显示本机所有的网络 网关是什么 那么网关到底是什么呢?网关实质上是一个网络通向其他网络的IP地址.比如有网络A和网络B,网络A的IP地址范围为&qu ...

  10. php关联不上mysql解决办法

      ## php无法关联mysql  php关联不上mysql解决办法: 尝试N多方法,需要的dll文件都复制了,依旧是不断提示: Fatal error: Call to undefined fun ...