CF 342E Xenia and Tree(点分树)

题目描述

http://codeforces.com/problemset/problem/342/E

Solution

和捉迷藏类似,时间复杂度 $O(n\log ^2)$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <queue>
#define maxn 100010
#define INF 1000000000
using namespace std;

int n, m, col[maxn];

struct Edge {
int to, next;
} e[maxn * 2]; int c1, head[maxn];
inline void add_edge(int u, int v) {
e[c1].to = v; e[c1].next = head[u]; head[u] = c1++;
}

int id[maxn * 2], in[maxn], dep[maxn];
void dfs(int u, int fa) {
static int cnt = 0;
id[++cnt] = u; in[u] = cnt;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa) continue;
dep[v] = dep[u] + 1; dfs(v, u); id[++cnt] = u;
}
}

inline int st_min(int l, int r) { return in[l] < in[r] ? l : r; }

int st[maxn * 2][21], Log[maxn * 2];
void init_st(int n) { Log[0] = -1;
for (int i = 1; i <= n; ++i) Log[i] = Log[i >> 1] + 1;
for (int i = 1; i <= n; ++i) st[i][0] = id[i];
for (int j = 1; j <= 20; ++j)
for (int i = 1; i + (1 << j) - 1 <= n; ++i)
st[i][j] = st_min(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
}

inline int get_lca(int l, int r) {
l = in[l]; r = in[r]; if (l > r) swap(l, r);
int k = Log[r - l + 1];
return st_min(st[l][k], st[r - (1 << k) + 1][k]);
}

inline int D(int x, int y) { return dep[x] + dep[y] - 2 * dep[get_lca(x, y)]; }

struct Heap {
priority_queue<int, vector<int>, greater<int>> add, del;

inline void push(int x) { add.push(x); }

inline void erase(int x) { del.push(x); }

inline int top() {
while (!del.empty() && del.top() == add.top()) del.pop(), add.pop();
return add.top();
}

inline void pop() {
while (!del.empty() && del.top() == add.top()) del.pop(), add.pop();
add.pop();
}

inline bool empty() { return add.size() - del.size() == 0; }

inline int size() { return add.size() - del.size(); }
} h[maxn * 2];

bool vis[maxn];
namespace Calc_sz {
int f[maxn], sz[maxn];
int sum, rt;

void init() { f[rt = 0] = INF; }

void dfs_sz(int u, int fa) {
sz[u] = 1;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa || vis[v]) continue;
dfs_sz(v, u); sz[u] += sz[v];
}
}

void dfs(int u, int fa) {
f[u] = 0;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa || vis[v]) continue;
dfs(v, u); f[u] = max(f[u], sz[v]);
} f[u] = max(f[u], sum - sz[u]);
if (f[u] < f[rt]) rt = u;
}

inline int get_rt(int u) {
rt = 0; dfs_sz(u, 0); sum = sz[u];
dfs(u, 0); return rt;
}
}

int fa[maxn];
void divide(int u) {
vis[u] = 1;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to; if (vis[v]) continue;
v = Calc_sz::get_rt(v); fa[v] = u; divide(v);
}
}

void update(int u) {
int x = u; h[u].push(0);
while (fa[u]) {
if (h[u + n].size()) h[fa[u]].erase(h[u + n].top());
h[u + n].push(D(x, fa[u]));
if (h[u + n].size()) h[fa[u]].push(h[u + n].top());
u = fa[u];
}
}

int query(int u) {
int x = u, ans = INF; if (h[u].size()) ans = h[u].top();
while (fa[u]) {
if (h[u + n].size()) h[fa[u]].erase(h[u + n].top());
if (h[fa[u]].size()) ans = min(ans, D(fa[u], x) + h[fa[u]].top());
if (h[u + n].size()) h[fa[u]].push(h[u + n].top());
u = fa[u];
}
return ans;
}

inline void solve_1() {
int x; cin >> x;
update(x);
}

inline void solve_2() {
int x; cin >> x;
cout << query(x) << "\n";
}

int main() { fill(head, head + maxn, -1);
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

cin >> n >> m;
for (int i = 1; i < n; ++i) {
int x, y; cin >> x >> y;
add_edge(x, y); add_edge(y, x);
} dep[1] = 1; dfs(1, 0); init_st(2 * n - 1);
Calc_sz::init(); divide(Calc_sz::get_rt(1));
update(1);
for (int i = 1; i <= m; ++i) {
int opt; cin >> opt;
if (opt == 1) solve_1();
else solve_2();
}
return 0;
}