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]);

           }

    }



  • 相关阅读:
    Ajax
    对于本地上关联了远程的git上的项目,删除现有的关联,然后关联新创建的git项目
    UNI-APP_uni-simple-router的快速上手(路由,nui路由拦截)
    js之观察者模式和发布订阅模式区别
    Vue CLI 3.0脚手架如何在本地配置mock数据
    es6解构赋值和扩展运算符
    js Array数组详细操作方法及解析合集
    list排序
    java常用的object数据处理
    java 读写 txt 文件
  • 原文地址:https://www.cnblogs.com/Hilda/p/2617251.html
Copyright © 2011-2022 走看看