zoukankan      html  css  js  c++  java
  • hdu 4607 Park Visit

    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
    首先求出树的直径,然后若k<=inf+1的话,则在最长链上走,若大于则距离为inf+2*(k-inf-1);
    #include <bits/stdc++.h>
    using namespace std;
    typedef unsigned long long ull;
    typedef long long ll;
    struct node
    {
        int to,next;
    }e[200005];
    int n,m,x,y,k,t,pos;
    int vis[100005],dis[100005],head[100005];
    void add(int u,int v)
    {
        e[pos].to=v;
        e[pos].next=head[u];
        head[u]=pos++;
    }
    void dfs(int f,int u,int d)
    {
        dis[u]=d;
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            if(e[i].to!=f)
                dfs(u,e[i].to,d+1);
        }
    }
    int main()
    {
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&m);
            memset(head,-1,sizeof(head));
            pos=0;
            for(int i=1;i<n;i++)
            {
                scanf("%d%d",&x,&y);
                add(x,y);
                add(y,x);
            }
            dfs(0,1,0);
            int inf=-1,pos;
            for(int i=1;i<=n;i++)
                if(dis[i]>inf)inf=dis[i],pos=i;
            dfs(0,pos,0);
            for(int i=1;i<=n;i++)
                inf=max(inf,dis[i]);
            while(m--)
            {
                scanf("%d",&k);
                if(k<=inf+1) printf("%d
    ",k-1);
                else printf("%d
    ",inf+2*(k-inf-1));
            }
        }
        return 0;
    }
  • 相关阅读:
    9 jmeter之检查点
    8 jmeter之集合点
    7 jmeter之参数化
    6 jmeter元件的作用域与执行顺序
    5 jmeter性能测试小小的实战
    4 jmeter badboy脚本开发技术详解
    3 jmeter的两种录制方法
    2 jmeter常用功能介绍-测试计划、线程组
    Errors occurred during the build. Errors running builder 'DeploymentBuilder' on project
    常见异常总结
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/9306657.html
Copyright © 2011-2022 走看看