POJ 3107 Godfather————樹形dp 求樹的重心

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

 

思路:

求樹的重心模板題。

六個人,組成一個有上下級的樹形網絡(上下級關係未知)。頭目的位置是:如果刪除頭目,那麼剩下的最大的子樹要儘可能小。求這樣的所有可能頭目。即求樹的重心。

對於樣例的解釋:

首先構造一個無根樹,如圖:

然後隨便假定一個節點爲根,這裏假定節點1爲根,然後無根樹會變成有根樹:

然後,我們找出每個節點的最大子樹的節點數,(紅色的是該節點最大子樹的節點數)(向父親方向的也算作考慮在內的一顆子樹)

然後,我們找出這些紅色數字中最小的那些對應節點,如圖:

所以,2和3就是這棵樹的重心。

 

AC代碼:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>

#define MAXN 50005

using namespace std;

int sz[MAXN], father[MAXN], n, MIN = 0x3f3f3f3f;
int to[MAXN << 1], Next[MAXN << 1], head[MAXN], tot;
vector<int> ans;

void add_edge(int u, int v) {
	to[tot] = v;
	Next[tot] = head[u];
	head[u] = tot++;
}

void dfs(int node, int pre) {
	sz[node] = 1;
	int MAX = 0;
	for (int i = head[node]; ~i; i = Next[i]) {
		int  v = to[i];
		if (v != pre) {
			dfs(v, node);
			sz[node] += sz[v];
			MAX = max(MAX, sz[v]);
		}
	}
	MAX = max(MAX, n - sz[node]);
	if (MAX < MIN) {
		ans.clear();
		MIN = MAX;
		ans.push_back(node);
	}
	else if (MAX == MIN) {
		ans.push_back(node);
	}
}

int main()
{
	tot = 0;
	memset(head, -1, sizeof(head));
	scanf("%d", &n);
	for (int i = 1; i <= n-1; i++) {
		int u, v;
		scanf("%d%d", &u, &v);
		add_edge(u, v);
		add_edge(v, u);
	}
	dfs(1, 0);
	sort(ans.begin(), ans.end());
	for (int i = 0; i < ans.size(); i++) {
		printf("%d", ans[i]);
		if(i != ans.size()){
			printf(" ");
		}
	}

	return 0;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章