You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are given a connected weighted graph with (N) cities and (M) roads.
5
+
We are given a connected weighted graph with `N` cities and `M` roads.
6
6
Each road has a *danger value*.
7
7
8
-
For each query (s, t), we must find a path from (s) to (t) such that:
8
+
For each query `s t`, we must find a path from `s` to `t` such that:
9
9
10
10
> the maximum danger of any road on the path is minimized.
11
11
@@ -19,7 +19,8 @@ If we build a **Minimum Spanning Tree (MST)** of the graph, then:
19
19
20
20
> For any two nodes, the path between them in the MST minimizes the maximum edge weight among all possible paths in the original graph.
21
21
22
-
This is a well-known property of MSTs.
22
+
Why is this true? Intuitively, in an MST every edge is as "cheap" as possible: if there were a path between two nodes whose maximum edge could be made smaller by taking some other route in the original graph, we could replace the larger edge on the MST path with that smaller edge and still keep the graph connected. That would give us another spanning tree with strictly smaller maximum edge on that path, contradicting the fact that the original tree was minimum. So for any pair of nodes, the path inside the MST uses edges that are as small as possible, and the largest edge on that MST path is the smallest possible "bottleneck" among all paths in the original graph.
23
+
23
24
Therefore, once we construct the MST, we only need to answer queries on that tree.
24
25
25
26
---
@@ -31,21 +32,21 @@ Therefore, once we construct the MST, we only need to answer queries on that tre
31
32
3. Preprocess the tree using **Binary Lifting**
32
33
4. Store:
33
34
34
-
*`anc[u][i]` → (2^i)-th ancestor of node (u)
35
-
*`mx[u][i]` → maximum edge weight from (u) to its (2^i)-th ancestor
35
+
*`anc[u][i]` → `2^i`-th ancestor of node `u`
36
+
*`mx[u][i]` → maximum edge weight from `u` to its `2^i`-th ancestor
36
37
5. For each query:
37
38
38
-
* compute LCA of (u) and (v)
39
-
* find maximum edge on path (u to LCA) and (v to LCA) using binary lifting
39
+
* compute LCA of `u` and `v`
40
+
* find maximum edge on path (`u` to LCA) and (`v` to LCA) using binary lifting
40
41
* answer = max of the two above values.
41
42
42
43
---
43
44
44
45
## Complexity
45
46
46
-
* MST construction: (O(M log N))
47
-
* Binary lifting preprocessing: (O(N log N))
48
-
* Each query: (O(log N))
47
+
* MST construction: `O(M log N)`
48
+
* Binary lifting preprocessing: `O(N log N)`
49
+
* Each query: `O(log N)`
49
50
50
51
This easily fits within constraints.
51
52
@@ -56,172 +57,186 @@ This easily fits within constraints.
0 commit comments