zoukankan      html  css  js  c++  java
  • hdu 4607 Park Visit 求树的直径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607

    Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The park is beautiful - but large, indeed. N feature spots in the park are connected by exactly (N-1) undirected paths, and Claire is too tired to visit all of them. After consideration, she decides to visit only K spots among them. She takes out a map of the park, and luckily, finds that there're entrances at each feature spot! Claire wants to choose an entrance, and find a way of visit to minimize the distance she has to walk. For convenience, we can assume the length of all paths are 1.
    Claire is too tired. Can you help her?
     
    题意描述:小伙伴在公园里玩,公园里有N个景点,由n-1条无向路径连通(每条路径长度为1)。现在小伙伴想参观其中k个景点,并且要求所走的路径最短。求最短路径。
    算法分析:首先知道这是一个无向连通图,形如树形结构。假设公园里有一条很长的路径,包含了比k大的景点个数,这时我们要走完k个景点的最短路径就是k-1(沿着这条路往下走即可),如果没有k个呢,就要想着尽量少的景点在别的路径(因为这条路径上景点个数多,走到别的路径还得回来继续往下走)。由此,可以想到求树的直径就是了。(关于求解树的直径的证明网上也有很多)
    求树的直径算法:树的直径是指树的最长简单路。
    求法: 两遍BFS :先任选一个起点BFS找到最长路的终点,再从终点进行BFS,则第二次BFS找到的最长路即为树的直径;
    原理:设起点为u,第一次BFS找到的终点v一定是树的直径的一个端点
    证明:1) 如果u 是直径上的点,则v显然是直径的终点(因为如果v不是的话,则必定存在另一个点w使得u到w的距离更长,则于BFS找到了v矛盾)
    2) 如果u不是直径上的点,则u到v必然于树的直径相交(反证),那么交点到v 必然就是直径的后半段了所以v一定是直径的一个端点,所以从v进行BFS得到的一定是直径长度
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<cmath>
     6 #include<algorithm>
     7 #include<queue>
     8 #define inf 0x7fffffff
     9 using namespace std;
    10 const int maxn=100000+10;
    11 const int M = 100000+10;
    12 
    13 int n,m;
    14 struct node
    15 {
    16     int v,w;
    17     int next;
    18 }edge[M*3];
    19 int head[maxn],edgenum;
    20 
    21 void add(int u,int v,int w)
    22 {
    23     edge[edgenum].v=v ;edge[edgenum].w=w ;
    24     edge[edgenum].next=head[u] ;head[u]=edgenum++ ;
    25 
    26     edge[edgenum].v=u ;edge[edgenum].w=w ;
    27     edge[edgenum].next=head[v] ;head[v]=edgenum++ ;
    28 }
    29 
    30 int vis[maxn],d[maxn];
    31 int bfs(int from)
    32 {
    33     queue<int> Q;
    34     Q.push(from);
    35     memset(d,-1,sizeof(d));
    36     memset(vis,0,sizeof(vis));
    37     d[from]=1;
    38     vis[from]=1;
    39     int maxlen=-1,k=0;
    40     while (!Q.empty())
    41     {
    42         int u=Q.front() ;Q.pop() ;
    43         for (int i=head[u] ;i!=-1 ;i=edge[i].next)
    44         {
    45             int v=edge[i].v;
    46             if (!vis[v])
    47             {
    48                 vis[v]=1;
    49                 d[v]=d[u]+1;
    50                 if (d[v]>maxlen)
    51                 {
    52                     maxlen=d[v];
    53                     k=v;
    54                 }
    55                 Q.push(v);
    56             }
    57         }
    58     }
    59     return k;
    60 }
    61 
    62 int main()
    63 {
    64     int t;
    65     scanf("%d",&t);
    66     while (t--)
    67     {
    68         memset(head,-1,sizeof(head));
    69         edgenum=0;
    70         int a,b;
    71         scanf("%d%d",&n,&m);
    72         for (int i=1 ;i<=n-1 ;i++)
    73         {
    74             scanf("%d%d",&a,&b);
    75             add(a,b,1);
    76         }
    77         int v=bfs(1);
    78         int u=bfs(v);
    79         int maxlen=d[u];
    80         //cout<<"debug= "<<maxlen<<endl;
    81         for (int i=0 ;i<m ;i++)
    82         {
    83             scanf("%d",&a);
    84             if (a<=maxlen) printf("%d
    ",a-1);
    85             else printf("%d
    ",maxlen-1+(a-maxlen)*2);
    86         }
    87     }
    88     return 0;
    89 }
  • 相关阅读:
    颜色渐变
    DELPHI 反射机制
    网络的收藏资料
    WM_Paint 消息详解
    解决EmbeddedWB弹出页面错误框的问题
    刁蛮公主第二集(纳米盘)
    第五章 用用户控件创建自定义控件
    RTX51 tiny系统要注意的问题:(关于时间片)
    第四章 高级控件编程
    CSDN新频道visual studio技术频道
  • 原文地址:https://www.cnblogs.com/huangxf/p/4366979.html
Copyright © 2011-2022 走看看