E. Tree Folding
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
Vanya wants to minimize a tree. He can perform the following operation multiple times: choose a vertex v, and two disjoint (except for v) paths of equal length a0 = v, a1, …, ak, and b0 = v, b1, …, bk. Additionally, vertices a1, …, ak, b1, …, bk must not have any neighbours in the tree other than adjacent vertices of corresponding paths. After that, one of the paths may be merged into the other, that is, the vertices b1, …, bk can be effectively erased:
Help Vanya determine if it possible to make the tree into a path via a sequence of described operations, and if the answer is positive, also determine the shortest length of such path.
Input
The first line of input contains the number of vertices n (2 ≤ n ≤ 2·105).
Next n - 1 lines describe edges of the tree. Each of these lines contains two space-separated integers u and v (1 ≤ u, v ≤ n, u ≠ v) — indices of endpoints of the corresponding edge. It is guaranteed that the given graph is a tree.
Output
If it is impossible to obtain a path, print -1. Otherwise, print the minimum number of edges in a possible path.
Examples
input
6
1 2
2 3
2 4
4 5
1 6
output
3
input
7
1 2
1 3
3 4
1 5
5 6
6 7
output
-1
Solution
官方的题解:
Let’s look at the performed actions in reverse. First, we have some path of odd length (by saying length we mean the number of edges} and double it several times. Now we do several “unfoldings”. Among two leaves of this path exactly one (or its copy) participate in each unfolding; depending on it we call the unfolding “left” or “right”. Note that left and right unfoldings have no edges in common; thus there is some vertex on the path which is not being unfolded. Let’s call this vertex root.
Here is a criterion that a vertex can be a valid root. Root the tree at it and look at its certain subtree: the depths of all leaves there must be equal. Moreover, among all subtrees of the root there must be not more than 2 different depths of the leaves. This criterion follows directly if you look at the sequence of unfoldings.
Now we have a solution: for each directed edge in a tree compute a set of depths to the leaves by a 2-way tree DP (actually, it must be computed only if its size is at most 1). Afterwards for each vertex check the root criterion.
However, there is an idea which makes the solution simpler: the midpoint of the diameter of the given tree is always an appropriate root. Given this, we should only run a standard tree DP which checks if all leaves in a subtree have the same depth.
Here is the outline of a proof: in the path from the first paragraph select the leftmost and the rightmost possible root, now look through all possible distances from left root to the left leaf and from the right root to the right leaf. There are several configurations which are easy to check manually.
我的做法,从叶节点开始,按层进行广搜,边搜边删边。按层实际上就是确保高度相同,方便折叠。
如果有个节点有3个或以上的高度,那他无法折叠,输出-1
当遍历到节点没有边时说明已经折叠完成
AC Code
#include <iostream>
#include <set>
#include <queue>
using namespace std;
const int maxn = static_cast<const int>(2e5 + 10);
bool visited[maxn];
int n;
set<int> lists[maxn], possibleDepths[maxn];
int outDeg[maxn];
void removeEdge(int u, int v) {
lists[u].erase(v);
lists[v].erase(u);
--outDeg[u];
--outDeg[v];
}
void addEdge(int u, int v) {
lists[u].insert(v);
lists[v].insert(u);
++outDeg[u];
++outDeg[v];
}
int resolve(int foo) {
while (foo % 2 == 0) foo /= 2;
return foo;
}
//void dfs(int u, int father){
// for(auto )
//}
int bfs() {
queue<int> que;
for (int i = 1; i <= n; i++) {
if (lists[i].size() == 1) {
que.push(i);
possibleDepths[i].insert(0);
}
}
while (!que.empty()) {
int u = que.front();
que.pop();
if (visited[u])
continue;
// cout << u << " : size " << possibleDepths[u].size() << endl;
if (lists[u].empty()) {
if (possibleDepths[u].size() > 2)
return -1;
else {
int res = 0;
for (auto k:possibleDepths[u]) {
res += k;
}
return res;
}
} else if (lists[u].size() == 1 && possibleDepths[u].size() == 1) {
visited[u] = true;
// cout << u << " " << v << endl;
int v = *lists[u].begin(), dis = *possibleDepths[u].begin();
// cout << dis << end;
possibleDepths[v].insert(dis + 1);
removeEdge(u, v);
que.push(v);
}
}
return -1;
}
int main() {
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int u = 0, v = 0;
cin >> u >> v;
addEdge(u, v);
}
int res = bfs();
// if(res != -1)
// cout << resolve(res);
// else cout <<
cout << resolve(res) << endl;
return 0;
}