/**
* Assume there is some hard route that goes from vertex
* u to vertex v. Let the node that the path from u to v and the
* furthest node from the hard route meet be node x. Use rerooting
* DP to calculate the hardest route and the # of such hardest routes
* for every node x.
*
* Time Complexity: O(n log(n))
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
cin.tie(0) -> sync_with_stdio(0);
int n;
cin >> n;
vector<vector<int>> adj(n);
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
--x, --y;
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<int> max_length(n), path_count(n);
auto dfs = [&](int u, int p, auto&& dfs) -> void {
/**
* Calculates the longest path from vertex u,
* and the number of such paths.
*/
max_length[u] = 0;
path_count[u] = 1;
for (int v : adj[u]) if (v != p) {
dfs(v, u, dfs);
if (max_length[u] < max_length[v] + 1) {
max_length[u] = max_length[v] + 1;
path_count[u] = path_count[v];
} else if (max_length[v] + 1 == max_length[u]) {
path_count[u] += path_count[v];
}
}
};
dfs(0, -1, dfs);
ll hard = 0, cnt = 1;
auto dfs2 = [&](int u, int p, ll parDist, ll parCnt,
auto&& dfs2) -> void {
/**
* Performs the rerooting, to count the hardest
* path and the # of such paths at this vertex.
*/
vector<array<ll, 2>> paths; // {distance, count}
if (u > 0 || (int) adj[u].size() == 1) {
paths.push_back({parDist, parCnt});
}
for (int v : adj[u]) if (v != p) {
paths.push_back({max_length[v] + 1, path_count[v]});
}
sort(paths.begin(), paths.end(), greater<>());
if ((int) adj[u].size() >= 3) { // can form a nonzero hard route
/**
* Let the 3 longest path lengths be a, b, c, with a > b > c.
* The optimal hard route "hardness" is a * (b + c).
*/
ll a = paths[0][0], b = paths[1][0], c = paths[2][0];
ll cur = a * (b + c), num = 0, ties = 0;
for (auto [k, v] : paths) {
if (k == c) ties += v;
}
// case 1: all are distinct.
if (a != b && b != c) {
num = paths[1][1] * ties;
}
// case 2: all are the same.
else if (a == b && b == c) {
num = ties * ties;
for (auto [k, v] : paths) {
if (k == a) num -= v * v;
}
num /= 2; // avoiding double counting
}
// case 3: first two are the same.
else if (a == b) {
num = (paths[0][1] + paths[1][1]) * ties;
}
// case 4: last two are the same.
else {
num = ties * ties;
for (auto [k, v] : paths) {
if (k == c) num -= v * v;
}
num /= 2;
}
if (hard < cur) {
hard = cur, cnt = num;
} else if (hard == cur) {
cnt += num;
}
}
// processing parent dist and parent count.
ll l1 = 0, l2 = 0, cnt1 = 0, cnt2 = 0;
for (auto [k, v] : paths) {
if (k + 1 > l1) {
swap(l1, l2);
swap(cnt1, cnt2);
l1 = k + 1, cnt1 = v;
} else if (k + 1 == l1) {
cnt1 += v;
} else if (k + 1 > l2) {
l2 = k + 1, cnt2 = v;
} else if (k + 1 == l2) {
cnt2 += v;
}
}
for (int v : adj[u]) if (v != p) {
// using the best parent hardness and parent count possible.
if (max_length[v] + 2 == l1) {
(path_count[v] == cnt1) ? dfs2(v, u, l2, cnt2, dfs2) :
dfs2(v, u, l1, cnt1 - path_count[v], dfs2);
} else {
dfs2(v, u, l1, cnt1, dfs2);
}
}
}; dfs2(0, -1, 0, 1, dfs2);
cout << hard << ' ' << cnt << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
376 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
376 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
1368 KB |
Output is correct |
26 |
Correct |
2 ms |
1116 KB |
Output is correct |
27 |
Correct |
2 ms |
1116 KB |
Output is correct |
28 |
Correct |
2 ms |
1112 KB |
Output is correct |
29 |
Correct |
2 ms |
1116 KB |
Output is correct |
30 |
Correct |
2 ms |
1116 KB |
Output is correct |
31 |
Correct |
2 ms |
1116 KB |
Output is correct |
32 |
Correct |
2 ms |
1116 KB |
Output is correct |
33 |
Correct |
2 ms |
1116 KB |
Output is correct |
34 |
Correct |
2 ms |
1116 KB |
Output is correct |
35 |
Correct |
2 ms |
1116 KB |
Output is correct |
36 |
Correct |
2 ms |
1116 KB |
Output is correct |
37 |
Correct |
2 ms |
1372 KB |
Output is correct |
38 |
Correct |
3 ms |
1780 KB |
Output is correct |
39 |
Correct |
2 ms |
1116 KB |
Output is correct |
40 |
Correct |
2 ms |
860 KB |
Output is correct |
41 |
Correct |
2 ms |
860 KB |
Output is correct |
42 |
Correct |
2 ms |
604 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
2 ms |
604 KB |
Output is correct |
45 |
Correct |
2 ms |
604 KB |
Output is correct |
46 |
Correct |
2 ms |
604 KB |
Output is correct |
47 |
Correct |
2 ms |
860 KB |
Output is correct |
48 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
376 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
1368 KB |
Output is correct |
26 |
Correct |
2 ms |
1116 KB |
Output is correct |
27 |
Correct |
2 ms |
1116 KB |
Output is correct |
28 |
Correct |
2 ms |
1112 KB |
Output is correct |
29 |
Correct |
2 ms |
1116 KB |
Output is correct |
30 |
Correct |
2 ms |
1116 KB |
Output is correct |
31 |
Correct |
2 ms |
1116 KB |
Output is correct |
32 |
Correct |
2 ms |
1116 KB |
Output is correct |
33 |
Correct |
2 ms |
1116 KB |
Output is correct |
34 |
Correct |
2 ms |
1116 KB |
Output is correct |
35 |
Correct |
2 ms |
1116 KB |
Output is correct |
36 |
Correct |
2 ms |
1116 KB |
Output is correct |
37 |
Correct |
2 ms |
1372 KB |
Output is correct |
38 |
Correct |
3 ms |
1780 KB |
Output is correct |
39 |
Correct |
2 ms |
1116 KB |
Output is correct |
40 |
Correct |
2 ms |
860 KB |
Output is correct |
41 |
Correct |
2 ms |
860 KB |
Output is correct |
42 |
Correct |
2 ms |
604 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
2 ms |
604 KB |
Output is correct |
45 |
Correct |
2 ms |
604 KB |
Output is correct |
46 |
Correct |
2 ms |
604 KB |
Output is correct |
47 |
Correct |
2 ms |
860 KB |
Output is correct |
48 |
Correct |
2 ms |
860 KB |
Output is correct |
49 |
Correct |
408 ms |
76708 KB |
Output is correct |
50 |
Correct |
372 ms |
83916 KB |
Output is correct |
51 |
Correct |
354 ms |
90004 KB |
Output is correct |
52 |
Correct |
386 ms |
68416 KB |
Output is correct |
53 |
Correct |
299 ms |
86352 KB |
Output is correct |
54 |
Correct |
299 ms |
94196 KB |
Output is correct |
55 |
Correct |
325 ms |
75328 KB |
Output is correct |
56 |
Correct |
280 ms |
83536 KB |
Output is correct |
57 |
Correct |
325 ms |
94508 KB |
Output is correct |
58 |
Correct |
298 ms |
86400 KB |
Output is correct |
59 |
Correct |
332 ms |
86576 KB |
Output is correct |
60 |
Correct |
314 ms |
82532 KB |
Output is correct |
61 |
Correct |
608 ms |
140388 KB |
Output is correct |
62 |
Correct |
577 ms |
121768 KB |
Output is correct |
63 |
Correct |
546 ms |
67964 KB |
Output is correct |
64 |
Correct |
592 ms |
55448 KB |
Output is correct |
65 |
Correct |
541 ms |
47180 KB |
Output is correct |
66 |
Correct |
485 ms |
42580 KB |
Output is correct |
67 |
Correct |
524 ms |
40204 KB |
Output is correct |
68 |
Correct |
519 ms |
39748 KB |
Output is correct |
69 |
Correct |
454 ms |
39212 KB |
Output is correct |
70 |
Correct |
475 ms |
38920 KB |
Output is correct |
71 |
Correct |
481 ms |
38816 KB |
Output is correct |
72 |
Correct |
480 ms |
38992 KB |
Output is correct |
73 |
Correct |
493 ms |
39556 KB |
Output is correct |
74 |
Correct |
452 ms |
39384 KB |
Output is correct |
75 |
Correct |
513 ms |
40136 KB |
Output is correct |
76 |
Correct |
458 ms |
41368 KB |
Output is correct |
77 |
Correct |
397 ms |
44176 KB |
Output is correct |
78 |
Correct |
238 ms |
51048 KB |
Output is correct |