-
Notifications
You must be signed in to change notification settings - Fork 1
/
keeping_on_track.cpp
91 lines (65 loc) · 1.59 KB
/
keeping_on_track.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <bits/stdc++.h>
typedef std::vector<std::vector<int>> AdjList;
typedef long long LL;
int N;
AdjList adj;
std::vector<int> p_tree;
std::vector<std::vector<int>> child_counts;
LL best;
int best_root;
LL count_pairs(std::vector<int>& child_sizes) {
LL children_remaining = N;
LL pairs = 0;
for (int i = 0; i < child_sizes.size(); ++i) {
children_remaining -= child_sizes[i];
pairs += child_sizes[i] * children_remaining;
}
return pairs;
}
int dfs(int u) {
int num_children = 0;
for (const int v : adj[u]) if (p_tree[v] < 0) {
p_tree[v] = u;
int tmp = dfs(v);
num_children += tmp;
child_counts[u].push_back(tmp);
}
child_counts[u].push_back(N-num_children);
std::sort(child_counts[u].begin(), child_counts[u].end());
LL pairs = count_pairs(child_counts[u]);
if (pairs > best) {
best = pairs;
best_root = u;
}
return num_children + 1;
}
int main() {
scanf("%d", &N);
adj.assign(N+1, std::vector<int>());
p_tree.assign(N+1,-1);
child_counts.assign(N+1, std::vector<int>());
best = -1;
best_root = 0;
for (int i = 0; i < N; ++i) {
int u,v; scanf("%d %d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
// Find a leaf node
int root = 0;
for (int i = 0; i <= N; ++i) {
if (adj[i].size() == 1) {
root = i;
break;
}
}
p_tree[root] = root;
dfs(root);
// printf("%d %d\n", best, best_root);
int tmp = child_counts[best_root].back();
child_counts[best_root].pop_back();
child_counts[best_root][child_counts[best_root].size()-1] += tmp;
int better = count_pairs(child_counts[best_root]);
printf("%d %d\n", best, better);
return 0;
}