This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define task "asd"
#define pll pair<ll, ll>
#define pii pair<pll, ll>
#define fi first
#define se second
using namespace std;
const ll mod = 3;
const ll N = 4e5+5;
const int base = 313;
ll n, k, m, tong, ans, t, a[N], b[N], d[N], s;
ll pw(ll k, ll n)
{
ll total = 1;
for(; n; n >>= 1)
{
if(n & 1)total = total * k % mod;
k = k * k % mod;
}
return total;
}
vector<ll> kq, adj[N];
void add(ll id, ll val)
{
for(; id <= n; id += id & -id)b[id] = max(b[id], val);
}
ll get(ll id)
{
ll total = 0;
for(; id; id -= id & -id)total = max(total, b[id]);
return total;
}
struct node
{
ll A, B, C, sl;
}p[N];
ll A, B;
void dfs(ll u, ll p)
{
vector<ll> val;
for(ll v : adj[u])
{
if(v == p)continue;
dfs(v, u);
b[u] |= b[v];
if(b[v])continue;
val.pb(a[v]+1);
}
if(u == B)b[u] = 1;
if(b[u])kq.pb(u);
sort(val.rbegin(), val.rend());
for(int i = 0; i < val.size(); i ++)a[u] = max(a[u], val[i] + i);
}
bool check(ll mid)
{
fill_n(d, n+2, 0);
ll cur = 0;
for(ll x : kq)
{
if(a[x] + cur + 1 <= mid)d[x] = 1, ++cur;
else if(a[x] + cur <= mid)cur += 2, d[x] = 1;
else cur += 2;
}
cur = 0;
for(int i = kq.size()-1; i >= 0; i --)
{
ll x = kq[i];
if(a[x] + cur + 1 <= mid)d[x] = 1, ++cur;
else if(a[x] + cur <= mid)cur += 2, d[x] = 1;
else cur += 2;
if(d[x] == 0)return false;
}
return true;
}
void sol()
{
cin >> n >> A >> B;
for(int i = 1; i < n; i ++)
{
ll x ,y;
cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
}
dfs(A, 0);
ll lf = 0, rt = n, mid;
while(lf <= rt)
{
mid = (lf + rt) / 2;
if(check(mid))rt = mid - 1;
else lf = mid + 1;
}
cout << lf;
}
int main()
{
if(fopen(task".INP", "r"))
{
freopen(task".INP", "r", stdin);
freopen(task".OUT", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
}
/*
6
BWWRBW
4
WWRB
https://codeforces.com/contest/791/problem/E
*/
Compilation message (stderr)
torrent.cpp: In function 'void dfs(long long int, long long int)':
torrent.cpp:55:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i = 0; i < val.size(); i ++)a[u] = max(a[u], val[i] + i);
| ~~^~~~~~~~~~~~
torrent.cpp: In function 'int main()':
torrent.cpp:102:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
102 | freopen(task".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
torrent.cpp:103:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
103 | freopen(task".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |