#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
vi graph[1000001];
int tdist[1000001];
int ans[1000001];
int dp[1000001];
int n,t,m;
void dfs_tdist(int v, int pop, int d, int cur_ans = 0)
{
tdist[v] = d++;
cur_ans += siz(graph[v])-2;
if(siz(graph[v]) == 1) cur_ans++;
if(v == pop) cur_ans = 0;
ans[v] = cur_ans;
forall(it,graph[v])
{
if(it != pop)
{
dfs_tdist(it,v,d,cur_ans);
}
}
}
void dfs_mdist(int v, int pop, int min_dep)
{
min_dep = min(min_dep,tdist[v]);
ans[v] += tdist[v] - min_dep;
forall(it,graph[v])
{
if(it != pop)
{
dfs_mdist(it,v,min_dep);
}
}
}
void dfs(int v, int pop, int cur = 1, int cur_add = 0, int d = 0)
{
vi best;
int my_cur = cur;
if((v == pop && siz(graph[v]) == 1) || (v != pop && siz(graph[v]) == 2)) cur++;
else cur = max(1,cur-max(0,siz(graph[v])-2)+1);
forall(it,graph[v])
{
if(it != pop)
{
int new_add = cur_add;
if(tdist[m] - tdist[it] == d+1) new_add += min(max(0,(v != pop ? siz(graph[v])-2 : siz(graph[v])-1)),cur);
dfs(it,v,cur,new_add,d+1);
best.pb(dp[it]);
}
}
sort(all(best));
reverse(all(best));
cur = my_cur;
if(siz(best) >= cur+1)
{
dp[v] = best[cur];
}
else if(siz(best) >= 1)
{
forall(it,best)
{
dp[v] = min(ans[v]+cur_add + (tdist[m] - tdist[v] != d ? (siz(graph[v]) != 1 ? max(1,siz(graph[v])-2) : 0) : 0), it);
}
}
else
{
dp[v] = ans[v] + cur_add + (tdist[m] - tdist[v] != d ? (siz(graph[v]) != 1 ? max(1,siz(graph[v])-2) : 0) : 0);
}
if(v == t)
{
dp[v] = ans[m] + cur_add;
}
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random_start();
cin >> n >> t >> m;
rep(i,n-1)
{
int a,b;
cin >> a >> b;
graph[a].pb(b);
graph[b].pb(a);
}
dfs_tdist(t,t,0);
dfs_mdist(m,m,tdist[m]);
dfs(m,m);
cout << dp[m] << "\n";
}
# | 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... |