// Remember...
#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int maxn=1e6+10,mod=1e9+7;
const ll inf=1e18;
vector<int> v[maxn], dps[maxn], path;
int dp[maxn], pr[maxn];
bool mark[maxn];
void dfs(int u,int par){
pr[u]=par;
int id=-1, cnt=0;
for(int y:v[u]){
if(y!=par){
cnt++;
dfs(y,u);
if(id==-1 || dp[id]<dp[y])
id=y;
}
}
for(int y:v[u]){
if(y!=par && y!=id)
dp[u]=max(dp[u], dp[y]);
}
dp[u]+=cnt;
}
bool ok(int lm){
int tot=0, usd=0;
for(int i=0;i<sz(path);i++){
tot+=sz(dps[i]);
}
for(int i=0;i<sz(path);i++){
usd++;
int pt=0, cnt=0;
while(pt<sz(dps[i]) && dps[i][pt]+tot>lm)
usd--, pt++, cnt++;
lm-=cnt;
tot-=sz(dps[i]);
if(usd<0 || lm<0) return 0;
}
return 1;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie();
int n,t,m; cin>>n>>t>>m;
for(int i=0;i<n-1;i++){
int a,b; cin>>a>>b;
v[a].PB(b);
v[b].PB(a);
}
dfs(t,-1);
int tmp=m;
while(tmp!=t){
mark[tmp]=1;
path.PB(tmp);
tmp=pr[tmp];
}
mark[t]=1;
for(int i=0;i<sz(path);i++){
for(int u:v[path[i]]){
if(mark[u]==0)
dps[i].PB(dp[u]);
}
sort(dps[i].begin(),dps[i].end(),greater<int>());
}
int l=-1,r=n-1;
while(r-l>1){
int mid=(l+r)>>1;
if(ok(mid)) r=mid;
else l=mid;
}
return cout<<r<<endl,0;
}
// Deathly mistakes:
// * Read the problem carefully.
// * Check maxn.
// * Overflows.
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
47352 KB |
Output is correct |
2 |
Correct |
47 ms |
47352 KB |
Output is correct |
3 |
Correct |
47 ms |
47352 KB |
Output is correct |
4 |
Correct |
46 ms |
47340 KB |
Output is correct |
5 |
Correct |
52 ms |
47352 KB |
Output is correct |
6 |
Correct |
50 ms |
47352 KB |
Output is correct |
7 |
Correct |
46 ms |
47352 KB |
Output is correct |
8 |
Correct |
50 ms |
47276 KB |
Output is correct |
9 |
Correct |
47 ms |
47292 KB |
Output is correct |
10 |
Correct |
47 ms |
47352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
451 ms |
86516 KB |
Output is correct |
2 |
Correct |
416 ms |
82728 KB |
Output is correct |
3 |
Correct |
991 ms |
87656 KB |
Output is correct |
4 |
Correct |
478 ms |
67552 KB |
Output is correct |
5 |
Correct |
1008 ms |
87772 KB |
Output is correct |
6 |
Correct |
1123 ms |
87624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
47352 KB |
Output is correct |
2 |
Correct |
47 ms |
47352 KB |
Output is correct |
3 |
Correct |
47 ms |
47352 KB |
Output is correct |
4 |
Correct |
46 ms |
47340 KB |
Output is correct |
5 |
Correct |
52 ms |
47352 KB |
Output is correct |
6 |
Correct |
50 ms |
47352 KB |
Output is correct |
7 |
Correct |
46 ms |
47352 KB |
Output is correct |
8 |
Correct |
50 ms |
47276 KB |
Output is correct |
9 |
Correct |
47 ms |
47292 KB |
Output is correct |
10 |
Correct |
47 ms |
47352 KB |
Output is correct |
11 |
Correct |
47 ms |
47312 KB |
Output is correct |
12 |
Correct |
47 ms |
47420 KB |
Output is correct |
13 |
Correct |
47 ms |
47352 KB |
Output is correct |
14 |
Correct |
49 ms |
47452 KB |
Output is correct |
15 |
Correct |
46 ms |
47480 KB |
Output is correct |
16 |
Correct |
47 ms |
47480 KB |
Output is correct |
17 |
Correct |
46 ms |
47352 KB |
Output is correct |
18 |
Correct |
47 ms |
47352 KB |
Output is correct |
19 |
Correct |
50 ms |
47224 KB |
Output is correct |
20 |
Correct |
48 ms |
47352 KB |
Output is correct |
21 |
Correct |
49 ms |
47360 KB |
Output is correct |
22 |
Correct |
47 ms |
47352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
47352 KB |
Output is correct |
2 |
Correct |
47 ms |
47352 KB |
Output is correct |
3 |
Correct |
47 ms |
47352 KB |
Output is correct |
4 |
Correct |
46 ms |
47340 KB |
Output is correct |
5 |
Correct |
52 ms |
47352 KB |
Output is correct |
6 |
Correct |
50 ms |
47352 KB |
Output is correct |
7 |
Correct |
46 ms |
47352 KB |
Output is correct |
8 |
Correct |
50 ms |
47276 KB |
Output is correct |
9 |
Correct |
47 ms |
47292 KB |
Output is correct |
10 |
Correct |
47 ms |
47352 KB |
Output is correct |
11 |
Correct |
451 ms |
86516 KB |
Output is correct |
12 |
Correct |
416 ms |
82728 KB |
Output is correct |
13 |
Correct |
991 ms |
87656 KB |
Output is correct |
14 |
Correct |
478 ms |
67552 KB |
Output is correct |
15 |
Correct |
1008 ms |
87772 KB |
Output is correct |
16 |
Correct |
1123 ms |
87624 KB |
Output is correct |
17 |
Correct |
47 ms |
47312 KB |
Output is correct |
18 |
Correct |
47 ms |
47420 KB |
Output is correct |
19 |
Correct |
47 ms |
47352 KB |
Output is correct |
20 |
Correct |
49 ms |
47452 KB |
Output is correct |
21 |
Correct |
46 ms |
47480 KB |
Output is correct |
22 |
Correct |
47 ms |
47480 KB |
Output is correct |
23 |
Correct |
46 ms |
47352 KB |
Output is correct |
24 |
Correct |
47 ms |
47352 KB |
Output is correct |
25 |
Correct |
50 ms |
47224 KB |
Output is correct |
26 |
Correct |
48 ms |
47352 KB |
Output is correct |
27 |
Correct |
49 ms |
47360 KB |
Output is correct |
28 |
Correct |
47 ms |
47352 KB |
Output is correct |
29 |
Correct |
46 ms |
47324 KB |
Output is correct |
30 |
Correct |
454 ms |
99840 KB |
Output is correct |
31 |
Correct |
550 ms |
99976 KB |
Output is correct |
32 |
Correct |
719 ms |
198952 KB |
Output is correct |
33 |
Correct |
533 ms |
193948 KB |
Output is correct |
34 |
Correct |
988 ms |
100760 KB |
Output is correct |
35 |
Correct |
1014 ms |
100828 KB |
Output is correct |
36 |
Correct |
1041 ms |
113692 KB |
Output is correct |
37 |
Correct |
1035 ms |
113684 KB |
Output is correct |
38 |
Correct |
852 ms |
115368 KB |
Output is correct |
39 |
Correct |
846 ms |
115460 KB |
Output is correct |
40 |
Correct |
876 ms |
115408 KB |
Output is correct |