New Year Tree
time limit per test 2 seconds
memory limit per test 256 megabytes
You are a programmer and you have a New Year Tree (not the traditional fur tree, though) — a tree of four vertices: one vertex of degree three (has number 1 1 1), connected with three leaves (their numbers are from 2 2 2 to 4 4 4).
On the New Year, programmers usually have fun. You decided to have fun as well by adding vertices to the tree. One adding operation looks as follows:
First we choose some leaf of the tree with number v.
Let’s mark the number of vertices on the tree at this moment by variable n, then two vertexes are added to the tree, their numbers are
n
+
1
n+1
n+1 and
n
+
2
n+2
n+2, also you get new edges, one between vertices
v
v
v and
n
+
1
n+1
n+1 and one between vertices
v
v
v and
n
+
2
n+2
n+2.
Your task is not just to model the process of adding vertices to the tree, but after each adding operation print the diameter of the current tree. Come on, let’s solve the New Year problem!
Input
The first line contains integer
q
(
1
≤
q
≤
5
∗
1
0
5
)
q (1 ≤ q ≤ 5*10^5)
q(1 ≤ q ≤ 5∗105) — the number of operations. Each of the next
q
q
q lines contains integer
v
i
(
1
≤
v
i
≤
n
)
v_i (1 ≤ v_i ≤ n)
vi(1 ≤ vi ≤ n) — the operation of adding leaves to vertex
v
i
v_i
vi. Variable
n
n
n represents the number of vertices in the current tree.
It is guaranteed that all given operations are correct.
Output
Print q integers — the diameter of the current tree after each operation.
Examples
input
5
2
3
4
8
5
output
3
4
4
5
6
Solution
首先要知道一个定理,在一棵树中,每次加1个节点,直径最多增长1
而且一定是有一个顶点是没有移动的(感性认识一下就能发现)。
那么我们每次要一个节点新增两个相邻的节点该怎么办??
其实是跟增长一个节点一样的,因为这两个点是连向同一个点的
这样的话我们就只需要维护直径的两个端点就好了(有多个的话随便两个)
我们先令1为根,每次新增点的时候维护倍增数组
再与当前直径的两个端点用
l
c
a
lca
lca计算一下距离,如果直径大了就更新
时间复杂度是
O
(
n
l
o
g
n
)
O(nlogn)
O(nlogn)的
Warning
有一个需要特别注意的地方,数组的范围大小至少开两倍(因为要加两个点)
我就是因为没有注意这个细节结果TLE了???!!!
Code
#include <cstdio>
#include <algorithm>
#define N 2000010
#define P 21
using namespace std;
inline int read() {
int x = 0;
char ch = getchar();
while(ch < '0' || ch > '9') ch = getchar();
while(ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x;
}
struct Node{
int to, nxt;
}e[N];
int tot, cnt, q, d1, d2, prelen, lst[N], dep[N], c[N][P];
inline void add(int u, int v) {
e[++tot].to = v;
e[tot].nxt = lst[u];
lst[u] = tot;
}
void dfs(int x, int fa) {
dep[x] = dep[fa] + 1;
c[x][0] = fa;
for (int i = 1; i < P; ++i)
c[x][i] = c[c[x][i - 1]][i - 1];
for (int i = lst[x]; i; i = e[i].nxt) {
if (e[i].to == fa) continue;
dfs(e[i].to, x);
}
}
inline int lca(int x, int y) {
if (dep[x] != dep[y]) {
if (dep[x] < dep[y]) swap(x, y);
int jump = dep[x] - dep[y];
for (int i = 0; i < P; ++i)
if (jump & (1 << i)) x = c[x][i];
}
if (x == y) return x;
for (int i = P - 1; i >= 0; --i)
if (c[x][i] != c[y][i]) {
x = c[x][i];
y = c[y][i];
}
return c[x][0];
}
inline void init() {
add(1, 2);
add(1, 3);
add(1, 4);
cnt = 4;
q = read();
d1 = 2;
d2 = 3;
dfs(1, 1);
prelen = 2;
}
inline void solve() {
for (int i = 1; i <= q; i++) {
int x;
x = read();
add(x, ++cnt);
add(x, ++cnt);
dfs(x, c[x][0]);
int l1 = lca(x, d1);
int l2 = lca(x, d2);
int len1 = dep[x] + dep[d1] - dep[l1] * 2, len2 = dep[x] + dep[d2] - dep[l2] * 2;
if (len1 + 1 <= prelen && len2 + 1 <= prelen) printf("%d\n", prelen);
else
if (len1 > len2) {
prelen = len1 + 1;
printf("%d\n", prelen);
d2 = cnt;
}
else {
prelen = len2 + 1;
printf("%d\n", prelen);
d1 = cnt;
}
}
}
int main() {
init();
solve();
return 0;
}