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

    题目:

    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
     
    思路 :
            求一条最长的路(树的直径) 。 如果问的点比最长的路上的点还多,则多一个点就多走两条路。

     代码:

    两次搜索:

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<vector>
     4 #include<iostream>
     5 using namespace std;
     6 
     7 const int T=100005;
     8 vector < int > Q[T];
     9 int flag[T],V,Max;
    10 
    11 
    12 void dfs(int x,int xx)
    13 {
    14     flag[x]=1;
    15     for(int i=0;i<(int)Q[x].size();i++)
    16     {
    17         int k=Q[x][i];
    18         if(!flag[k])
    19         {
    20             dfs(k,xx+1);
    21             flag[k]=0;
    22         }
    23     }
    24     if(xx>Max)
    25     {
    26         V=x;
    27         Max=xx;
    28     }
    29 }
    30 
    31 
    32 int main()
    33 {
    34     int N;
    35     scanf("%d",&N);
    36     while(N--)
    37     {
    38         int n,m,i,a,b,v;
    39         memset(flag,0,sizeof(flag));
    40         scanf("%d %d",&n,&m);
    41         for(i=1;i<=n;i++)
    42            Q[i].clear();
    43         for(i=1;i<n;i++)
    44         {
    45             scanf("%d %d",&a,&b);
    46             Q[a].push_back(b);
    47             Q[b].push_back(a);
    48         }
    49         V=0; Max=0;
    50         dfs(1,1);
    51         flag[1]=0;
    52         v=V; Max=0; V=0;
    53         dfs(v,1);
    54        // printf("Max==%d
    ",Max);
    55         for(i=1;i<=m;i++)
    56         {
    57             scanf("%d",&a);
    58             if(a<=Max) printf("%d
    ",a-1);
    59             else printf("%d
    ",Max-1+(a-Max)*2);
    60         }
    61     }
    62 }
    View Code

    树形dp:

    View Code
  • 相关阅读:
    setAnimationTransition:forView:cache: 运行动画时背景色问题
    架构师速成4.6-软技能和硬技能
    Java获取某年某周的第一天
    openssl之BIO系列之12---文件描写叙述符(fd)类型BIO
    centos 使用 CP 命令 不提示 覆盖
    [3 Jun 2015 ~ 9 Jun 2015] Deep Learning in arxiv
    P1314 聪明的质监员
    P2858 [USACO06FEB]奶牛零食Treats for the Cows
    1163 访问艺术馆
    P1352 没有上司的舞会
  • 原文地址:https://www.cnblogs.com/lysr--tlp/p/tlppp.html
Copyright © 2011-2022 走看看