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