#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
if (n == 1) {
cout << 0 << '\n';
return 0;
}
vector<vector<int>> adj(n);
for (int i = 0; i < n - 1; i += 1) {
int a, b;
cin >> a >> b;
a -= 1, b -= 1;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<pair<int, int>> dp(n);
function<pair<int, int>(int, int)> Dfs = [&](int node, int pre) {
int ans = 1;
for (auto x : adj[node]) {
if (x == pre) continue;
ans += Dfs(x, node).first;
}
return dp[node] = make_pair(ans, pre);
};
Dfs(0, -1);
long long ans = 0;
int max_depth = 0;
function<void(int, int, int, int, bool)> Get = [&](int node, int pre, int depth, int sub, bool now) {
if (now) {
max_depth = max(depth, max_depth);
}
ans += 1LL * depth * 2;
for (auto x : adj[node]) {
if (x == pre) continue;
bool c1 = (x == sub || sub == -1);
Get(x, node, depth + 1, sub, now | c1);
}
return;
};
auto Init = [&]() {
max_depth = 0;
ans = 0;
return;
};
vector<int> sum(n);
for (int i = 0; i < n; i++) {
for (auto x : adj[i]) {
if (x == dp[i].second) continue;
sum[i] += dp[x].first;
}
}
for (int node = 0; node < n; node += 1) {
vector<pair<int, int>> comps;
int id = 0, cnt = 0;
for (auto x : adj[node]) {
int sz = (x == dp[node].second ? n - 1 - sum[node] : dp[x].first);
comps.emplace_back(sz, x);
if (comps[id].first <= sz) {
id = cnt;
}
cnt += 1;
}
int mx = comps[id].first;
int sub = comps[id].second;
if (mx > n - mx) {
cout << -1 << '\n';
continue;
}
else {
if (n - 2 * mx == 0) {
Get(node, -1, 0, sub, 0);
}
else {
Get(node, -1, 0, -1, 0);
}
cout << ans - max_depth << '\n';
Init();
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
1856 KB |
Output is correct |
2 |
Correct |
11 ms |
2388 KB |
Output is correct |
3 |
Correct |
12 ms |
3028 KB |
Output is correct |
4 |
Correct |
10 ms |
1996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
3500 KB |
Output is correct |
2 |
Correct |
30 ms |
4592 KB |
Output is correct |
3 |
Correct |
22 ms |
5716 KB |
Output is correct |
4 |
Correct |
26 ms |
3592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
8512 KB |
Output is correct |
2 |
Correct |
92 ms |
11196 KB |
Output is correct |
3 |
Correct |
75 ms |
13876 KB |
Output is correct |
4 |
Correct |
49 ms |
8640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
666 ms |
42372 KB |
Output is correct |
2 |
Correct |
608 ms |
55796 KB |
Output is correct |
3 |
Correct |
571 ms |
69328 KB |
Output is correct |
4 |
Correct |
425 ms |
42328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1349 ms |
84492 KB |
Output is correct |
2 |
Correct |
1281 ms |
111580 KB |
Output is correct |
3 |
Correct |
1288 ms |
131072 KB |
Output is correct |
4 |
Correct |
998 ms |
84464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1440 ms |
84380 KB |
Output is correct |
2 |
Correct |
1259 ms |
111464 KB |
Output is correct |
3 |
Correct |
1339 ms |
131072 KB |
Output is correct |
4 |
Correct |
947 ms |
84564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1344 ms |
84460 KB |
Output is correct |
2 |
Correct |
1261 ms |
111612 KB |
Output is correct |
3 |
Correct |
1256 ms |
131072 KB |
Output is correct |
4 |
Correct |
968 ms |
84488 KB |
Output is correct |