#ifdef LOCAL
#include "local.h"
#else
#include <bits/stdc++.h>
#define debug(...)
#define DB(...)
#endif
using namespace std;
using ll = long long;
using ld = long double;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define For(i, l, r) for (int i = (l); i <= (r); ++i)
#define Ford(i, r, l) for (int i = (r); i >= (l); --i)
#define Rep(i, n) For (i, 0, (n) - 1)
#define Repd(i, n) Ford (i, (n) - 1, 0)
#define Fe(...) for (auto __VA_ARGS__)
template <class C> inline int isz(const C& c) { return static_cast<int>(c.size()); }
template <class T> inline bool chmin(T& a, const T& b) { return (a > b) ? a = b, true : false; }
template <class T> inline bool chmax(T& a, const T& b) { return (a < b) ? a = b, true : false; }
const bool __initialization = []() {
cin.tie(nullptr)->sync_with_stdio(false);
#define TASK "empty"
if (fopen(TASK".inp", "r")) {
(void)(!freopen(TASK".inp", "r", stdin));
(void)(!freopen(TASK".out", "w", stdout)); }
cout << setprecision(12) << fixed;
return true;
}();
constexpr ld eps = 1e-9;
constexpr int inf = 1e9;
constexpr ll linf = 1e18;
// =============================================================================
constexpr int maxn = 2e5 + 5;
struct Edge {
int u, v;
int other(int x) const { return u ^ v ^ x; }
};
int n;
vector<int> g[maxn];
Edge e[maxn];
int sz[maxn];
int color[maxn];
int par[maxn];
void Dfs(int u, int c) {
sz[u] = 1;
Fe (id : g[u]) {
if (color[id]) continue;
color[id] = c;
int v = e[id].other(u);
par[v] = u;
Dfs(v, c);
sz[u] += sz[v];
}
}
signed main() {
cin >> n;
For (i, 1, n - 1) {
int u, v; cin >> u >> v;
g[u].push_back(i);
g[v].push_back(i);
e[i] = { u, v };
}
int ans = n;
For (i, 1, n - 1) {
auto [u, v] = e[i];
memset(color + 1, 0, 4 * (n - 1));
color[i] = -1;
par[u] = -1;
Dfs(u, 1);
par[v] = -1;
Dfs(v, 2);
int subu = sz[u];
int subv = sz[v];
For (j, 1, n - 1) {
if (i == j) continue;
auto [pu, pv] = e[j];
if (pv == par[pu]) swap(pu, pv);
array<int, 3> comp;
if (color[j] == 1) {
comp = { subv, sz[pv], subu - sz[pv] };
}
else {
comp = { subu, sz[pv], subv - sz[pv] };
}
sort(all(comp));
int diff = comp[2] - comp[0];
chmin(ans, diff);
}
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
133 ms |
5068 KB |
Output is correct |
7 |
Correct |
156 ms |
5140 KB |
Output is correct |
8 |
Correct |
125 ms |
5144 KB |
Output is correct |
9 |
Correct |
128 ms |
5068 KB |
Output is correct |
10 |
Correct |
146 ms |
5140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
133 ms |
5068 KB |
Output is correct |
7 |
Correct |
156 ms |
5140 KB |
Output is correct |
8 |
Correct |
125 ms |
5144 KB |
Output is correct |
9 |
Correct |
128 ms |
5068 KB |
Output is correct |
10 |
Correct |
146 ms |
5140 KB |
Output is correct |
11 |
Execution timed out |
1067 ms |
17800 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |