#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int dp[N];
int h[N];
bool viz[N];
vector<int> gr[N];
int par[N];
void first_dfs(int nod, int dad){
par[nod] = dad;
h[nod] = 1 + h[dad];
for(auto x:gr[nod]){
if(x != dad)
first_dfs(x, nod);
}
}
void dfs(int nod, int dad){
int nrvec = 0;
for(auto x:gr[nod]){
if(x == dad || viz[x] == true)
continue;
nrvec++;
dfs(x, nod);
}
int m1 = 0, m2 = 0;
for(auto x:gr[nod]){
if(x == dad || viz[x])
continue;
if(nrvec + 1 + dp[x] > m1){
m2 = m1;
m1 = nrvec + 1 + dp[x];
}
else if(nrvec + 1 + dp[x] > m2){
m2 = nrvec + 1 + dp[x];
}
}
dp[nod] = m2;
}
int ans = 0;
int solve(int nod, int climbed){
viz[nod] = true;
if(par[nod] == 0)
return 0;
int sum = solve(par[nod], climbed + 1);
dfs(nod, 0);
ans = max(ans, dp[nod] - climbed + sum);
sum += gr[nod].size() - (par[nod] != 0) - 1;
return sum;
}
int main()
{
//freopen(".in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n, root, m;
cin>>n>>root>>m;
for(int i =1 ; i<n; i++){
int x, y;
cin>>x>>y;
gr[x].push_back(y);
gr[y].push_back(x);
}
first_dfs(root, 0);
solve(m, 0);
cout<<ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
13 ms |
23708 KB |
Output is correct |
3 |
Correct |
13 ms |
23800 KB |
Output is correct |
4 |
Incorrect |
13 ms |
23756 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
357 ms |
70212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
13 ms |
23708 KB |
Output is correct |
3 |
Correct |
13 ms |
23800 KB |
Output is correct |
4 |
Incorrect |
13 ms |
23756 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
13 ms |
23708 KB |
Output is correct |
3 |
Correct |
13 ms |
23800 KB |
Output is correct |
4 |
Incorrect |
13 ms |
23756 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |