-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathE.cpp
86 lines (77 loc) · 1.71 KB
/
E.cpp
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
#include <bits/stdc++.h>
#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
const int MAXN = 2e5 + 5;
struct Edge{
int to,nxt;
}e[MAXN<<1];
int head[MAXN],cnt;
inline void add(int u,int v){
e[++cnt] = (Edge){v,head[u]};head[u] = cnt;
e[++cnt] = (Edge){u,head[v]};head[v] = cnt;
}
int n;
int lim;
bool flag;
int f[MAXN];
inline void dfs(int v,int fa=0){
int mx = -1,cmx = -1,mn = 1e9;
for(int i = head[v];i;i = e[i].nxt){
if(e[i].to == fa) continue;
dfs(e[i].to,v);mn = std::min(mn,f[e[i].to]);
if(f[e[i].to] > mx){
cmx = mx;mx = f[e[i].to];
}
else if(f[e[i].to] > cmx){
cmx = f[e[i].to];
}
}
if(mx == -1){
f[v] = 0;
return;
}
if(mx+2 <= lim){
f[v] = mn+1;
}
else if(mx == lim-1){
if(cmx == lim-1) flag = 0;
else f[v] = lim;
}
else{
flag = 0;
}
}
inline bool chk(int mid){
flag = 1;lim = mid;
dfs(1);
return flag;
}
inline void Solve(){
scanf("%d",&n);FOR(i,1,n) head[i] = 0;cnt = 0;
FOR(i,2,n){
int u,v;scanf("%d%d",&u,&v);add(u,v);
}
int l = 1,r = n,ans = -1;
while(l <= r){
int mid = (l + r) >> 1;
if(chk(mid)) ans = mid,r = mid-1;
else l = mid+1;
}
printf("%d\n",ans);
}
int main(){
int T;scanf("%d",&T);
while(T--) Solve();
return 0;
}