Poj 2599 Godfather(树的重心)

本文介绍了一个基于树结构的问题,即如何找到树中的重心节点。通过分析警察试图找出芝加哥黑手党头目的背景故事,文章详细解释了算法实现过程,包括输入输出格式、核心数据结构与算法步骤。

Godfather
Time Limit: 2000MS Memory Limit: 65536K
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
Source
Northeastern Europe 2005, Northern Subregion

/*
找树的重心们.
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define MAXN 50001
using namespace std;
int n,m,rt,f[MAXN],ans[MAXN],tot,sum,cut,head[MAXN],size[MAXN];
struct edge{int v,next;}e[MAXN*2];
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
    return x*f;
}
void add(int u,int v)
{
    e[++cut].v=v;e[cut].next=head[u];head[u]=cut;
}
void Clear()
{
    memset(size,0,sizeof size);
    memset(head,0,sizeof head);
    memset(f,0,sizeof f);
    cut=rt=tot=0;
}
void slove(int u,int fa)
{
    size[u]=1;
    for(int i=head[u];i;i=e[i].next)
    {
        if(e[i].v==fa) continue;
        slove(e[i].v,u);
        size[u]+=size[e[i].v];
        f[u]=max(f[u],size[e[i].v]);
    }
    f[u]=max(f[u],sum-size[u]);
    if(f[rt]>f[u]) rt=u,ans[tot=1]=u;
    else if(f[rt]==f[u]) ans[++tot]=u;
}
int main()
{
    int t,x,y;
    n=read();
    Clear();
    for(int i=1;i<=n-1;i++)
    {
        x=read(),y=read();
        add(x,y),add(y,x);
    }
    sum=n;f[0]=1e9;
    slove(1,rt);
    sort(ans+1,ans+tot+1);
    for(int i=1;i<=tot;i++) printf("%d ",ans[i]);
    printf("\n");
    return 0;
}
### POJ 树的重心问题解法 树的重心问题在POJ平台上的经典题目是 **POJ1655**。该问题的核心在于通过深度优先搜索(DFS)计算每个节点的子树大小,并进一步确定删除某个节点后,剩余部分的最大子树大小。最终目标是找到一个节点,使得删除该节点后,剩余的最大子树大小最小。 以下是关于该问题的具体解法和代码实现: #### 问题描述 给定一棵树,要求找到树的重心树的重心定义为:删除某个节点后,所有生成的连通分量中,最大连通分量的节点数尽可能小。如果存在多个满足条件的节点,则输出编号最小的节点。 #### 解法思路 1. 使用 DFS 遍历整棵树,计算每个节点的子树大小 `son[u]`。 2. 在 DFS 过程中,对于每个节点 `u`,记录其所有子树的最大节点数 `Max`。 3. 计算当前节点 `u` 的父节点延伸出去的节点数目 `n - son[u]`。 4. 确定当前节点 `u` 删除后,剩余的最大子树大小 `tmp = max(Max, n - son[u])`。 5. 更新答案,选择使得 `tmp` 最小的节点作为重心。若 `tmp` 相等,则选择编号较小的节点。 #### 代码实现 以下是一个基于 C++ 的完整实现: ```cpp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 20005; int head[N], top = 0; int n; int son[N]; int ans, point; struct Edge { int v, next; } edge[N * 2]; void init() { memset(head, -1, sizeof(head)); top = 0; memset(son, 0, sizeof(son)); ans = n + 1; // 初始化为一个较大值 } void addedge(int u, int v) { edge[top].v = v; edge[top].next = head[u]; head[u] = top++; } void dfs(int u, int fa) { son[u] = 1; int Max = 0; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].v; if (v == fa) continue; dfs(v, u); son[u] += son[v]; Max = max(Max, son[v]); } int tmp = max(Max, n - son[u]); if (tmp < ans || (tmp == ans && u < point)) { ans = tmp; point = u; } } int main() { int T; scanf("%d", &T); while (T--) { init(); scanf("%d", &n); int u, v; for (int i = 1; i < n; i++) { scanf("%d%d", &u, &v); addedge(u, v); addedge(v, u); } dfs(1, -1); printf("%d %d\n", point, ans); } return 0; } ``` #### 关键点解释 1. **初始化**:使用 `init()` 函数清空全局变量,确保每次测试用例独立运行[^3]。 2. **边的存储**:采用邻接表存储树的结构,便于快速访问每个节点的子节点。 3. **DFS 遍历**:通过递归方式计算每个节点的子树大小,并更新最大子树大小。 4. **结果更新**:在遍历过程中,实时更新最优解,确保最终答案满足题意。 #### 时间复杂度 - **DFS 遍历**:每个节点和边仅被访问一次,时间复杂度为 \(O(n)\)。 - **总复杂度**:对于多组测试数据,时间复杂度为 \(O(T \cdot n)\),其中 \(T\) 是测试用例数量,\(n\) 是节点数量。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值