zoukankan      html  css  js  c++  java
  • 树形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);
              }
         }
    }
    


  • 相关阅读:
    通过field:global给子元素添加css样式
    TP5 调用邮箱接口
    php数组使用json_encode函数中文被编码成null的原因和解决办法
    UNIX系统上的抓包工具tcpdump常用命令说明
    快速搭建ELK7.5版本的日志分析系统--搭建篇
    Kubernetes实战之部署ELK Stack收集平台日志
    k8s实战之部署Prometheus+Grafana可视化监控告警平台
    Linux防火墙firewalld安全设置
    在zabbix中实现发送带有图片的邮件和微信告警
    用Dockerfile部署zabbix
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348227.html
Copyright © 2011-2022 走看看