#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
307 ms |
76168 KB |
Output is correct |
2 |
Correct |
249 ms |
71004 KB |
Output is correct |
3 |
Correct |
697 ms |
77236 KB |
Output is correct |
4 |
Correct |
317 ms |
50456 KB |
Output is correct |
5 |
Correct |
722 ms |
77204 KB |
Output is correct |
6 |
Correct |
686 ms |
77208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |