zoukankan      html  css  js  c++  java
  • MUTC2013 H-Park Visit-hdu4607

    Park Visit

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 85    Accepted Submission(s): 34


    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
     

    Source
     

    Recommend
    liuyiding
     

    求出树的直径r,当k<=r时,ans=k-1;当k>r时,ans=(k-r)*2+r-1;

    -----

    第一次尝试使用超DIO的模板。

    /** head-file **/
    
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <iomanip>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <string>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <list>
    #include <set>
    #include <map>
    #include <algorithm>
    
    /** define-for **/
    
    #define REP(i, n) for (int i=0;i<int(n);++i)
    #define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
    #define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
    #define REP_1(i, n) for (int i=1;i<=int(n);++i)
    #define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
    #define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
    #define REP_N(i, n) for (i=0;i<int(n);++i)
    #define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
    #define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
    #define REP_1_N(i, n) for (i=1;i<=int(n);++i)
    #define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
    #define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)
    
    /** define-useful **/
    
    #define clr(x,a) memset(x,a,sizeof(x))
    #define sz(x) int(x.size())
    #define see(x) cerr<<#x<<" "<<x<<endl
    #define se(x) cerr<<" "<<x
    #define pb push_back
    #define mp make_pair
    
    /** test **/
    
    #define Display(A, n, m) {                      
        REP(i, n){                                  
            REP(j, m) cout << A[i][j] << " ";       
            cout << endl;                           
        }                                           
    }
    
    #define Display_1(A, n, m) {                    
        REP_1(i, n){                                
            REP_1(j, m) cout << A[i][j] << " ";     
            cout << endl;                           
        }                                           
    }
    
    using namespace std;
    
    /** typedef **/
    
    typedef long long LL;
    
    /** Add - On **/
    
    const int direct4[4][2]= { {0,1},{1,0},{0,-1},{-1,0} };
    const int direct8[8][2]= { {0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} };
    const int direct3[6][3]= { {1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1} };
    
    const int MOD = 1000000007;
    const int INF = 0x3f3f3f3f;
    const long long INFF = 1LL << 60;
    const double EPS = 1e-9;
    const double OO = 1e15;
    const double PI = acos(-1.0); //M_PI;
    
    const int maxn=111111;
    const int maxm=511111;
    int n,m;
    
    struct EDGENODE
    {
        int to;
        int w;
        int next;
    };
    
    int head[maxn];
    EDGENODE edges[maxm];
    int edge;
    void init()
    {
        clr(head,-1);
        edge=0;
    }
    void addedge(int u,int v,int c=0)
    {
        edges[edge].w=c,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++;
        edges[edge].w=c,edges[edge].to=u,edges[edge].next=head[v],head[v]=edge++;
    }
    
    bool vis[maxn];
    int len[maxn];
    
    void dfs(int u,int pa)
    {
        len[u]=1;
        if (pa!=-1) len[u]+=len[pa];
        for (int i=head[u]; i!=-1; i=edges[i].next)
        {
            int v=edges[i].to;
            if (v!=pa&&!vis[v])
            {
                dfs(v,u);
            }
        }
    }
    
    int get_tree_len()
    {
        int ret=0,t;
        clr(vis,0);
        dfs(1,-1);
        REP_1(i,n) if (len[i]>ret)
        {
            ret=len[i];
            t=i;
        }
        clr(vis,0);
        dfs(t,-1);
        REP_1(i,n) ret=max(ret,len[i]);
        return ret;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while (T--)
        {
            scanf("%d%d",&n,&m);
            init();
            REP(i,n-1)
            {
                int u,v;
                scanf("%d%d",&u,&v);
                addedge(u,v);
            }
            int r=get_tree_len();
            //see(r);
            REP(i,m)
            {
                int k;
                scanf("%d",&k);
                if (k<=r) printf("%d
    ",k-1);
                else  printf("%d
    ",(k-r)*2+r-1);
            }
        }
        return 0;
    }
    



  • 相关阅读:
    WPF的布局--DockPanel
    WPF的布局--StackPanel
    C#中的不可空类型转为可空类型
    linux下安装nodejs及npm
    HTML DOM 事件对象 ondragend 事件
    pc端页面在移动端显示问题
    css设置文字上下居中,一行文字居中,两行或多行文字同样居中。
    超简单的gif图制作工具
    Git创建与合并分支
    props default 数组/对象的默认值应当由一个工厂函数返回
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226278.html
Copyright © 2011-2022 走看看