• 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;
    }


  • 相关阅读:
    网络故障排除工具 | 快速定位网络故障
    Brocade博科光纤交换机zone配置
    博科Brocade 300光纤交换机配置zone教程
    游戏开发
    第8章 图
    第7章 二叉树
    第6章 树型结构
    第5章 递归
    第4章 字符串、数组和特殊矩阵
    第3章 顺序表的链式存储
  • 原文地址:https://www.cnblogs.com/kingjordan/p/12027095.html
Copyright © 2020-2023  润新知