#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define modwwe int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task2 "top1apio"
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".out","w",stdout)
#define pb push_back
#define mask(k) (1ll<<k)
#define checktime cerr << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
using namespace std;
#define getchar_unlocked getchar
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
using i128 = __int128;
int rand(int l, int r)
{
return uniform_int_distribution<int>(l, r)(rd);
}
void phongbeo();
const int inf = 1e9;
const int mod2 = 998244353;
//const ll base=67;
ll n, m, s1, s2, s4, s3, sf, k, s5, s6, s7, s8, s9, mx2, res, dem2 = 0, dem = 0, s33, dem3, dem4, mid, l2,
r2, center;
ll i, s10, s12, k1, k2, k3, s11, lim, w, l, r, dem5, dem6, dem7, dem9, root;
ll el = 19;
main()
{
///top1apio
if (fopen(task2".inp", "r"))
{
fin(task2);
fou(task2);
}
///top1apio
if (fopen(task".inp", "r"))
{
fin(task);
fou(task);
}
///top1apio
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
// cin>>s1;
ll t=1;
//cin>>t;
s10=t;
while(t--)
phongbeo();
/// checktime
///top1apio
}
int in[1000001],ou[1000001],par[1000001],dep[1000001],dp[1000001],ans[1000001];
int cost[1000001];
vector<int> v[1000001];
bool check(int x,int y)
{
if(in[x]<=in[y]&&in[y]<=ou[x])return 1;
return 0;
}
void dfs(int x,int y)
{
in[x]=++dem;
if(x!=m)
dep[x]=dep[y]+(v[x].size()-1);
par[x]=y;
for(auto f:v[x])
if(f^y)
{
dfs(f,x);
}
ou[x]=dem;
}
void des(int x,int y)
{
for(auto f:v[x])
if(f^y)
des(f,x);
if(v[x].size()==1||v[x].size()==2)
{
cost[x]=dep[x];
return;
}
s3=-1;
s5=0;
for(auto f:v[x])
if(f^y&&dep[f]>s3)
s3=dep[f],s5=f;
s3=-1;
for(auto f:v[x])
if(f^y&&f^s5)
s3=max(s3,dep[f]);
cost[x]=s3;
}
bool dg(int x,int y,int mx,int lx)
{
if(dep[x]>lx)return 0;
int s=0;
for(auto f:v[x])
if(f^y&&cost[f]<=lx)s++;
if(s<=mx+1)return 1;
return 0;
}
bool check(int x)
{
int lst=0;
s5=0;
int lim=k;
while(true)
{
s5++;
bool ok=0;
for(auto f:v[lim])
if(f^par[lim]&&f^lst)
{
if(s5>=v[lim].size()-3+(lst==0))
if(dg(f,lim,s5-v[lim].size()+3-(lst==0),x+(lst!=0)))
{
ok=1;
break;
}
}
if(!ok)
{
s5=s5-(v[lim].size()-1)+(lst!=0);
x-=(v[lim].size()-1)+(lst!=0);
}
else
{
dem=dem2=0;
for(auto f:v[lim])
if(f^par[lim]&&f^lst)
{
if(cost[f]>x+(lst!=0))
{
dem++;
break;
}
dem2++;
}
if(dem2==dem)
{
dem--;
}
s5-=dem;
x-=dem;
}
if(s5<0||x<0)return 0;
lim=par[lim];
if(lim==m)break;
}
return 1;
}
void phongbeo()
{
cin>>n>>m>>k;
for(int i=1; i<=n; i++)ans[i]=inf;
for(int i=1; i<n; i++)
cin>>l>>r,v[l].pb(r),v[r].pb(l);
dfs(m,0);
des(m,0);
l=0;
r=n-1;
while(l<=r)
{
int mid=l+r>>1;
if(!check(mid))l=mid+1;
else r=mid-1;
}
cout<<l-1;
}
Compilation message (stderr)
mousetrap.cpp:34:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
34 | main()
| ^~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:13:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | #define fin(x) freopen(x".inp","r",stdin)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
mousetrap.cpp:39:9: note: in expansion of macro 'fin'
39 | fin(task2);
| ^~~
mousetrap.cpp:14:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | #define fou(x) freopen(x".out","w",stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:40:9: note: in expansion of macro 'fou'
40 | fou(task2);
| ^~~
mousetrap.cpp:13:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | #define fin(x) freopen(x".inp","r",stdin)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
mousetrap.cpp:45:9: note: in expansion of macro 'fin'
45 | fin(task);
| ^~~
mousetrap.cpp:14:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | #define fou(x) freopen(x".out","w",stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:46:9: note: in expansion of macro 'fou'
46 | fou(task);
| ^~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |