/tmp/solutions/build/tree_diameter-main.cpp:
1| |#include <common.h>
2| |prelude;
3| |
4| |namespace {
5| |
6| |constexpr int N = 5e5;
7| |
8| |struct {
9| | int to;
10| | int len;
11| | int nxt;
12| |} edge[N * 2];
13| |int pa[N];
14| |int head[N];
15| |
16| 7.22M|def tree_diameter(int u, int p) -> std::pair<u64, int> {
17| 7.22M| pa[u] = p;
18| 7.22M| std::pair<u64, int> ans{0, u};
19| 21.6M| for (int e = head[u]; e; e = edge[e].nxt) {
^14.4M
------------------
| Branch (19:25): [True: 66.67%, False: 33.33%]
------------------
20| 14.4M| int v = edge[e].to;
21| 14.4M| int len = edge[e].len;
22| 14.4M| if (v != p) {
------------------
| Branch (22:9): [True: 50.00%, False: 50.00%]
------------------
23| 7.22M| def res = tree_diameter(v, u);
24| 7.22M| res.first += len;
25| 7.22M| ans = std::max(ans, res);
26| 7.22M| }
27| 14.4M| }
28| 7.22M| return ans;
29| 7.22M|}
30| |
31| |} // namespace
32| |
33| 16|int main() {
34| 16| rd rd;
35| 16| wt wt;
36| 16| int n = rd.uh();
37| 16|#ifdef LOCAL
38| 16| std::memset(head, 0, 4 * n);
39| 16|#endif
40| 3.61M| for (int i = 1; i < n; ++i) {
^3.61M
------------------
| Branch (40:19): [True: 100.00%, False: 0.00%]
------------------
41| 3.61M| int a = rd.uh();
42| 3.61M| int b = rd.uh();
43| 3.61M| int c = rd.uw();
44| 3.61M| edge[i * 2 | 0] = {b, c, head[a]}, head[a] = i * 2 | 0;
45| 3.61M| edge[i * 2 | 1] = {a, c, head[b]}, head[b] = i * 2 | 1;
46| 3.61M| }
47| 16| let[_, u] = tree_diameter(0, -1);
48| 16| let[d, v] = tree_diameter(u, -1);
49| 16| int c = 0;
50| 500k| for (int i = v; i != -1; i = pa[i]) ++c;
^500k ^500k
------------------
| Branch (50:19): [True: 100.00%, False: 0.00%]
------------------
51| 16| wt.ud(d);
52| 16| wt.uw(c);
53| 500k| for (int i = v; i != -1; i = pa[i]) wt.uw(i);
^500k ^500k
------------------
| Branch (53:19): [True: 100.00%, False: 0.00%]
------------------
54| 16| return 0;
55| 16|}