Gravity Codeforces 1182D Complete Mirror fucks tree / tree diameter / topological sort

Meaning of the questions: give your tree, ask if there is a Fengyun tree root, such that for any two points, if they are the same distance from the root, then their degree must be equal.

1 idea: the center of gravity of the tree fucks

According to a sample found that the center of gravity of the tree could be the answer, so we can first determine what the focus of the tree of Can. If not, the rest can only be a degree of 1:00 when the root. Of course, we can not enumerate all of point 1, or a chrysanthemum Figure overtime, and my approach is for the center of gravity is the root of the tree search again, for each degree of the depth of point 1 of only a record, then enumerate these points, if there is to have, otherwise no. The worst such complexity should be able to O (n * sqrt (n)), but certainly run discontent. As for why to do this because if there is more depth of feeling for each point, or pick a can or can not. I did not expect the court so too, wish to comment on the words of district chiefs out of a can hack data. . .

Code:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define db double
#define LL long long
#define pii pair<int, int>
using namespace std;
const int maxn = 100010;
vector<int> G[maxn];
void add(int x, int y) {
	G[x].push_back(y);
	G[y].push_back(x);
}
bool flag = 0;
bool v[maxn];
int sz[maxn], n, re[maxn];
int years = INF root;
map<int, int> mp;
map<int, int> ::iterator it;
void dfs(int x) {
	v[x] = 1;
	No. [x] = 1;
	int max_part = 0;
	for (auto y : G[x]) {
		if(v[y]) continue;
		dfs(y);
		No. [x] + = c [gamma];
		max_part = max(max_part, sz[y]);
	}
	max_part = max(max_part, n - sz[x]);
	if(max_part < ans) {
		= max_part years;
		root = x;
	}
}
void dfs1(int x, int fa, int deep) {
	if(re[deep] == 0) {
		re[deep] = G[x].size();
	} else {
		if(re[deep] != G[x].size()) {
			flag = 1;
		//	return;
		}
	}
	No. [x] = 1;
	for (auto y : G[x]) {
		if(y == fa) continue;
		dfs1(y, x, deep + 1);
		//if(flag == 1) return;
		No. [x] + = c [gamma];
	}
	if(G[x].size() == 1) {
		mp[deep] = x;
	}
}
bool solve() {
	flag = 0;
	memset(re, 0, sizeof(re));
	for (auto y : G[root]) {
		dfs1(y, root, 1);
	}
	int now = 0;
	for (auto y : G[root]) {
		if(now == 0) now = sz[y];
		else {
			if(now != sz[y]) {
				flag = 1;
				return flag;
			}
		}
	}
	return flag;
}
int main () {
	int x, y;
	scanf("%d", &n);
	for (int i = 1; i < n; i++) {
		scanf("%d%d", &x, &y);
		add(x, y);
	}
	dfs(1);
	if(!solve()) {
		printf("%d\n", root);
		return 0;
	}
	else {
		for (it = mp.begin(); it != mp.end(); it++) {
			root = it -> second;
			if(!solve()) {
				printf("%d\n", root);
				return 0;
			}
		}
	}
	printf("-1\n");
} 

 Official explanations ideas :( 2) run over tree diameter, first determine the diameter of both ends okay, if not, starting from the midpoint diameter, without looking side to reach the degree of the diameter of 1 point, judgment okay .

 The code cushions

   3 :( official explanations idea) run again topological sorting, until only one point, and then find the nearest and farthest points of degree 1 point, the judge okay.

   Code is also a goo

  2 and 3 only thinking ideas and intuitive feel like the right QAQ, will not permit, the court could not think of this line of thinking, the only fucks water a water QAQ.

  The official solution to a problem in conjunction with Figure 2 should be a better understanding of ideas and thoughts 3:

  Official explanations: https://codeforces.com/blog/entry/67614

Guess you like

Origin www.cnblogs.com/pkgunboat/p/11012957.html