This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//memset 0x3f -> > 1000000000
#include <bits/stdc++.h>
#define f first
#define s second
#define MAXSIZE (int)(1e6+5)
#define MAXINT (int)(1e9)
#define MAXLL (int)(1e18)
#define log2int(x) (32 - __builtin_clz(x) - 1)
#define log2ll(x) (64ll - __builtin_clzll(x) - 1ll)
#define LSOne(x) (x & (-x))
#define int long long
// #define MAXINT MAXLL
// #define log2int log2ll
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
typedef pair<int,int> ii;
string answerString[2] = {"NO", "YES"};
int add(int x, int y, int mod)
{
int t = x + y;
return t - mod * (t>=mod);
}
int sub(int x, int y, int mod)
{
int t = x - y;
return t + mod * (t<0);
}
//notes
int n, t, m;
vector <int> g[MAXSIZE];
bool flagged = false;
int dfs(int u, int p)
{
if (u == t)
{
flagged = true;
return 0;
}
priority_queue <int> pq;
bool paOfT = false; int valOfT = 0;
for (int v : g[u])
if (v != p)
{
int val = dfs(v, u);
if (flagged)
{
paOfT = true;
valOfT = val;
}
else pq.push(val + 1);
}
int sum = 0;
while (!pq.empty())
{
sum++; pq.pop();
if (pq.empty()) break;
sum += pq.top(); pq.pop();
}
if (paOfT)
{
flagged = true;
sum += valOfT;
}
else flagged = false;
//cout<<u<<" "<<sum<<endl;
return sum;
}
signed main()
{
//freopen("text.inp","r",stdin);
//freopen("text.out","w",stdout);
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>t>>m;
for (int i=1;i<n;i++)
{
int u, v; cin>>u>>v;
g[u].push_back(v); g[v].push_back(u);
}
cout<<dfs(m, -1);
}
# | 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... |