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;
            }
        }
    }
  • 相关阅读:
    block iOS 块
    面试有感
    Could not automatically select an Xcode project. Specify one in your Podfile like so
    xcrun: error: active developer path
    NSDictionary
    CSS3魔法堂:CSS3滤镜及Canvas、SVG和IE滤镜替代方案详解
    转:CSS盒模型
    转:手机端html5触屏事件(touch事件)
    转: div:给div加滚动条 div的滚动条设置
    转:什么时候用阻止事件冒泡
  • 原文地址:https://www.cnblogs.com/zxhl/p/5459982.html
Copyright © 2011-2022 走看看