zoukankan      html  css  js  c++  java
  • hdu-4607-Park Visit

    Park Visit

    题目连接Click here~~~~

    Problem Description
    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?
      解析:类似于找一个最长的树根,即

    *__*__*__*__*__*__*__*__*__
              |         |    |    |
              *        *    *    *
              |         |
              *        * 

    其次就是找最长的树根时,要两次遍历:

    1. 第一次遍以任意一个点A出发,找到距离最远的那个点B。

     2. 再从B出发,找到距离B最远的那个点C,则BC线段长度即为树根长。

     证明:首先,B 一定是直径的一端。

         a) 如果 A 是直径的一端,那么 B 显然是直径的另一端,结论成立。

         b) 如果 A 不是直径的一端,那么路径(A->B)一定与直径交于一点 p,且(p->B)一定是直径的一部分,推出 B 是直径的一端,结论成立。

          1> 为什么一定存在交点 p?

              如果不存在交点 p ,由于树是连通的,那么加若干边使得直径经过(A->B)上的一点 p,直径不会变得更短。

          2>为什么(p->B)一定是直径的一部分。

              如果不是,(A->B)就不是从 A 到 B 最长的路径。

    然后大家就明白了,设树根长为L,假如要参观的景点N<=L,则输出 N-1 ,否则,输出 L - 1 + 2 * ( N - L )。  
    具体代码如下:

    #include<stdio.h>
    #include<string.h>
    #include<vector>
    #include<queue>
    using namespace std;
    int s[100005];
    int main()
    {
        int N,m,n,a,b,i,l;
        scanf("%d",&N);
        while(N--)
        {
            scanf("%d%d",&m,&n);
            vector<int> Q[100005];
            queue<int> q;
            memset(s,0,sizeof(s));
            for(i=0;i<m-1;i++)
            {
                scanf("%d%d",&a,&b);
                Q[a].push_back(b);
                Q[b].push_back(a);
            }
            q.push(1);s[1]=1;
            while(!q.empty())
            {
                a=q.front();
                for(i=0;i<Q[a].size();i++)
                {
                    if(!s[Q[a][i]])  q.push(Q[a][i]);
                }
                s[a]=1;q.pop();
            }
            q.push(a);memset(s,0,sizeof(s));s[a]=1;
            while(!q.empty())
            {
                a=q.front();
                for(i=0;i<Q[a].size();i++)
                {
                    if(!s[Q[a][i]])  {q.push(Q[a][i]);s[Q[a][i]]=s[a]+1;}
                }
                q.pop();
            }
            l=s[a];
            while(n--)
            {
                scanf("%d",&a);
                if(a<=l) printf("%d
    ",a-1);
                else     printf("%d
    ",l-1+2*(a-l));
            }
        }
    }
  • 相关阅读:
    Writing and deploying a custom report in Visual Studio for Dynamics 365 /PowerApps
    Integrating Dynamics 365 CE with PowerApps
    Creating a console application to import data to Dynamics 365 / Power Apps
    DNS仍然是整个互联网中最脆弱的一环
    域名解析TTL值设置为多少合适?
    DNS TTL 字段就是骗你的
    DNS域名轮询业务监控
    域名到站点的负载均衡技术一览
    nginx+iis实现负载均衡
    用Node.js基于Express框架重写博客程序,从此告别ASP.NET。
  • 原文地址:https://www.cnblogs.com/aukle/p/3220070.html
Copyright © 2011-2022 走看看