• 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;
            }
        }
    }
  • 相关阅读:
    BZOJ 1185 [HNOI2007]最小矩形覆盖 ——计算几何
    BZOJ 1007 [HNOI2008]水平可见直线 ——计算几何
    BZOJ 1069 [SCOI2007]最大土地面积 ——计算几何
    BZOJ 2829 信用卡凸包 ——计算几何
    BZOJ 2300 [HAOI2011]防线修建 ——计算几何
    BZOJ 1027 [JSOI2007]合金 ——计算几何
    BZOJ 1043 [HAOI2008]下落的圆盘 ——计算几何
    BZOJ 1294 [SCOI2009]围豆豆Bean ——计算几何
    BZOJ 1043 [HAOI2008]下落的圆盘 ——计算几何
    radius服务器搭建
  • 原文地址:https://www.cnblogs.com/zxhl/p/5459982.html
Copyright © 2020-2023  润新知