• POJ 3107 树形dp


    Godfather
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 6812   Accepted: 2390

    Description

    Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to arrest the mafia leaders.

    Unfortunately, the structure of Chicago mafia is rather complicated. There are n persons known to be related to mafia. The police have traced their activity for some time, and know that some of them are communicating with each other. Based on the data collected, the chief of the police suggests that the mafia hierarchy can be represented as a tree. The head of the mafia, Godfather, is the root of the tree, and if some person is represented by a node in the tree, its direct subordinates are represented by the children of that node. For the purpose of conspiracy the gangsters only communicate with their direct subordinates and their direct master.

    Unfortunately, though the police know gangsters’ communications, they do not know who is a master in any pair of communicating persons. Thus they only have an undirected tree of communications, and do not know who Godfather is.

    Based on the idea that Godfather wants to have the most possible control over mafia, the chief of the police has made a suggestion that Godfather is such a person that after deleting it from the communications tree the size of the largest remaining connected component is as small as possible. Help the police to find all potential Godfathers and they will arrest them.

    Input

    The first line of the input file contains n — the number of persons suspected to belong to mafia (2 ≤ n ≤ 50 000). Let them be numbered from 1 to n.

    The following n − 1 lines contain two integer numbers each. The pair aibi means that the gangster ai has communicated with the gangster bi. It is guaranteed that the gangsters’ communications form a tree.

    Output

    Print the numbers of all persons that are suspected to be Godfather. The numbers must be printed in the increasing order, separated by spaces.

    Sample Input

    6
    1 2
    2 3
    2 5
    3 4
    3 6

    Sample Output

    2 3

    Source

    Northeastern Europe 2005, Northern Subregion
    题意:
    n个点,n-1条无向边,问去掉哪些点能够使得剩下所有的子树中节点数最多的子树的节点数最少,从小到大输出他们
    代码:
    //两遍dfs,第一次以1为根节点从下到上统计每个节点作为根的子树中共有几个节点,第二遍枚举
    //如果去掉某个点那么他的值是他的子节点构成的若干子树和1节点减去该节点形成的树中
    //节点数多的那个值,最后找到值最小的节点就行了。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int maxn=50009;
    int n,val[maxn],cnt[maxn],head[maxn],tol,ans[maxn];
    struct Edge{
        int to,w,next;
    }edge[maxn*2];
    void Add(int x,int y){
        edge[tol].to=y;
        edge[tol].next=head[x];
        head[x]=tol++;
    }
    void dfs1(int x,int fa){
        val[x]=1;
        for(int i=head[x];i!=-1;i=edge[i].next){
            int y=edge[i].to;
            if(y==fa) continue;
            dfs1(y,x);
            val[x]+=val[y];
        }
    }
    void dfs2(int x,int fa){
        int tmp=0;
        for(int i=head[x];i!=-1;i=edge[i].next){
            int y=edge[i].to;
            if(y==fa) continue;
            dfs2(y,x);
            tmp=max(tmp,val[y]);
        }
        cnt[x]=max(tmp,val[1]-val[x]);
    }
    int main()
    {
        while(scanf("%d",&n)==1){
            memset(head,-1,sizeof(head));
            tol=0;
            for(int i=1;i<n;i++){
                int x,y;
                scanf("%d%d",&x,&y);
                Add(x,y);Add(y,x);
            }
            dfs1(1,0);
            dfs2(1,0);
            int minx=cnt[1];
            for(int i=2;i<=n;i++)
                minx=min(minx,cnt[i]);
            int nu=0;
            for(int i=1;i<=n;i++)if(cnt[i]==minx){
                ans[++nu]=i;
            }
            for(int i=1;i<nu;i++) printf("%d ",ans[i]);
            printf("%d
    ",ans[nu]);
        }
        return 0;
    }
  • 相关阅读:
    P1169 [ZJOI2007]棋盘制作[悬线法/二维dp]
    P2279 [HNOI2003]消防局的设立[树形dp]
    Django项目部署
    Python3编译安装以及创建虚拟运行环境
    ASA与N6K对接
    Django使用admin管理后台管理数据库表
    WebStrom配置
    H3C常用配置和命令
    VPC配置介绍
    Linux下编译安装MySQL
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6873864.html
Copyright © 2020-2023  润新知