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;
#define sz(x) ((int)size(x))
#define all(x) begin(x), end(x)
#define trace(x) cout << #x << ": " << (x) << endl;
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) { return (int) ((ll) rnd() % (r - l + 1)) + l; }
template<typename T>
bool ckmn(T &x, T y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template<typename T>
bool ckmx(T &x, T y) {
if (x < y) {
x = y;
return true;
}
return false;
}
const int N = 1000001, infI = 1e9 + 7;
const ll infL = 3e18;
int n, t, m;
vector<int> g[N];
int dp[2][N];
void dfs(int v, int p) {
int mx1 = 0, mx2 = 0, cnt = 0;
for (int to : g[v]) {
if (to != p) {
++cnt;
dfs(to, v);
if (mx1 < dp[0][to]) {
mx2 = mx1;
mx1 = dp[0][to];
} else {
ckmx(mx2, dp[0][to]);
}
}
}
dp[1][v] = mx1 + cnt;
dp[0][v] = 1 + mx2 + (cnt - 1);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> t >> m;
--t, --m;
for (int i = 1; i < n; ++i) {
int a, b;
cin >> a >> b;
--a, --b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(t, -1);
cout << dp[0][m];
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... |