//Autor: Bartłomiej Czarkowski
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif
const int N = 1001000;
int n, a, b, m, q;
int par[N];
int son[N];
int cost[N];
int blk[N];
int dp[N]; // ile żeby uwięzić i wyprowadzić do ojca
vector<int> v[N];
void dfs_dp(int x) {
vector<int> war = {0, 0};
for (int i : v[x]) {
if (i == par[x]) {
continue;
}
par[i] = x;
dfs_dp(i);
war.push_back(dp[i]);
}
nth_element(war.begin(), war.begin() + 1, war.end(), [&](int a, int b) {
return a > b;
});
dp[x] = war[1] + war.size() - 2;
}
int main() {
scanf("%d%d%d", &n, &q, &m);
for (int i = 1; i < n; ++i) {
scanf("%d%d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
dfs_dp(q);
int x = m;
while (x != q) {
son[par[x]] = x;
x = par[x];
}
cost[x] = 0;
while (x != m) {
x = son[x];
blk[x] = blk[par[x]];
vector<int> war = {0, 0};
for (int i : v[x]) {
if (i != par[x] && i != son[x]) {
war.push_back(dp[i]);
++blk[x];
}
}
nth_element(war.begin(), war.begin() + 1, war.end(), [&](int a, int b) {
return a > b;
});
cost[x] = max(war[1] + blk[x], cost[x]);
}
//~ for (int i = 1; i <= n; ++i) {
//~ printf("%d: %d\n", i, dp[i]);
//~ }
printf("%d\n", cost[m]);
return 0;
}
Compilation message
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:38:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf("%d%d%d", &n, &q, &m);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:40:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23752 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
14 ms |
23764 KB |
Output is correct |
5 |
Correct |
15 ms |
23784 KB |
Output is correct |
6 |
Correct |
16 ms |
23892 KB |
Output is correct |
7 |
Correct |
13 ms |
23820 KB |
Output is correct |
8 |
Correct |
13 ms |
23820 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
431 ms |
62864 KB |
Output is correct |
2 |
Correct |
362 ms |
59068 KB |
Output is correct |
3 |
Correct |
980 ms |
64040 KB |
Output is correct |
4 |
Correct |
432 ms |
43852 KB |
Output is correct |
5 |
Correct |
907 ms |
63964 KB |
Output is correct |
6 |
Correct |
865 ms |
64056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23752 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
14 ms |
23764 KB |
Output is correct |
5 |
Correct |
15 ms |
23784 KB |
Output is correct |
6 |
Correct |
16 ms |
23892 KB |
Output is correct |
7 |
Correct |
13 ms |
23820 KB |
Output is correct |
8 |
Correct |
13 ms |
23820 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23776 KB |
Output is correct |
11 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23752 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
14 ms |
23764 KB |
Output is correct |
5 |
Correct |
15 ms |
23784 KB |
Output is correct |
6 |
Correct |
16 ms |
23892 KB |
Output is correct |
7 |
Correct |
13 ms |
23820 KB |
Output is correct |
8 |
Correct |
13 ms |
23820 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23776 KB |
Output is correct |
11 |
Correct |
431 ms |
62864 KB |
Output is correct |
12 |
Correct |
362 ms |
59068 KB |
Output is correct |
13 |
Correct |
980 ms |
64040 KB |
Output is correct |
14 |
Correct |
432 ms |
43852 KB |
Output is correct |
15 |
Correct |
907 ms |
63964 KB |
Output is correct |
16 |
Correct |
865 ms |
64056 KB |
Output is correct |
17 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |