zoukankan      html  css  js  c++  java
  • (DP求最长路) hdu 4607

    Park Visit

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2624    Accepted Submission(s): 1178


    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?
     
    Input
    An integer T(T≤20) will exist in the first line of input, indicating the number of test cases.
    Each test case begins with two integers N and M(1≤N,M≤105), which respectively denotes the number of nodes and queries.
    The following (N-1) lines, each with a pair of integers (u,v), describe the tree edges.
    The following M lines, each with an integer K(1≤K≤N), describe the queries.
    The nodes are labeled from 1 to N.
     
    Output
    For each query, output the minimum walking distance, one per line.
     
    Sample Input
    1 4 2 3 2 1 2 4 2 2 4
     
    Sample Output
    1 4
     
    Source
     
    Recommend
    liuyiding   |   We have carefully selected several similar problems for you:  5227 5226 5225 5224 5223 
     
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<algorithm>
    #include<cstdlib>
    #include<vector>
    using namespace std;
    vector<int> e[100005];
    int n,m,dist[100005],k;
    void dfs(int u,int father)
    {
        for(int i=0;i<e[u].size();i++)
        {
            int v=e[u][i];
            if(v==father)
                continue;
            dist[v]=dist[u]+1;
            dfs(v,u);
        }
    }
    int main()
    {
        int tt;
        scanf("%d",&tt);
        while(tt--)
        {
            scanf("%d%d",&n,&m);
            for(int i=1;i<=n;i++)
                e[i].clear();
            for(int i=1;i<n;i++)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                e[x].push_back(y);
                e[y].push_back(x);
            }
            memset(dist,0,sizeof(dist));
            dfs(1,-1);
            int len=0,u;
            for(int i=1;i<=n;i++)
            {
                if(dist[i]>len)
                {
                    len=dist[i];
                    u=i;
                }
            }
            memset(dist,0,sizeof(dist));
            dfs(u,-1);
            len=0;
            for(int i=1;i<=n;i++)
            {
                if(dist[i]>len)
                    len=dist[i];
            }
            len++;
            while(m--)
            {
                scanf("%d",&k);
                if(len>=k)
                {
                    printf("%d
    ",k-1);
                }
                else
                {
                    printf("%d
    ",(k-len)*2+len-1);
                }
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    eclipse快捷键
    iOS音频播放 (二):AudioSession 转
    HNU13377:Book Club(DFS)
    BAPC2014 K&amp;&amp;HUNNU11591:Key to Knowledge(中途相遇法)
    小米净水器与小区过滤价格水对照.xls
    NUTCH2.3 hadoop2.7.1 hbase1.0.1.1 solr5.2.1部署(一)
    hibernate动态表名映射--仅仅有想不到,没有做不到
    【BZOJ 1660】 [Usaco2006 Nov]Bad Hair Day 乱发节
    oracle-企业信息化
    线性查找算法
  • 原文地址:https://www.cnblogs.com/water-full/p/4493956.html
Copyright © 2011-2022 走看看