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;
    }
    

      

  • 相关阅读:
    纯js实现字符串formate方法
    C#实现json压缩和格式化
    简单的前端校验框架实现
    快速拷贝文件
    0012 移除元素
    0011 删除链表的倒数第N个节点
    0010 最长公共前缀
    0009 合并两个有序链表
    0008 合并K个排序链表
    0007 回文数
  • 原文地址:https://www.cnblogs.com/xuesu/p/4026647.html
Copyright © 2011-2022 走看看