给定一棵nn个节点的树。

对于每个点单独考虑,删掉它,你可以把一个节点的父亲改成另一个节点(只能做一次),使森林中最大的树sizesize最小。对每个点求出这个答案

n105n\le 10^5

CF768G

Solution

不难发现,在点 xx 处计算答案时,会从最大的联通块中选择一棵子树接到最小的联通块上

二分答案, 转化成区间存在性问题, 直接用multiset在每个点上维护子树内所有点的 sizesize 信息

此外还需要单独考虑一下子树外联通块的情况,并且这个点到根的这条链上的信息也要单独维护,因为它们的sizesize会减掉当前点的sizesize

树上启发式合并维护即可

具体细节见代码

Code

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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <bits/stdc++.h>

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }

inline void proc_status()
{
ifstream t ("/proc/self/status");
cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) <<endl;
}

template <typename T> T read ()
{
T sum = 0, fl = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

const int Maxn = 1e5 + 100;

int N;
int e, Begin[Maxn], To[Maxn << 1], Next[Maxn << 1];
int size[Maxn], son[Maxn], snd[Maxn], min_size[Maxn];
int Ans[Maxn];

multiset <int> S[4];
//S[0]: anc, S[1]: heavy, S[2]: light, S[3]: other

inline void add_edge (int x, int y) { To[++e] = y; Next[e] = Begin[x]; Begin[x] = e; }

inline void dfs_pre (int x)
{
size[x] = 1, min_size[x] = 0x3f3f3f3f;

for (int i = Begin[x]; i; i = Next[i])
{
int y = To[i];
dfs_pre(y);
size[x] += size[y];
if (size[y] > size[son[x]]) snd[x] = size[son[x]], son[x] = y;
else if (size[y] > snd[x]) snd[x] = size[y];
Chkmin(min_size[x], size[y]);
}

if (size[x] != N) Chkmin(min_size[x], N - size[x]);
}

inline void Modify (int x, int val, int k)
{
if (val == 1) S[k].insert(x), S[3].erase(S[3].find(x));
else S[k].erase(S[k].find(x)), S[3].insert(x);
}

inline void Add (int x, int k)
{
Modify (size[x], 1, k);
for (int i = Begin[x]; i; i = Next[i]) Add (To[i], k);
}

inline void Erase (int x, int k)
{
Modify (size[x], -1, k);
for (int i = Begin[x]; i; i = Next[i]) Erase (To[i], k);
}

inline int Find (int Min, int Max, int l, int r, int k, int delta)
{
if (Min == Max && l <= Min && Min <= r) return Min;

int ans = 0x3f3f3f3f;
while (l <= r)
{
int mid = l + r >> 1;
multiset <int> :: iterator x = S[k].lower_bound(Max - mid + delta);
if (x != S[k].end() && (*x) <= mid - Min + delta) r = mid - 1, ans = mid;
else l = mid + 1;
}
return ans;
}

inline void Calc (int x)
{
if (!son[x]) { Ans[x] = N - size[x]; return; }
if (size[x] == N || size[son[x]] > N - size[x]) // max subtree is heavy
Ans[x] = Find (min_size[x], size[son[x]], max(N - size[x], snd[x]), size[son[x]], 1, 0);
else // root is in the max subtree
Ans[x] = min(Find (min_size[x], N - size[x], size[son[x]], N - size[x], 0, size[x]),
Find (min_size[x], N - size[x], size[son[x]], N - size[x], 3, 0));
}

multiset <int> :: iterator it, Save[Maxn];

inline void dfs (int x, int flag)
{
Modify (size[x], 1, 0);

for (int i = Begin[x]; i; i = Next[i])
{
int y = To[i];
if (y != son[x]) dfs(y, 0);
}

if (son[x]) dfs(son[x], 1);

for (int i = Begin[x]; i; i = Next[i])
{
int y = To[i];
if (y != son[x]) Add (y, 2);
}

S[0].erase(S[0].find(size[x]));
Calc(x);
S[3].insert(size[x]);

if (flag)
{
Modify(size[x], 1, 1);
int top = 0;
for (it = S[2].begin(); it != S[2].end(); ++it) S[1].insert(*it), Save[++top] = it;
for (int i = 1; i <= top; ++i) S[2].erase(Save[i]);
}
else
{
for (int i = Begin[x]; i; i = Next[i])
{
int y = To[i];
if (y != son[x]) Erase (y, 2);
else Erase (y, 1);
}
}
}

int root;

inline void Solve ()
{
dfs_pre (root);
for (int i = 1; i <= N; ++i) S[3].insert(size[i]);

dfs(root, 0);

for (int i = 1; i <= N; ++i) printf("%d\n", Ans[i]);
}

inline void Input ()
{
N = read<int>();
for (int i = 1; i <= N; ++i)
{
int x = read<int>(), y = read<int>();
if (!x) { root = y; continue; }
add_edge (x, y);
}
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("G.in", "r", stdin);
freopen("G.out", "w", stdout);
#endif
Input();
Solve();
return 0;
}