zoukankan      html  css  js  c++  java
  • HDU 4607 Park Visit 求树的直径

    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
     

    题意:

      给你一个n-1条边n个点的无向图

      m个询问,每次询问你从任意一个起点出发路过k个点的最短路径长度

    题解:

      求出树的直径

      在k小于直径上的点时就走直径就好了

      大于的话也就是中途多走k-直径点数个点

      长度画图就明白了

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <cmath>
    #include <algorithm>
    #include<vector>
    using namespace std;
    const int N = 1e5+20, M = 1e6+10, mod = 1e9+7, inf = 1e9+1000;
    typedef long long ll;
    
    int n,m,a[N],mx,d[N],mxx;
    vector<int >G[N];
    void dfs(int x,int fa) {
        d[x] = d[fa]+1;
        if(d[x] > mxx) mxx = d[x], mx = x;
        for(int i=0;i<G[x].size();i++) {
            if(G[x][i]==fa) continue;
            dfs(G[x][i],x);
        }
    }
    int main() {
        int T;
        scanf("%d",&T);
        while(T--) {
            mxx = 0, mx = 0;
            memset(d,0,sizeof(d));
            scanf("%d%d",&n,&m);
            for(int i=1;i<=n;i++) G[i].clear();
            for(int i=1;i<n;i++) {
                int u,v;
                scanf("%d%d",&u,&v);
                G[u].push_back(v);
                G[v].push_back(u);
            }
            dfs(1,0);
            memset(d,0,sizeof(d));
            mxx = 0;
            dfs(mx,0);
            int ans = mxx;
            while(m--) {
                int x;
                scanf("%d",&x);
                if(x<=ans) cout<<x-1<<endl;
                else cout<<(x-ans)*2+ans-1<<endl;
            }
        }
    }
  • 相关阅读:
    TP框架基础
    PHP文件上传
    layui-简单的登录注册界面
    layui-注册界面
    JavaScript原生Ajax请求纯文本数据
    ajax异步请求数据
    PHP数据库连接
    PHP页面跳转以及伪登录实例
    OVER 分析函数
    Ubuntu 16 vi输入方向键会变成ABCD的解决方法
  • 原文地址:https://www.cnblogs.com/zxhl/p/5459982.html
Copyright © 2011-2022 走看看