#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 |
1 |
Incorrect |
10 ms |
23896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
245 ms |
87992 KB |
Output is correct |
2 |
Correct |
199 ms |
81488 KB |
Output is correct |
3 |
Correct |
587 ms |
90192 KB |
Output is correct |
4 |
Correct |
251 ms |
56860 KB |
Output is correct |
5 |
Correct |
615 ms |
89952 KB |
Output is correct |
6 |
Correct |
604 ms |
90024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
23896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
23896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |