// In the name of the God
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;
const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 1e6+5;
int n, t, m, par[maxn], dp[maxn];
vector <int> adj[maxn], vec[maxn];
void dfs1(int v, int p) {
par[v] = p;
for (int u : adj[v]) {
if (u != p) {
dfs1(u, v);
dp[v]++;
vec[v].pb(dp[u]);
}
}
sort(all(vec[v]), greater<int>());
for (int i = 1; i < vec[v].size(); i += 2) dp[v] += vec[v][i];
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> t >> m; t--, m--;
for (int i = 1; i < n; i++) {
int v, u; cin >> v >> u;
adj[--v].pb(--u);
adj[u].pb(v);
}
dfs1(t, t);
int ans = 0, last = -1;
while (m != t) {
vec[m].clear();
for (int u : adj[m]) {
if (u != par[m] and u != last) {
vec[m].pb(dp[u]);
ans++;
}
}
sort(all(vec[m]), greater<int>());
for (int i = 1; i < vec[m].size(); i += 2) ans += vec[m][i];
last = m;
m = par[m];
}
cout << ans << '\n';
return 0;
}
Compilation message
mousetrap.cpp: In function 'void dfs1(long long int, long long int)':
mousetrap.cpp:32:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int i = 1; i < vec[v].size(); i += 2) dp[v] += vec[v][i];
| ~~^~~~~~~~~~~~~~~
mousetrap.cpp: In function 'int32_t main()':
mousetrap.cpp:54:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int i = 1; i < vec[m].size(); i += 2) ans += vec[m][i];
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
49752 KB |
Output is correct |
2 |
Correct |
11 ms |
49756 KB |
Output is correct |
3 |
Correct |
11 ms |
49752 KB |
Output is correct |
4 |
Correct |
12 ms |
49872 KB |
Output is correct |
5 |
Incorrect |
13 ms |
49756 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
270 ms |
114992 KB |
Output is correct |
2 |
Correct |
245 ms |
108368 KB |
Output is correct |
3 |
Incorrect |
734 ms |
119196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
49752 KB |
Output is correct |
2 |
Correct |
11 ms |
49756 KB |
Output is correct |
3 |
Correct |
11 ms |
49752 KB |
Output is correct |
4 |
Correct |
12 ms |
49872 KB |
Output is correct |
5 |
Incorrect |
13 ms |
49756 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
49752 KB |
Output is correct |
2 |
Correct |
11 ms |
49756 KB |
Output is correct |
3 |
Correct |
11 ms |
49752 KB |
Output is correct |
4 |
Correct |
12 ms |
49872 KB |
Output is correct |
5 |
Incorrect |
13 ms |
49756 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |