树形dp&&树的重心(D - Godfather POJ - 3107)

时间:2023-03-09 19:09:52
树形dp&&树的重心(D - Godfather POJ - 3107)

题目链接:https://cn.vjudge.net/contest/277955#problem/D

题目大意:求树的重心(树的重心指的是树上的某一个点,删掉之后形成的多棵树中节点数最大值最小)。

具体思路:对于每一个点,我们求出以当前的点为根的根数的节点个数, 然后在求树的重心的时候,一共有两种情况,一种树去除该点后这个点的子节点中存在所求的最大值,还有一种情况是这个点往上会求出最大值,往上的最大值就是(n-dp[rt][0]).

AC代码:

 #include<iostream>
#include<cmath>
#include<stack>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
# define inf 0x3f3f3f3f
# define ll long long
const int maxn = 2e5+;
struct node
{
int nex;
int to;
} edge[maxn];
int num,head[maxn],dp[maxn][],father[maxn];
int sto[maxn],minn,n;
void init()
{
minn=inf;
num=;
memset(head,-,sizeof(head));
memset(dp,,sizeof(dp));
}
void addedge(int fr,int to)
{
edge[num].to=to;
edge[num].nex=head[fr];
head[fr]=num++;
}
void dfs1(int fr,int rt)
{
dp[fr][]=;
for(int i=head[fr]; i!=-; i=edge[i].nex)
{
int to=edge[i].to;
if(to==rt)
continue;
dfs1(to,fr);
dp[fr][]+=dp[to][];
}
}
void dfs2(int fr,int rt)
{
dp[fr][]=n-dp[fr][];
for(int i=head[fr]; i!=-; i=edge[i].nex)
{
int to=edge[i].to;
if(to==rt)
continue;
dfs2(to,fr);
dp[fr][]=max(dp[fr][],dp[to][]);
}
minn=min(minn,dp[fr][]);
}
int main()
{
init();
scanf("%d",&n);
int t1,t2;
for(int i=; i<=n; i++)
{
scanf("%d %d",&t1,&t2);
addedge(t1,t2);
addedge(t2,t1);
}
dfs1(,-);
dfs2(,-);
int flag=;
for(int i=; i<=n; i++)
{
if(dp[i][]==minn)
{
if(flag)
{
printf("%d",i);
flag=;
}
else
printf(" %d",i);
}
}
printf("\n");
return ;
}