动态规划 DP-1 与邻接表:Godfather POJ - 3107

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
问题概述
第一行输入n表示一共有n个人,接下来的n-1行输入相互交谈的两个人,这两个人的上下级关系未知。已知每一个人之和和他的直接下属和直接上司交谈,下属可以有多个,上司只有一个。而且已知,可以将他们的关系看成一棵树,Godfather是这样一个节点,把它从树上删除后剩余子树的最大值要尽可能小。
问题分析
首先介绍一下经常会用到的邻接表的建立,来存储一棵树。

#define maxn 50002

struct nod{
    int v;//某个父亲节点的孩子节点
    int next;//指向下一个兄弟节点
};

nod e[2*maxn];//按照0,1,2。。。n的顺序存储nod
int head[maxn];//表头
int e_size;//按照0,1,2。。。n的顺序递增

void addEdge(int v,int u){
    e[e_size].v=u;
    e[e_size].next=head[v];//新增孩子节点
    head[v]=e_size++;//改变表头的指向
}//新增一个链表的节点,存入到e[e_size]中,并指向链表的第一个节点再用表头指向该节点,就这样插入到表头和第一个节点之间成为头节点。

这里写图片描述

接下来介绍具体的计算过程:
首先广度优先遍历整棵树,计算以每一个节点为根节点所在树的大小num_Node[i]。
再次广度优先遍历整棵树,计算删除当前节点后剩余的连接部分的大小,取最大值del_Node[i]。
设i_j为i的孩子节点,del_Node[i]=max(num_Node[i_j],n-num_Node[i]);
i_j=i_1,i_2,i_3,,,,,,i_n;
最后取最大值的最小值。
result=min(del_Node[i]);i=1,2,3,,,,,n;
代码实现

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

#define maxn 50002

struct nod{
    int v;
    int next;
};

nod e[2*maxn];
int head[maxn];
int e_size;
int num_Node[maxn];
int del_Node[maxn];

int bfs(int root,int father,int n){
    int sum=0;
    int temp;
    int maxi=-1;
    for(int j=head[root];j!=-1;j=e[j].next)
        if(e[j].v!=father){
        temp=bfs(e[j].v,root,n);
        sum+=temp;
        if(temp>maxi)maxi=temp;
    }
        num_Node[root]=sum+1;
        del_Node[root]=max(maxi,n-num_Node[root]);
        return num_Node[root];
}
void addEdge(int v,int u){
    e[e_size].v=u;
    e[e_size].next=head[v];
    head[v]=e_size++;
}


int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<n+1;i++){
        head[i]=-1;
        num_Node[i]=0;
        del_Node[i]=0;
    }
    e_size=0;
    int v,u;
    for(int i=1;i<n;i++){
        scanf("%d %d",&v,&u);
        addEdge(v,u);
        addEdge(u,v);
    }
    bfs(1,-1,n);
    int min=n+1;
    for(int i=1;i<n+1;i++)
        if(del_Node[i]<min)min=del_Node[i];
    bool first=true;
    for(int i=1;i<n+1;i++)
        if(del_Node[i]==min){
            if(first){first=false;printf("%d",i);}
            else printf(" %d",i);
        }
    printf("\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值