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;
            }
        }
    }
  • 相关阅读:
    get_folder_size.ps1
    python3-database-shelve
    Windows中实现不依赖账户登录的开机启动程序
    SpringBoot+SpringDataJPA如何实现自定义且自由度高的查询[多表,多查询条件,多排序条件,分页,自定义sql封装]
    Windows phone 8.1之数据绑定(Data Binding)
    TextBox使用技巧--转载
    在Eclipse中使用git把项目导入到git中--转载
    运用多种知识点实现一个综合小游戏
    Git帮助之初始化项目设置向导
    如何从Eclipse导入github上的项目源码--转载
  • 原文地址:https://www.cnblogs.com/zxhl/p/5459982.html
Copyright © 2011-2022 走看看