题目链接 : http://codeforces.com/gym/100781/attachments

题意 :

有n个编号为0-n-1的点, 给出目前已经有的边(最多n-1条), 问如何添加最少的边, 使得整个图连通, 且其中两点间距离的最大值最小, 一条边距离为1单位

思路 :

两点间距离的情况 : 1. 子图中任意两点间距离   2.两个子图中两点间的距离

无论如何添加边对第一种的距离都没有影响, 对第二种却有影响

考虑添加一条边连通两个子图后, 最长的距离为 子图1中距离添加边的节点的最长的距离 + 子图2中距离添加边的节点的最长的距离 + 添加的1条边的距离

所以选择添加边的节点是 在某个子图中, 使所有节点到它的最长距离最小的点

但是不用算出这个点, 只需要这个距离, 这个距离就是一个子图中距离最长两点距离的一半, 即(max_length + 1) / 2

一个图中最长距离求法是在对某个子图任一节点深搜, 搜到距离最长的点, 再对这个点深搜, 记录最长距离(新技能)

最终取 : 情况1 和 情况2 距离最大值

注意情况二中, 如果存在三个子图最大(max_length + 1) / 2 都相等, 最终答案是要加2条边的距离而不是加1条, 比如0-1    1-2    3-4 这三个子图

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. const int MAXN = 1e5+;
  9.  
  10. vector<int> edge[MAXN];
  11. int depth[MAXN];
  12. bool vis[MAXN];
  13. int maxx, maxx1, mark1;
  14. int length[MAXN];
  15.  
  16. int Max(int a, int b)
  17. {
  18. return a > b ? a : b;
  19. }
  20.  
  21. void Dfs(int u, int fa)
  22. {
  23. vis[u] = ;
  24. depth[u] = depth[fa] + ;
  25. if(depth[u] > maxx1) {
  26. mark1 = u;
  27. maxx1 = depth[u];
  28. }
  29. int len = edge[u].size();
  30. for(int i = ; i < len; i++) {
  31. int v = edge[u][i];
  32. if(v == fa) continue;
  33. Dfs(v, u);
  34. }
  35. }
  36.  
  37. bool cmp(int a, int b)
  38. {
  39. return a > b;
  40. }
  41.  
  42. void Init(int n)
  43. {
  44. for(int i = ; i <= n; i++) {
  45. edge[i].clear();
  46. vis[i] = ;
  47. }
  48. maxx = ;
  49. }
  50.  
  51. int main()
  52. {
  53. int n, l;
  54. int u, v;
  55.  
  56. while(scanf("%d %d", &n, &l) != EOF) {
  57. Init(n);
  58. for(int i = ; i < l; i++) {
  59. scanf("%d %d", &u, &v);
  60. edge[u].push_back(v);
  61. edge[v].push_back(u);
  62. }
  63. int cnt = ;
  64. for(int i = ; i < n; i++) {
  65. maxx1 = ;
  66. if(vis[i] == ) {
  67. depth[i] = -;
  68. Dfs(i, i);
  69. depth[mark1] = -;
  70. if(maxx1 != ) {
  71. Dfs(mark1, mark1);
  72. }
  73. length[i] = (maxx1 + ) / ;
  74. if(maxx1 > maxx) maxx = maxx1;
  75. cnt++;
  76. }
  77. }
  78. sort(length, length+n, cmp);
  79. if(n == ) printf("0\n");
  80. else if(n == ) printf("1\n");
  81. else if(n == ) printf("2\n");
  82. else if(cnt == ) printf("%d\n", maxx);
  83. else if(length[] == length[] && length[] == length[]) {
  84. printf("%d\n", Max(maxx, length[] + length[] + ));
  85. }
  86. else {
  87. printf("%d\n", Max(maxx, length[] + length[] + ));
  88. }
  89. }
  90.  
  91. return ;
  92. }

NCPC 2015 - Problem A - Adjoin the Networks的更多相关文章

  1. NCPC 2015 October 10, 2015 Problem D

    NCPC 2015Problem DDisastrous DowntimeProblem ID: downtimeClaus Rebler, cc-by-saYou’re investigating ...

  2. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem K. UTF-8 Decoder 模拟题

    Problem K. UTF-8 Decoder 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c702 ...

  3. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem I. Alien Rectangles 数学

    Problem I. Alien Rectangles 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c ...

  4. dp - Google Code jam Qualification Round 2015 --- Problem B. Infinite House of Pancakes

    Problem B. Infinite House of Pancakes Problem's Link:   https://code.google.com/codejam/contest/6224 ...

  5. Google Code jam Qualification Round 2015 --- Problem A. Standing Ovation

    Problem A. Standing Ovation Problem's Link:   https://code.google.com/codejam/contest/6224486/dashbo ...

  6. Google Code Jam Round 1C 2015 Problem A. Brattleship

    Problem You're about to play a simplified "battleship" game with your little brother. The ...

  7. Google Code Jam Round 1A 2015 Problem B. Haircut 二分

    Problem You are waiting in a long line to get a haircut at a trendy barber shop. The shop has B barb ...

  8. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem H. Parallel Worlds 计算几何

    Problem H. Parallel Worlds 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c7 ...

  9. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem F. Turning Grille 暴力

    Problem F. Turning Grille 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c70 ...

随机推荐

  1. hdu3308LCIS(线段树,点更新,段查寻,查寻时一定要注意跨越时如何计算)

    Problem Description Given n integers. You have two operations: U A B: replace the Ath number by B. ( ...

  2. [CSS3] Interactive Pseudo-Classes :link :visited :hover :active

    The interactive pseudo-classes for links (and buttons) allow us to make sure the user knows what ele ...

  3. [Javascript] Advanced Reduce: Flatten, Flatmap and ReduceRight

    Learn a few advanced reduction patterns: flatten allows you to merge a set of arrays into a single a ...

  4. Jquery UI的datepicker插件使用

    原文链接;http://www.ido321.com/375.html Jquery UI是一个非常丰富的Jquery插件,而且UI的各部分插件能够独自分离出来使用.这是其它非常多Jquery插件没有 ...

  5. PHP5.4新特性(转)

    PHP5.4正式前两天发布了,之前有看了一些PHP5.4主要特性相关文章,因此在这里小结一下. 其中好几点更新是由Laruence贡献的!本文部分内容也是源自Laruence的博客. 1. Buid- ...

  6. AngularJS clone directive 指令复制

    需求背景:         directive模块化某表单信息,但表单信息可加入多条.此时就涉及到clone directive. 解决方式:         能够通过使用angularjs中$com ...

  7. [转] 使用Spring Boot和Gradle创建项目

    Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的 ...

  8. Linux GRUB 2及修改默认启动项

    The GRUB 2 boot loader makes sure that you can boot Linux. GRUB 2 is installed in the boot sector of ...

  9. machine learning in action , part 1

    We should think in below four questions: the decription of machine learning key tasks in machine lea ...

  10. SQL从入门到基础–03 SQLServer基础1(主键选择、数据插入、数据更新)

    一.SQL语句入门 1. SQL语句是和DBMS“交谈”专用的语句,不同DBMS都认SQL语法. 2. SQL语句中字符串用单引号. 3. SQL语句中,对于SQL关键字大小写不敏感,对于字符串值大小 ...