Godfather (树形dp + 树的重心)

题目:Godfather

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-1行表示哪两个点相连。

思路:第三个树的重心问题。这个题更直接……直接输出树的重心。

定义dp数组表示删除第i个结点后剩下的子树和以i结点为根结点的子树节点数的最大值。

next1数组表示结点i的子结点有多少个。

状态转移方程见代码。

这个题开始用vector做会超时。

只能用struct node 建树做。

思路是一样的,只是建树方式不同。

源代码:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stdio.h>
#define maxn 100005
using namespace std;
struct Tree{
    int sta,end,next;
}tree[maxn];
int n;
bool visit[maxn];
int head[maxn];
int next1[maxn];
int sum;
int ans;
int dp[maxn];
int minn = 9999999;
void add(int x,int y){
    sum++;
    tree[sum].next = head[x];
    head[x] = sum;
    tree[sum].sta = x;
    tree[sum].end = y;
}
void tree_dp(int now){
    next1[now] = 1;
    visit[now] = 1;
    for (int i = head[now]; i != -1; i = tree[i].next){
        if (visit[tree[i].end])
            continue;
        tree_dp(tree[i].end);
        next1[now] += next1[tree[i].end];
        ans = max(ans,next1[tree[i].end]);//表示以节点i为父节点的子树中节点个数的最大值
    }
    dp[now] = max(ans,n-next1[now]);//表示删除节点i后形成的几个部分中节点个数的最大值
}
int main(){
    int x,y;
    while (scanf("%d",&n)!=EOF){
        memset(visit,0,sizeof(visit));
        memset(head,-1,sizeof(head));
        memset(dp,0,sizeof(dp));
        for (int i = 1; i < n; i++){
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        tree_dp(1);
        vector <int> v;
        for (int i = 1; i <= n; i++){
            if (dp[i] < minn){
                minn = dp[i];
                v.clear();
            }
            if (minn == dp[i])
                v.push_back(i);
        }
        for (int i = 0; i < v.size(); i++)
            printf("%d ",v[i]);
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值