本文主要是介绍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;
}
这篇关于Godfather (树形dp + 树的重心)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!