[JZOJ 5028] flea Kingdom

Description

Flea Kingdom unrest broke out, the king at the same time quell the riots, the country needs at the local flea handpicked prime minister a person to do.
However, when the Imperial way King is very strange, flea Kingdom can be seen as a tree, he considers that the prime minister must be a better position flea service, so he will choose a to all nodes in the distance and the smallest node, and this node in the King James, if there is a plurality of nodes and to meet the minimum distance a is optionally.
However, unrest flea country is too much, so the tree could also change the mentality is to say, the tree may have several edges disappear, if this happens, then there will be the same number of sides appear to ensure that the entire structure is still a tree.
Now the flea would like to know each node flea If you want to be King James, at least how many edges disappear (of course there are the same number of sides appear). As a true fan only fleas, you can help him solve this problem?

Input

The first line of a positive integer and n represents the number of points in the tree.
Next, n-1 lines of two positive integers u, v, expressed between a point and the point u v a side of the tree.

Output
Output n lines, a number of i-th row, represents the i-node if you want to be handpicked at least how many disappeared.

Sample Input

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

Sample Output

0
4
4
4
4
4
4
4
4
4

Data Constraint

Thinking

First find the center of gravity, then we need to find how many children focus deleted to make no more than half the size of the rest of the whole tree.
Then the answer can be found in other points of either the value minus one, or is this value, it is not difficult to determine.

Code

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 77;

int ls[N], current, n, siz[N], root, answer[N], acc, limit_id;
vector<int> vec;

struct E
{
	int to, nxt;
} e[N << 1];

bool compare(int a, int b) { return siz[a] > siz[b]; }

void addpath(int src, int dst)
{
	e[current].to = dst, e[current].nxt = ls[src];
	ls[src] = current++;
}

void dfs(int u, int fa)
{
	int max_son = 0;
	siz[u] = 1;
	for (int i = ls[u]; i != -1; i = e[i].nxt)
		if (e[i].to != fa)
			dfs(e[i].to, u), siz[u] += siz[e[i].to], max_son = max(max_son, siz[e[i].to]);
	max_son = max(max_son, n - siz[u]);
	if (max_son <= (n >> 1))
		root = u;
}

void getAns(int u, int fa)
{
	answer[u] = limit_id - (siz[u] + acc >= (n >> 1)) + 1;
	for (int i = ls[u]; i != -1; i = e[i].nxt)
		if (e[i].to != fa)
			getAns(e[i].to, u);
}

int main()
{
	freopen("flea.in", "r", stdin);
	freopen("flea.out", "w", stdout);
	memset(ls, -1, sizeof(ls));
	scanf("%d", &n);
	for (int i = 1, u, v; i <= n - 1; i++)
		scanf("%d%d", &u, &v), addpath(u, v), addpath(v, u);
	dfs(1, 0), dfs(root, 0);
	for (int i = ls[root]; i != -1; i = e[i].nxt)
		vec.push_back(e[i].to);
	sort(vec.begin(), vec.end(), compare);
	limit_id = -1;
	for (int i = 0, siz_ = vec.size(); i < siz_; i++)
	{
		acc += siz[vec[i]];
		if (acc >= (n >> 1))
		{
			limit_id = i;
			break;
		}
	}
	for (int i = 0; i <= limit_id; i++)
		acc -= siz[vec[i]], getAns(vec[i], root), acc += siz[vec[i]];
	if (limit_id != -1)
		acc -= siz[vec[limit_id]];
	for (int i = limit_id + 1, siz_ = vec.size(); i < siz_; i++)
		getAns(vec[i], root);
	for (int i = 1; i <= n; i++)
		printf("%d\n", answer[i]);
	return 0;
}
Published 703 original articles · won praise 392 · Views 140,000 +

Guess you like

Origin blog.csdn.net/Eric1561759334/article/details/104086295