zoukankan      html  css  js  c++  java
  • Park Visit

    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≤10 5), 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  计算走过m个点需要走多远的距离
    有两种情况   假设该集合最长链长度为k;
    1.   k>=m-1;   那么直接在最长链上走就可以      d=m-1
    2.   k<m-1     除了最长链还需要在分支上找点   这样到达分支上的每个点  还要返回到最长链 就需要多走该点到最长链的距离 
      那么d=(m-1-k)*2+k
    #include<cstdio>
    #include<queue>
    #include<cstring>
    using namespace std;
    struct node 
    {
    	int from,to,val,next;
    };
    node dian[1000001];
    int cut;
    int head[100001];
    void chushihua()
    {
    	cut=0;
    	memset(head,-1,sizeof(head));
    }
    
    void  Edge(int u,int v,int w)
    {
    	dian[cut].from =u;
    	dian[cut].to =v;
    	dian[cut].val =w;
    	dian[cut].next =head[u];
    	head[u]=cut++;
    }
    int jilu;
    int vis[100001];
    int dist[100001];
    int ans;
    int n,m;
    void bfs(int s)
    {
    memset(vis,0,sizeof(vis));
    memset(dist,0,sizeof(dist));
    	ans=0;
    	queue<int>q;
    	q.push(s);vis[s]=1;dist[s]=0;
    	while(!q.empty() )
    	{
    		int u=q.front() ;
    		q.pop() ;
    		for(int i=head[u];i!=-1;i=dian[i].next )
    		{
    			int v=dian[i].to ;
    			if(!vis[v])
    			{
    				if(dist[v]<dist[u]+dian[i].val )
    				dist[v]=dist[u]+dian[i].val ;
    					vis[v]=1;
    		            q.push(v); 	
    			}
    		}
         }
    	for(int i=1;i<=n;i++)
    	{
    			if(ans<dist[i])
    		     {
    			    ans=dist[i];
    			    jilu=i;
    	          }
    	}
    		
    }
    int main()
    {
      int t;
      scanf("%d",&t);
      while(t--)
      {
      	scanf("%d%d",&n,&m);
         	chushihua();
    		int a,b,c;
    		for(int i=1;i<n;i++)
    		{
    			scanf("%d%d%",&a,&b);
    			Edge(a,b,1);
    			Edge(b,a,1);
    		}
    		bfs(1);
    		bfs(jilu);
    		while(m--)
    		{
    			scanf("%d",&c);
    			if(c<=ans+1)
    			{
    				printf("%d
    ",c-1);
    			}
    			else
    			{
    				printf("%d
    ",(c-ans-1)*2+ans);
    			}
    		}
      }
    	
    	return 0;
    }


  • 相关阅读:
    2016 年青岛网络赛---Sort(k叉哈夫曼)
    Gym 100703G---Game of numbers(DP)
    棋盘覆盖(分治法)
    大整数乘法(分治法)
    博客编辑---数学公式
    《程序员代码面试指南》第八章 数组和矩阵问题 子数组的最大累加和问题
    《程序员代码面试指南》第八章 数组和矩阵问题 奇数下标都是奇数或者偶数下标都是偶数
    《程序员代码面试指南》第八章 数组和矩阵问题 自然数数组的排序
    《程序员代码面试指南》第八章 数组和矩阵问题 计算数组的小和
    《程序员代码面试指南》第八章 数组和矩阵问题 未排序数组中累加和小于或等于给定值的最长子数组长度
  • 原文地址:https://www.cnblogs.com/kingjordan/p/12027095.html
Copyright © 2011-2022 走看看