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


  • 相关阅读:
    Java StringTokenizer Example
    java 删除字符串中的特定字符
    [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
    Uniform resource name
    [Python]网络爬虫(一):抓取网页的含义和URL基本构成
    coco2dx 精灵类
    window和nodejs作用域区别(待续)
    ubuntu开机遇到-您的当前网络有.local域,我们不建议这样做而且这与AVAHI网络服务探测不兼容。该服务已被禁用
    ruby中的reject和reject!
    ruby中将数组转换成hash
  • 原文地址:https://www.cnblogs.com/kingjordan/p/12027095.html
Copyright © 2011-2022 走看看