POJ 2378——Tree Cutting【树形DP & 删边 & 树重心】

题目传送门


Description

After Farmer John realized that Bessie had installed a “tree-shaped” network among his N (1 <= N <= 10,000) barns at an incredible cost, he sued Bessie to mitigate his losses.

Bessie, feeling vindictive, decided to sabotage Farmer John’s network by cutting power to one of the barns (thereby disrupting all the connections involving that barn). When Bessie does this, it breaks the network into smaller pieces, each of which retains full connectivity within itself. In order to be as disruptive as possible, Bessie wants to make sure that each of these pieces connects together no more than half the barns on FJ.

Please help Bessie determine all of the barns that would be suitable to disconnect.


Input

  • Line 1: A single integer, N. The barns are numbered 1…N.

  • Lines 2…N: Each line contains two integers X and Y and represents a connection between barns X and Y.
    Output

  • Lines 1…?: Each line contains a single integer, the number (from 1…N) of a barn whose removal splits the network into pieces each having at most half the original number of barns. Output the barns in increasing numerical order. If there are no suitable barns, the output should be a single line containing the word “NONE”.


Sample Input

10
1 2
2 3
3 4
4 5
6 7
7 8
8 9
9 10
3 8


Sample Output

3
8


Hint

INPUT DETAILS:

The set of connections in the input describes a “tree”: it connects all the barns together and contains no cycles.

OUTPUT DETAILS:

If barn 3 or barn 8 is removed, then the remaining network will have one piece consisting of 5 barns and two pieces containing 2 barns. If any other barn is removed then at least one of the remaining pieces has size at least 6 (which is more than half of the original number of barns, 5).


Source

USACO 2004 December Silver


题意

给一棵n个结点的树。求去掉哪个点能使得剩下的每个连通子图的节点数不超过n/2.输出满足条件的点。
如果有多个这样的点,则升序输出。
如果没有这样的点,输出NONE,但是一定会有这样的点,嘿嘿嘿~


题解

  • 树的重心模板题
  • 树重心链接

AC-Code

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int INF = 0x7fffffff;
const int MAXN = 5e4 + 5;
const int MOD = 1e9 + 7;

struct Edge {
	int next;
	int to;
}edge[MAXN << 1];
int head[MAXN];
int num;
int root[MAXN];
int son[MAXN];
int f[MAXN];
int n;
int cnt;
void getroot(int now, int fa) {
	son[now] = 1;
	f[now] = 0;
	for (int i = head[now]; ~i; i = edge[i].next) {
		if (edge[i].to == fa)	continue;
		getroot(edge[i].to, now);
		son[now] += son[edge[i].to];
		f[now] = max(f[now], son[edge[i].to]);
	}
	f[now] = max(f[now], n - son[now]);
	if (f[now] < f[root[num]]) {
		num = 0;
		root[num] = now;
	}
	else if (f[now] == f[root[num]]) {
		root[++num] = now;
	}

}
void addEdge(int u, int v) {
	edge[cnt].to = v;
	edge[cnt].next = head[u];
	head[u] = cnt++;
}
int main() {
	int a, b;
	ios;
	while (~scanf("%d", &n)) {
		memset(head, -1, sizeof head);
		cnt = 0;
		for (int i = 1; i < n; i++) {
			scanf("%d%d", &a, &b);
			addEdge(a, b);
			addEdge(b, a);
		}
		memset(root, 0, sizeof root);
		f[0] = n;
		num = 0;
		getroot(1, 0);
		sort(root, root + num + 1);
		for (int i = 0; i <= num; i++) {
			printf("%d\n", root[i]);
		}
	}

	return 0;
}

你可能感兴趣的:(DP,ACM_POJ,树形DP,树重心)