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>
using namespace std;
typedef long long ll;
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define pb push_back
const int maxn5 = 1e6 + 10;
int tale, mo, dp[maxn5];
vector <int> adj[maxn5];
inline void dfs(int v, int par){
int mx = 0, smx = 0;
for(auto u : adj[v]) if(u != par && u != tale){
dfs(u, v);
if(dp[u] >= mx){
smx = mx;
mx = dp[u];
}
else
smx = max(smx, dp[u]);
}
dp[v] = smx + adj[v].size() - 1;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n, a, b; cin >> n >> a >> b;
a--; b--;
mo = b;
tale = a;
for(int i = 0; i < n - 1; i++){
int a, b; cin >> a >> b;
a--; b--;
adj[a].pb(b);
adj[b].pb(a);
}
assert(a != b);
if(a == b)
return cout << 0 << endl, 0;
dfs(mo, -1);
cout << dp[mo] << endl;
}
# | 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... |