1- from typing import List , Tuple
2-
3-
41class EulerTour :
5- def __init__ (self , edges : List [Tuple [int , int , int ]], root : int = 0 ) -> None :
6- """
2+ def __init__ (self , edges : list [tuple [int , int , int ]], root : int = 0 ) -> None :
3+ """オイラーツアーのライブラリ
4+
75 edges[i] = (u, v, w)
86 なお閉路がない、連結という前提 エラー処理をしていない
97
@@ -14,15 +12,14 @@ def __init__(self, edges: List[Tuple[int, int, int]], root: int = 0) -> None:
1412 Warning:
1513 ac-library-pythonを__init__内で使用しているので注意
1614 定数倍が遅い事に注意 あとメモリも注意 結構リストを使用している
17- """
18- # assert len(edges) >= 1
1915
16+ """
2017 from atcoder .segtree import SegTree
2118
2219 self .edges = edges
2320 self ._n = max ([max (u , v ) for u , v , w in edges ]) + 1
2421 self .root = root
25- self .graph : List [ List [ Tuple [int , int , int ]]] = [[] for _ in [0 ] * self ._n ]
22+ self .graph : list [ list [ tuple [int , int , int ]]] = [[] for _ in [0 ] * self ._n ]
2623
2724 for i , (u , v , w ) in enumerate (edges ):
2825 self .graph [u ].append ((v , w , i ))
@@ -37,12 +34,10 @@ def __init__(self, edges: List[Tuple[int, int, int]], root: int = 0) -> None:
3734 [(d , i ) for i , d in enumerate (self .depth )],
3835 )
3936
40- return
41-
4237 def _build (self ) -> None :
43- self .euler_tour : List [ Tuple [int , int ]] = [(0 , - 1 )]
44- self .edge_cost : List [int ] = [0 ]
45- self .depth : List [int ] = [0 ]
38+ self .euler_tour : list [ tuple [int , int ]] = [(0 , - 1 )]
39+ self .edge_cost : list [int ] = [0 ]
40+ self .depth : list [int ] = [0 ]
4641
4742 def dfs (cur : int , p : int = - 1 , d : int = 0 ) -> None :
4843 for nxt , w , i in self .graph [cur ]:
@@ -78,8 +73,6 @@ def dfs(cur: int, p: int = -1, d: int = 0) -> None:
7873 self .last_arrival [u ] = i
7974
8075 def lca (self , a : int , b : int ) -> int :
81- # assert 0 <= a < self._n and 0 <= b < self._n
82-
8376 l , r = (
8477 min (self .first_arrival [a ], self .first_arrival [b ]),
8578 max (self .last_arrival [a ], self .last_arrival [b ]),
@@ -92,10 +85,7 @@ def path_query_from_root(self, u: int) -> int:
9285 return self .segtree_edgecost .prod (0 , self .first_arrival [u ] + 1 )
9386
9487 def path_query (self , a : int , b : int ) -> int :
95- """
96- aからbへの最短経路
97- """
98- # assert 0 <= a < self._n and 0 <= b < self._n
88+ """aからbへの最短経路"""
9989 try :
10090 l = self .lca (a , b )
10191 except IndexError :
@@ -108,6 +98,5 @@ def path_query(self, a: int, b: int) -> int:
10898 )
10999
110100 def change_edge_cost (self , i : int , w : int ) -> None :
111- # assert 0 <= i < len(self.edges)
112101 self .segtree_edgecost .set (self .edge_plus [i ], w )
113102 self .segtree_edgecost .set (self .edge_minus [i ], - w )
0 commit comments