#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n;
cin >> n;
vector<vector<int>> adj(n);
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
adj[--x].push_back(--y);
adj[y].push_back(x);
}
vector<int> max_length(n);
vector<int> path_count(n);
/**
* Calculates the longest path from vertex u,
* and the number of such paths.
*/
function<void(int, int)> dfs = [&](int u, int p) {
max_length[u] = 0;
path_count[u] = 1;
for (int v : adj[u])
if (v != p) {
dfs(v, u);
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);
ll max_hardness = 0;
ll hardest_path_count = 1;
/**
* Performs the rerooting, to count the hardest
* path and the # of such paths at this vertex.
*/
function<void(int, int, ll, ll)> dfs2 = [&](int u, int p, ll parent_dist,
ll parent_count) {
vector<array<ll, 2>> paths; // {distance, count}
if (u > 0 || (int)adj[u].size() == 1) {
paths.push_back({parent_dist, parent_count});
}
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 hardness 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];
ll b = paths[1][0];
ll c = paths[2][0];
ll current_hardness = a * (b + c);
ll current_path_count = 0;
ll ties = 0;
for (auto [len, amt] : paths) {
if (len == c) { ties += amt; }
}
if (a != b && b != c) {
// case 1: all are distinct.
current_path_count = paths[0][1]*paths[1][1] * ties;
} else if (a == b && b == c) {
// case 2: all are the same.
current_path_count = ties * ties;
for (auto [len, amt] : paths) {
if (len == a) { current_path_count -= amt * amt; }
}
current_path_count /= 2; // avoiding double counting
} else if (a == b) {
// case 3: first two are the same.
current_path_count = (paths[0][1] + paths[1][1]) * ties;
} else {
// case 4: last two are the same.
current_path_count = ties * ties;
for (auto [len, amt] : paths) {
if (len == c) { current_path_count -= amt * amt; }
}
current_path_count /= 2; // avoiding double counting
}
if (max_hardness < current_hardness) {
max_hardness = current_hardness;
hardest_path_count = current_path_count;
} else if (max_hardness == current_hardness) {
hardest_path_count += current_path_count;
}
}
// processing parent dist and parent count.
ll longest1 = 0;
ll longest2 = 0;
ll count1 = 0;
ll count2 = 0;
for (auto [len, amt] : paths) {
if (len + 1 > longest1) {
swap(longest1, longest2);
swap(count1, count2);
longest1 = len + 1;
count1 = amt;
} else if (len + 1 == longest1) {
count1 += amt;
} else if (len + 1 > longest2) {
longest2 = len + 1;
count2 = amt;
} else if (len + 1 == longest2) {
count2 += amt;
}
}
for (int v : adj[u])
if (v != p) {
// using the best parent hardness and parent count possible.
if (max_length[v] + 2 == longest1) {
(path_count[v] == count1)
? dfs2(v, u, longest2, count2)
: dfs2(v, u, longest1, count1 - path_count[v]);
} else {
dfs2(v, u, longest1, count1);
}
}
};
dfs2(0, -1, 0, 1);
cout << max_hardness << ' ' << hardest_path_count << '\n';
}
Compilation message
road.cpp: In lambda function:
road.cpp:67:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
67 | for (auto [len, amt] : paths) {
| ^
road.cpp:77:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
77 | for (auto [len, amt] : paths) {
| ^
road.cpp:87:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
87 | for (auto [len, amt] : paths) {
| ^
road.cpp:106:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
106 | for (auto [len, amt] : paths) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 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 |
348 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 |
436 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 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 |
348 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 |
436 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 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 |
3 ms |
1132 KB |
Output is correct |
26 |
Correct |
3 ms |
1372 KB |
Output is correct |
27 |
Correct |
3 ms |
1372 KB |
Output is correct |
28 |
Correct |
3 ms |
1428 KB |
Output is correct |
29 |
Correct |
3 ms |
1200 KB |
Output is correct |
30 |
Correct |
3 ms |
1372 KB |
Output is correct |
31 |
Correct |
3 ms |
1412 KB |
Output is correct |
32 |
Correct |
3 ms |
1116 KB |
Output is correct |
33 |
Correct |
3 ms |
1372 KB |
Output is correct |
34 |
Correct |
3 ms |
1372 KB |
Output is correct |
35 |
Correct |
3 ms |
1384 KB |
Output is correct |
36 |
Correct |
3 ms |
1368 KB |
Output is correct |
37 |
Correct |
3 ms |
1628 KB |
Output is correct |
38 |
Correct |
5 ms |
2140 KB |
Output is correct |
39 |
Correct |
3 ms |
1116 KB |
Output is correct |
40 |
Correct |
3 ms |
860 KB |
Output is correct |
41 |
Correct |
3 ms |
680 KB |
Output is correct |
42 |
Correct |
3 ms |
604 KB |
Output is correct |
43 |
Correct |
3 ms |
740 KB |
Output is correct |
44 |
Correct |
5 ms |
604 KB |
Output is correct |
45 |
Correct |
3 ms |
604 KB |
Output is correct |
46 |
Correct |
3 ms |
700 KB |
Output is correct |
47 |
Correct |
4 ms |
668 KB |
Output is correct |
48 |
Correct |
3 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 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 |
348 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 |
436 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 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 |
3 ms |
1132 KB |
Output is correct |
26 |
Correct |
3 ms |
1372 KB |
Output is correct |
27 |
Correct |
3 ms |
1372 KB |
Output is correct |
28 |
Correct |
3 ms |
1428 KB |
Output is correct |
29 |
Correct |
3 ms |
1200 KB |
Output is correct |
30 |
Correct |
3 ms |
1372 KB |
Output is correct |
31 |
Correct |
3 ms |
1412 KB |
Output is correct |
32 |
Correct |
3 ms |
1116 KB |
Output is correct |
33 |
Correct |
3 ms |
1372 KB |
Output is correct |
34 |
Correct |
3 ms |
1372 KB |
Output is correct |
35 |
Correct |
3 ms |
1384 KB |
Output is correct |
36 |
Correct |
3 ms |
1368 KB |
Output is correct |
37 |
Correct |
3 ms |
1628 KB |
Output is correct |
38 |
Correct |
5 ms |
2140 KB |
Output is correct |
39 |
Correct |
3 ms |
1116 KB |
Output is correct |
40 |
Correct |
3 ms |
860 KB |
Output is correct |
41 |
Correct |
3 ms |
680 KB |
Output is correct |
42 |
Correct |
3 ms |
604 KB |
Output is correct |
43 |
Correct |
3 ms |
740 KB |
Output is correct |
44 |
Correct |
5 ms |
604 KB |
Output is correct |
45 |
Correct |
3 ms |
604 KB |
Output is correct |
46 |
Correct |
3 ms |
700 KB |
Output is correct |
47 |
Correct |
4 ms |
668 KB |
Output is correct |
48 |
Correct |
3 ms |
860 KB |
Output is correct |
49 |
Correct |
406 ms |
91216 KB |
Output is correct |
50 |
Correct |
409 ms |
101136 KB |
Output is correct |
51 |
Correct |
402 ms |
109396 KB |
Output is correct |
52 |
Correct |
412 ms |
79700 KB |
Output is correct |
53 |
Correct |
396 ms |
102296 KB |
Output is correct |
54 |
Correct |
389 ms |
112524 KB |
Output is correct |
55 |
Correct |
367 ms |
87252 KB |
Output is correct |
56 |
Correct |
362 ms |
98380 KB |
Output is correct |
57 |
Correct |
425 ms |
115792 KB |
Output is correct |
58 |
Correct |
398 ms |
104528 KB |
Output is correct |
59 |
Correct |
408 ms |
104276 KB |
Output is correct |
60 |
Correct |
407 ms |
99156 KB |
Output is correct |
61 |
Correct |
560 ms |
179028 KB |
Output is correct |
62 |
Correct |
577 ms |
153424 KB |
Output is correct |
63 |
Correct |
578 ms |
79184 KB |
Output is correct |
64 |
Correct |
501 ms |
61520 KB |
Output is correct |
65 |
Correct |
441 ms |
50008 KB |
Output is correct |
66 |
Correct |
427 ms |
44112 KB |
Output is correct |
67 |
Correct |
419 ms |
40832 KB |
Output is correct |
68 |
Correct |
428 ms |
39764 KB |
Output is correct |
69 |
Correct |
421 ms |
39224 KB |
Output is correct |
70 |
Correct |
396 ms |
38820 KB |
Output is correct |
71 |
Correct |
390 ms |
38592 KB |
Output is correct |
72 |
Correct |
422 ms |
38992 KB |
Output is correct |
73 |
Correct |
399 ms |
39252 KB |
Output is correct |
74 |
Correct |
376 ms |
39376 KB |
Output is correct |
75 |
Correct |
381 ms |
40164 KB |
Output is correct |
76 |
Correct |
372 ms |
41116 KB |
Output is correct |
77 |
Correct |
369 ms |
44120 KB |
Output is correct |
78 |
Correct |
278 ms |
49988 KB |
Output is correct |