zoukankan      html  css  js  c++  java
  • 快速切题 sgu134.Centroid 树形dp

    134. Centroid

    time limit per test: 0.25 sec. 
    memory limit per test: 4096 KB

     

    You are given an undirected connected graph, with N vertices and N-1 edges (a tree). You must find the centroid(s) of the tree. 
    In order to define the centroid, some integer value will be assosciated to every vertex. Let's consider the vertex k. If we remove the vertex k from the tree (along with its adjacent edges), the remaining graph will have only N-1 vertices and may be composed of more than one connected components. Each of these components is (obviously) a tree. The value associated to vertex k is the largest number of vertices contained by some connected component in the remaining graph, after the removal of vertex k. All the vertices for which the associated value is minimum are considered centroids.

     

    Input

    The first line of the input contains the integer number N (1<=N<=16 000). The next N-1 lines will contain two integers, a and b, separated by blanks, meaning that there exists an edge between vertex a and vertex b.

     

    Output

    You should print two lines. The first line should contain the minimum value associated to the centroid(s) and the number of centroids. The second line should contain the list of vertices which are centroids, sorted in ascending order.

     

    Sample Input

    7
    1 2
    2 3
    2 4
    1 5
    5 6
    6 7
    

    Sample Output

    3 1
    1
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int maxn=16005;
    const int maxm=2*maxn;
    int first[maxn];
    int nxt[maxm];
    int to[maxm];
    int maxson[maxn];
    int subtree[maxn];
    int n;
    int dfs(int s,int f){
        int sum=0;
        for(int p=first[s];p!=-1;p=nxt[p]){
            if(to[p]==f)continue;
            int subson=dfs(to[p],s);
            maxson[s]=max(maxson[s],subson);
            sum+=subson;
        }
        maxson[s]=max(maxson[s],n-sum-1);
        return subtree[s]=sum+1;
    }
    void addedge(int f,int t,int i){
        nxt[2*i]=first[f];
        first[f]=2*i;
        to[2*i]=t;
        nxt[2*i+1]=first[t];
        first[t]=2*i+1;
        to[2*i+1]=f;
    }
    int heap[maxn];
    int main(){
        scanf("%d",&n);
        memset(first,-1,sizeof(first));
        for(int i=1;i<n;i++){
            int f,t;
            scanf("%d%d",&f,&t);
            addedge(f,t,i);
        }
        dfs(1,0);
        int ans=0xffffff,len=0;
        for(int i=1;i<=n;i++){
            if(maxson[i]<ans){
                len=0;ans=maxson[i];
                heap[len++]=i;
            }
            else if(maxson[i]==ans){
                heap[len++]=i;
            }
        }
        printf("%d %d
    ",ans,len);
        for(int i=0;i<len;i++){
            printf("%d%c",heap[i],i==len-1?'
    ':' ');
        }
        return 0;
    }
    

      

  • 相关阅读:
    Code review
    一点心得
    有关双向追踪性的一点感觉
    测试用例分析的一点心得
    js简单的抽屉菜单
    新的感受
    linux的VPS如何分区
    PHP中Unicode转码和解码的实现
    xampp安装及配置
    js Unicode编码转换
  • 原文地址:https://www.cnblogs.com/xuesu/p/4026647.html
Copyright © 2011-2022 走看看