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];
int ans = 0, htotale[maxn5];
vector <int> adj[maxn5];
inline bool dfs(int v, int par){
if(v == tale){
dp[v] = 0;
htotale[v] = 0;
return true;
}
int mx = 0, smx = 0;
int z = -1;
for(auto u : adj[v]) if(u != par){
bool re = dfs(u, v);
if(re){
z = u;
}
if(dp[u] >= mx){
smx = mx;
mx = dp[u];
}
else
smx = max(smx, dp[u]);
}
if(z == -1){
dp[v] = smx + adj[v].size() - 1;
//cout << "false " << v << ' ' << dp[v] << endl;
return false;
}
mx = smx = 0;
for(auto u : adj[v]) if(u != par && u != z){
if(dp[u] >= mx){
smx = mx;
mx = dp[u];
}
else
smx = max(smx, dp[u]);
}
dp[v] = smx + adj[v].size() - 2 + htotale[z];
if(v == mo)
dp[v]++;
htotale[v] = htotale[z] + adj[v].size() - 2;
ans = max(ans, dp[v]);
//cout << v << ' ' << dp[v] << ' ' << htotale[v] << ' ' << smx << ' ' << mx << endl;
return true;
}
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 << ans << 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... |