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 <iostream>
#include <vector>
#include <algorithm>
#define pb push_back
#define all(xs) xs.begin(), xs.end()
#define rep(i, n) for (int i=0; i<(n); i++)
using namespace std;
int N, T, M;
vector<int> G[1000000];
int dp[1000000];
bool sub[1000000];
void dfs(int x, int p) {
vector<int> values;
int ctr = 0, ti = -1;
sub[x] = x == M;
for (int t : G[x]) {
if (t == p) continue;
dfs(t, x);
sub[x] |= sub[t];
if (sub[t]) ti = t;
values.pb(dp[t]);
ctr++;
}
if (ti != -1) {
dp[x] = dp[ti];
if (p != -1) dp[x] += ctr-1;
return;
}
sort(all(values), greater<int>());
if (values.size() >= 2) ctr += values[1];
dp[x] = ctr;
}
signed main() {
cin >> N >> T >> M;
T--, M--;
rep(i, N-1) {
int u, v;
cin >> u >> v;
u--, v--;
G[u].pb(v);
G[v].pb(u);
}
dfs(T, -1);
cout << dp[T] << "\n";
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... |