#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int oo = 1e9;
#define sz(a) (int)a.size()
#define eb emplace_back
using pii = pair<int, int>;
int n;
namespace arcv {
int n;
vector<int> adj[N];
int par[N];
int sz[N];
int st[N];
int en[N];
int tour[N];
int ans = oo;
int times = 0;
multiset<int> S;
void calc_sz(int u, int p) {
par[u] = p;
sz[u] = 1;
for(int v : adj[u]) {
if(v == p)
continue;
calc_sz(v, u);
sz[u] += sz[v];
}
}
int get(int a, int b, int c) {
return max({a, b, c}) - min({a, b, c});
}
void DFS1(int u, int p) {
auto it = S.lower_bound((n - sz[u]) / 2);
if(it != S.end())
ans = min(ans, get(sz[u], *it, n - sz[u] - (*it)));
if(it != S.begin())
--it;
ans = min(ans, get(sz[u], *it, n - sz[u] - (*it)));
for(int v : adj[u]) {
if(v == p)
continue;
DFS1(v, u);
}
S.emplace(sz[u]);
en[u] = times;
}
void DFS2(int u, int p) {
auto it = S.lower_bound((n + sz[u]) / 2);
if(it != S.end())
ans = min(ans, get((*it) - sz[u], sz[u], n - (*it)));
if(it != S.begin())
--it;
if(it != S.end())
ans = min(ans, get((*it) - sz[u], sz[u], n - (*it)));
S.emplace(sz[u]);
for(int v : adj[u]) {
if(v == p)
continue;
DFS2(v, u);
}
S.erase(S.find(sz[u]));
}
void solution() {
cin >> n;
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].eb(v);
adj[v].eb(u);
}
calc_sz(1, 0);
DFS1(1, 0);
S.clear();
DFS2(1, 0);
cout << ans;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
arcv::solution();
}
Compilation message
papricice.cpp: In function 'void arcv::DFS1(int, int)':
papricice.cpp:45:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
45 | if(it != S.begin())
| ^~
papricice.cpp:48:13: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
48 | ans = min(ans, get(sz[u], *it, n - sz[u] - (*it)));
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
5024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
5212 KB |
Output is correct |
7 |
Correct |
3 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5208 KB |
Output is correct |
9 |
Correct |
3 ms |
5212 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
5212 KB |
Output is correct |
7 |
Correct |
3 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5208 KB |
Output is correct |
9 |
Correct |
3 ms |
5212 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
168 ms |
23340 KB |
Output is correct |
12 |
Correct |
198 ms |
23088 KB |
Output is correct |
13 |
Correct |
165 ms |
23888 KB |
Output is correct |
14 |
Correct |
149 ms |
23380 KB |
Output is correct |
15 |
Correct |
183 ms |
23096 KB |
Output is correct |
16 |
Correct |
124 ms |
23760 KB |
Output is correct |
17 |
Correct |
180 ms |
23356 KB |
Output is correct |
18 |
Correct |
167 ms |
27564 KB |
Output is correct |
19 |
Correct |
152 ms |
23320 KB |
Output is correct |
20 |
Correct |
175 ms |
23240 KB |
Output is correct |