zoukankan      html  css  js  c++  java
  • POJ 3107 Godfather 【树形DP】

    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 ai, bi 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

    思路

    和树形DP3类型一样,只不过他是要求去掉某个节点后得到子树节点和最小值,再求总的子树的结点和的最大值。

    但是这道题有一点很让人蛋疼,就是vector会超时。因此要用邻接表,进行存边。

    源码

    #include<stdio.h>

    #include<string.h>

    #include<algorithm>

    #include<iostream>

    #define INF 0xfffffff

    using namespace std;

    int head[50005], visit[50005], dp[50005], num[50005];

    int k, MIN, t, n;

    struct Edge

    {

           int v, next;     

    }edge[50005*2];

    void addedge(int aa, int bb)

    {

           edge[k].v=bb;

           edge[k].next=head[aa];

           head[aa]=k;

           k++;

    }

    void dfs(int tt)

    {

           int i,minn=-1;

           visit[tt]=1;

           dp[tt]=1;

           for(i=head[tt]; i; i=edge[i].next)

           {

                  int vv=edge[i].v;

                  if(!visit[vv])

                  {

                         dfs(vv);

                         dp[tt]+=dp[vv];

                         minn=max(minn, dp[vv]);

                  }

           }

           minn=max(minn, n-dp[tt]);

           if(minn<MIN)

           {

                  t=0;

                  num[t++]=tt;

                  MIN=minn;

           }

           else if(minn==MIN)

           {

                  num[t++]=tt;

           }

    }

    int main()

    {

           int i, j, a, b;

           while(scanf("%d", &n)!=EOF)

           {

                  memset(head, 0, sizeof(head));

                  memset(dp, 0, sizeof(dp));

                  memset(visit, 0, sizeof(visit));

                  memset(num, 0, sizeof(num));

                  k=1;

                  for(i=0; i<n-1; i++)

                  {

                         scanf("%d%d", &a, &b);

                         addedge(a, b);

                         addedge(b, a);      

                  }

                  MIN=INF;

                  dfs(1);

                  sort(num, num+t);

                  for(i=0; i<t-1; i++)

                         printf("%d ", num[i]);

                  printf("%d\n", num[t-1]);

           }

    }



  • 相关阅读:
    MySQLday04(剩余窗口函数,其他常用函数,存储引擎,如何选择数据类型,字符集,索引,存储过程,触发器,LOCK TABLES 和 UNLOCK TABLES,事务控制,分布式事务的使用,JDBC)
    MySQLday03(JSON类型,算术运算符,比较运算符,逻辑运算符,位运算符,运算符的优先级,字符串函数,数值函数,日期和时间函数,流程函数,JSON函数,窗口函数)
    Mysqlday02(导入外部sql文件,order by,group by,having,多表联查,查元数据,数值类型,日期类型,字符数据类型)
    Mysqlday01(Mysql简介,sql简介,分类)
    Spring Boot 入门实战(6)--JdbcTempalte、Mybatis 、多数据源及 Atomicos 整合(XA 事务)
    Spring Boot 入门实战(5)--JdbcTempalte、Mybatis及多数据源整合(单库事务)
    Java 操作 XML(11)--XMLBeans 使用
    Qt QThread 创建多线程程序
    C++Primer第五版 第十三章 拷贝控制
    QtCreator float与QString之间的转化
  • 原文地址:https://www.cnblogs.com/Hilda/p/2617251.html
Copyright © 2011-2022 走看看