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 endl '\n'
using namespace std;
typedef long long ll;
const ll MAXN = 1e6 + 10;
ll n, start, finish;
vector < ll > adj[MAXN];
ll dp[MAXN];
ll deg[MAXN];
void dfs(ll u, ll par)
{
priority_queue < ll > pq;
for(ll v : adj[u])
{
if(v == par)
continue;
dfs(v, u);
pq.push(dp[v]);
}
if(pq.size() == 1)
dp[u] = 1;
else if(pq.size() >= 2)
{
pq.pop();
ll cur = pq.top();
if(u == par)
dp[u] = deg[u] + cur;
else
dp[u] = deg[u] + cur - 1;
}
}
void read()
{
cin >> n >> finish >> start;
for(int i = 1; i <= n - 1; i ++)
{
ll u,v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
deg[u]++;
deg[v]++;
}
}
void solve()
{
dfs(start, start);
cout << dp[start] - 1 << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
read();
solve();
return 0;
}
# | 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... |