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

           }

    }



  • 相关阅读:
    java笔记使用线程池优化多线程编程
    java笔记查看和修改线程名称
    java笔记查看和修改线程的优先级
    java笔记策略模式和简单工厂模式
    java笔记用ThreadLocal管理线程,Callable<V>接口实现有返回值的线程
    java笔记枚举总结与详解
    java笔记关于克隆技术
    java笔记反射机制之基础总结与详解
    java笔记使用事件分配线程更新Swing控件
    java笔记关于int和byte[]的转换
  • 原文地址:https://www.cnblogs.com/Hilda/p/2617251.html
Copyright © 2011-2022 走看看