AGC009D Uninity

AGC009D Uninity

Description

给定一棵树,求点分树的最小层数。

$n\le 10^5$

Solution

令 $x$ 在点分树上的深度为 $d_x$。

原树上的两个点,如果 $x,y$ 满足 $d_x=d_y$,则一定满足 $x\to y$ 的路径上有一个点 $z,d_z>d_x$。

令 $f_{x,i}$ 表示是否存在 $x$ 与深度为 $i$ 的点之间没有一个 $>i$ 的点。

然后贪心选择可以选的最小的。注意如果 $x$ 的两个子树 $u,v$,有 $f_{u,i}=f_{v,i}=1$,那么如果 $d_x\le i$,则连接 $u,v$ 后会不合法。

复杂度 $O(n\log n)$,但似乎可以做到 $O(n)$。

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
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define maxn 200005
#define put() putchar('\n')
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
using namespace std;
void read(int &x){
int f=1;x=0;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
x*=f;
}
namespace Debug{
Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;}
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
#define fi first
#define se second
#define mk make_pair
const int mod=1e9+7;
int power(int x,int y=mod-2) {
int sum=1;
while (y) {
if (y&1) sum=sum*x%mod;
x=x*x%mod;y>>=1;
}
return sum;
}
int g[maxn][21],f[maxn],n,nums[21],ans;
vector<int>to[maxn];
void dfs(int x,int pre) {
int i,j;
for (auto y:to[x]) if (y^pre) {
dfs(y,x);
}
for (i=0;i<=20;i++) nums[i]=0;
for (auto y:to[x]) if (y^pre) {
for (i=0;i<=20;i++) if (g[y][i]) nums[i]++;
}
int fl=0;
for (i=20;i>=0;i--) {
if (nums[i]==0) fl=i;
else if (nums[i]>1) break;
}
ans=max(ans,fl);nums[fl]=1;
for (i=fl;i<=20;i++) if (nums[i]) g[x][i]=1;
}
signed main(void){
int i,x,y;
read(n);
for (i=1;i<n;i++) {
read(x),read(y);
to[x].push_back(y);
to[y].push_back(x);
}
dfs(1,0);
printf("%d\n",ans);
return 0;
}