树形DP求树的直径

hdu4607

Park Visit

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


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
程序:


#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define M 100009
#define inf 99999999
struct st
{
     int u,v,w,next;
}edge[M*3];
int head[M],use[M],t,dis[M][3],in[M];
void init()
{
     t=0;
     memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
     edge[t].u=u;
     edge[t].v=v;
     edge[t].w=w;
     edge[t].next=head[u];
     head[u]=t++;
}
void dfs(int u)
{
     use[u]=1;
     for(int i=head[u];i!=-1;i=edge[i].next)
     {
          int v=edge[i].v;
          if(!use[v])
          {
               dfs(v);
               //更新最大值和次大值
               if(dis[u][0]<dis[v][0]+edge[i].w)
               {
                    int tt=dis[u][0];
                    dis[u][0]=dis[v][0]+edge[i].w;
                    dis[u][1]=tt;
               }
               else if(dis[u][1]<dis[v][0]+edge[i].w)
                    dis[u][1]=dis[v][0]+edge[i].w;
          }
     }
     if(in[u]==1&&u!=1)//注意
          dis[u][0]=dis[u][1]=0;
}
int main()
{
     int T,n,m,i;
     scanf("%d",&T);
     while(T--)
     {
          init();
          scanf("%d%d",&n,&m);
          memset(in,0,sizeof(in));
          for(i=1;i<n;i++)
          {
               int a,b;
               scanf("%d%d",&a,&b);
               add(a,b,1);
               add(b,a,1);
               in[a]++;
               in[b]++;
          }
          memset(use,0,sizeof(use));
          memset(dis,0,sizeof(dis));
          dfs(1);
          int ans=-1;//记录直径
          for(i=1;i<=n;i++)
          {
               if(ans<dis[i][0]+dis[i][1])
                    ans=dis[i][0]+dis[i][1];
          }
          while(m--)
          {
               int s;
               scanf("%d",&s);
               if(s<=ans+1)
                    printf("%d
",s-1);
               else
                    printf("%d
",ans+(s-ans-1)*2);
          }
     }
}


原文地址:https://www.cnblogs.com/mypsq/p/4348227.html