結果
問題 | No.1254 補強への架け橋 |
ユーザー | kemuniku |
提出日時 | 2024-09-27 02:50:27 |
言語 | Nim (2.0.2) |
結果 |
AC
|
実行時間 | 178 ms / 2,000 ms |
コード長 | 25,237 bytes |
コンパイル時間 | 6,232 ms |
コンパイル使用メモリ | 81,792 KB |
実行使用メモリ | 29,440 KB |
最終ジャッジ日時 | 2024-09-27 02:50:49 |
合計ジャッジ時間 | 20,897 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 1 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 1 ms
5,376 KB |
testcase_12 | AC | 2 ms
5,376 KB |
testcase_13 | AC | 2 ms
5,376 KB |
testcase_14 | AC | 2 ms
5,376 KB |
testcase_15 | AC | 1 ms
5,376 KB |
testcase_16 | AC | 1 ms
5,376 KB |
testcase_17 | AC | 1 ms
5,376 KB |
testcase_18 | AC | 2 ms
5,376 KB |
testcase_19 | AC | 2 ms
5,376 KB |
testcase_20 | AC | 2 ms
5,376 KB |
testcase_21 | AC | 1 ms
5,376 KB |
testcase_22 | AC | 2 ms
5,376 KB |
testcase_23 | AC | 2 ms
5,376 KB |
testcase_24 | AC | 2 ms
5,376 KB |
testcase_25 | AC | 2 ms
5,376 KB |
testcase_26 | AC | 1 ms
5,376 KB |
testcase_27 | AC | 2 ms
5,376 KB |
testcase_28 | AC | 2 ms
5,376 KB |
testcase_29 | AC | 1 ms
5,376 KB |
testcase_30 | AC | 2 ms
5,376 KB |
testcase_31 | AC | 1 ms
5,376 KB |
testcase_32 | AC | 2 ms
5,376 KB |
testcase_33 | AC | 2 ms
5,376 KB |
testcase_34 | AC | 2 ms
5,376 KB |
testcase_35 | AC | 2 ms
5,376 KB |
testcase_36 | AC | 2 ms
5,376 KB |
testcase_37 | AC | 2 ms
5,376 KB |
testcase_38 | AC | 2 ms
5,376 KB |
testcase_39 | AC | 2 ms
5,376 KB |
testcase_40 | AC | 1 ms
5,376 KB |
testcase_41 | AC | 2 ms
5,376 KB |
testcase_42 | AC | 1 ms
5,376 KB |
testcase_43 | AC | 3 ms
5,376 KB |
testcase_44 | AC | 2 ms
5,376 KB |
testcase_45 | AC | 2 ms
5,376 KB |
testcase_46 | AC | 2 ms
5,376 KB |
testcase_47 | AC | 3 ms
5,376 KB |
testcase_48 | AC | 2 ms
5,376 KB |
testcase_49 | AC | 2 ms
5,376 KB |
testcase_50 | AC | 2 ms
5,376 KB |
testcase_51 | AC | 3 ms
5,376 KB |
testcase_52 | AC | 3 ms
5,376 KB |
testcase_53 | AC | 3 ms
5,376 KB |
testcase_54 | AC | 2 ms
5,376 KB |
testcase_55 | AC | 2 ms
5,376 KB |
testcase_56 | AC | 2 ms
5,376 KB |
testcase_57 | AC | 2 ms
5,376 KB |
testcase_58 | AC | 2 ms
5,376 KB |
testcase_59 | AC | 1 ms
5,376 KB |
testcase_60 | AC | 2 ms
5,376 KB |
testcase_61 | AC | 2 ms
5,376 KB |
testcase_62 | AC | 2 ms
5,376 KB |
testcase_63 | AC | 11 ms
5,632 KB |
testcase_64 | AC | 4 ms
5,376 KB |
testcase_65 | AC | 7 ms
5,376 KB |
testcase_66 | AC | 6 ms
5,376 KB |
testcase_67 | AC | 4 ms
5,376 KB |
testcase_68 | AC | 7 ms
5,376 KB |
testcase_69 | AC | 9 ms
5,376 KB |
testcase_70 | AC | 5 ms
5,376 KB |
testcase_71 | AC | 4 ms
5,376 KB |
testcase_72 | AC | 9 ms
5,376 KB |
testcase_73 | AC | 4 ms
5,376 KB |
testcase_74 | AC | 8 ms
5,376 KB |
testcase_75 | AC | 6 ms
5,376 KB |
testcase_76 | AC | 3 ms
5,376 KB |
testcase_77 | AC | 6 ms
5,376 KB |
testcase_78 | AC | 10 ms
5,504 KB |
testcase_79 | AC | 10 ms
5,760 KB |
testcase_80 | AC | 9 ms
5,376 KB |
testcase_81 | AC | 11 ms
5,376 KB |
testcase_82 | AC | 10 ms
5,376 KB |
testcase_83 | AC | 178 ms
27,520 KB |
testcase_84 | AC | 127 ms
26,112 KB |
testcase_85 | AC | 69 ms
17,664 KB |
testcase_86 | AC | 108 ms
23,296 KB |
testcase_87 | AC | 121 ms
24,960 KB |
testcase_88 | AC | 16 ms
6,528 KB |
testcase_89 | AC | 132 ms
27,264 KB |
testcase_90 | AC | 79 ms
18,688 KB |
testcase_91 | AC | 54 ms
15,360 KB |
testcase_92 | AC | 28 ms
9,472 KB |
testcase_93 | AC | 95 ms
22,400 KB |
testcase_94 | AC | 87 ms
20,096 KB |
testcase_95 | AC | 87 ms
20,224 KB |
testcase_96 | AC | 133 ms
25,472 KB |
testcase_97 | AC | 47 ms
13,312 KB |
testcase_98 | AC | 133 ms
25,728 KB |
testcase_99 | AC | 75 ms
17,536 KB |
testcase_100 | AC | 149 ms
27,904 KB |
testcase_101 | AC | 26 ms
8,960 KB |
testcase_102 | AC | 14 ms
6,272 KB |
testcase_103 | AC | 28 ms
9,344 KB |
testcase_104 | AC | 40 ms
12,288 KB |
testcase_105 | AC | 99 ms
22,400 KB |
testcase_106 | AC | 51 ms
14,208 KB |
testcase_107 | AC | 147 ms
27,648 KB |
testcase_108 | AC | 134 ms
26,240 KB |
testcase_109 | AC | 103 ms
22,144 KB |
testcase_110 | AC | 92 ms
19,968 KB |
testcase_111 | AC | 99 ms
21,120 KB |
testcase_112 | AC | 36 ms
11,648 KB |
testcase_113 | AC | 89 ms
19,456 KB |
testcase_114 | AC | 51 ms
13,952 KB |
testcase_115 | AC | 17 ms
6,784 KB |
testcase_116 | AC | 61 ms
16,000 KB |
testcase_117 | AC | 38 ms
11,776 KB |
testcase_118 | AC | 128 ms
25,344 KB |
testcase_119 | AC | 67 ms
17,408 KB |
testcase_120 | AC | 119 ms
24,704 KB |
testcase_121 | AC | 31 ms
10,368 KB |
testcase_122 | AC | 59 ms
15,616 KB |
testcase_123 | AC | 2 ms
5,376 KB |
testcase_124 | AC | 139 ms
29,312 KB |
testcase_125 | AC | 137 ms
29,440 KB |
ソースコード
import macros;macro ImportExpand(s:untyped):untyped = parseStmt($s[2]) ImportExpand "cplib/tree/heavylightdecomposition.nim" <=== "when not declared CPLIB_TREE_HLD:\n const CPLIB_TREE_HLD* = 1\n import sequtils\n import algorithm\n import sets\n #[ import cplib/graph/graph ]#\n when not declared CPLIB_GRAPH_GRAPH:\n const CPLIB_GRAPH_GRAPH* = 1\n \n import sequtils\n import math\n type DynamicGraph*[T] = ref object of RootObj\n edges*: seq[seq[(int32, T)]]\n len*: int\n type StaticGraph*[T] = ref object of RootObj\n src*, dst*: seq[int32]\n cost*: seq[T]\n elist*: seq[(int32, T)]\n start*: seq[int32]\n len*: int\n \n type WeightedDirectedGraph*[T] = ref object of DynamicGraph[T]\n type WeightedUnDirectedGraph*[T] = ref object of DynamicGraph[T]\n type UnWeightedDirectedGraph* = ref object of DynamicGraph[int]\n type UnWeightedUnDirectedGraph* = ref object of DynamicGraph[int]\n type WeightedDirectedStaticGraph*[T] = ref object of StaticGraph[T]\n type WeightedUnDirectedStaticGraph*[T] = ref object of StaticGraph[T]\n type UnWeightedDirectedStaticGraph* = ref object of StaticGraph[int]\n type UnWeightedUnDirectedStaticGraph* = ref object of StaticGraph[int]\n \n type GraphTypes*[T] = DynamicGraph[T] or StaticGraph[T]\n type DirectedGraph* = WeightedDirectedGraph or UnWeightedDirectedGraph or WeightedDirectedStaticGraph or UnWeightedDirectedStaticGraph\n type UnDirectedGraph* = WeightedUnDirectedGraph or UnWeightedUnDirectedGraph or WeightedUnDirectedStaticGraph or UnWeightedUnDirectedStaticGraph\n type WeightedGraph*[T] = WeightedDirectedGraph[T] or WeightedUnDirectedGraph[T] or WeightedDirectedStaticGraph[T] or WeightedUnDirectedStaticGraph[T]\n type UnWeightedGraph* = UnWeightedDirectedGraph or UnWeightedUnDirectedGraph or UnWeightedDirectedStaticGraph or UnWeightedUnDirectedStaticGraph\n type DynamicGraphTypes* = WeightedDirectedGraph or UnWeightedDirectedGraph or WeightedUnDirectedGraph or UnWeightedUnDirectedGraph\n type StaticGraphTypes* = WeightedDirectedStaticGraph or UnWeightedDirectedStaticGraph or WeightedUnDirectedStaticGraph or UnWeightedUnDirectedStaticGraph\n \n proc add_edge_dynamic_impl*[T](g: DynamicGraph[T], u, v: int, cost: T, directed: bool) =\n g.edges[u].add((v.int32, cost))\n if not directed: g.edges[v].add((u.int32, cost))\n \n proc initWeightedDirectedGraph*(N: int, edgetype: typedesc = int): WeightedDirectedGraph[edgetype] =\n result = WeightedDirectedGraph[edgetype](edges: newSeq[seq[(int32, edgetype)]](N), len: N)\n proc add_edge*[T](g: var WeightedDirectedGraph[T], u, v: int, cost: T) =\n g.add_edge_dynamic_impl(u, v, cost, true)\n \n proc initWeightedUnDirectedGraph*(N: int, edgetype: typedesc = int): WeightedUnDirectedGraph[edgetype] =\n result = WeightedUnDirectedGraph[edgetype](edges: newSeq[seq[(int32, edgetype)]](N), len: N)\n proc add_edge*[T](g: var WeightedUnDirectedGraph[T], u, v: int, cost: T) =\n g.add_edge_dynamic_impl(u, v, cost, false)\n \n proc initUnWeightedDirectedGraph*(N: int): UnWeightedDirectedGraph =\n result = UnWeightedDirectedGraph(edges: newSeq[seq[(int32, int)]](N), len: N)\n proc add_edge*(g: var UnWeightedDirectedGraph, u, v: int) =\n g.add_edge_dynamic_impl(u, v, 1, true)\n \n proc initUnWeightedUnDirectedGraph*(N: int): UnWeightedUnDirectedGraph =\n result = UnWeightedUnDirectedGraph(edges: newSeq[seq[(int32, int)]](N), len: N)\n proc add_edge*(g: var UnWeightedUnDirectedGraph, u, v: int) =\n g.add_edge_dynamic_impl(u, v, 1, false)\n \n proc len*[T](G: WeightedGraph[T]): int = G.len\n proc len*(G: UnWeightedGraph): int = G.len\n \n iterator `[]`*[T](g: WeightedDirectedGraph[T] or WeightedUnDirectedGraph[T], x: int): (int, T) =\n for e in g.edges[x]: yield (e[0].int, e[1])\n iterator `[]`*(g: UnWeightedDirectedGraph or UnWeightedUnDirectedGraph, x: int): int =\n for e in g.edges[x]: yield e[0].int\n \n proc add_edge_static_impl*[T](g: StaticGraph[T], u, v: int, cost: T, directed: bool) =\n g.src.add(u.int32)\n g.dst.add(v.int32)\n g.cost.add(cost)\n if not directed:\n g.src.add(v.int32)\n g.dst.add(u.int32)\n g.cost.add(cost)\n \n proc build_impl*[T](g: StaticGraph[T]) =\n g.start = newSeqWith(g.len + 1, 0.int32)\n for i in 0..<g.src.len:\n g.start[g.src[i]] += 1\n g.start.cumsum\n g.elist = newSeq[(int32, T)](g.start[^1])\n for i in countdown(g.src.len - 1, 0):\n var u = g.src[i]\n var v = g.dst[i]\n g.start[u] -= 1\n g.elist[g.start[u]] = (v, g.cost[i])\n proc build*(g: StaticGraphTypes) = g.build_impl()\n \n proc initWeightedDirectedStaticGraph*(N: int, edgetype: typedesc = int, capacity: int = 0): WeightedDirectedStaticGraph[edgetype] =\n result = WeightedDirectedStaticGraph[edgetype](\n src: newSeqOfCap[int32](capacity),\n dst: newSeqOfCap[int32](capacity),\n cost: newSeqOfCap[edgetype](capacity),\n elist: newSeq[(int32, edgetype)](0),\n start: newSeq[int32](0),\n len: N\n )\n proc add_edge*[T](g: var WeightedDirectedStaticGraph[T], u, v: int, cost: T) =\n g.add_edge_static_impl(u, v, cost, true)\n \n proc initWeightedUnDirectedStaticGraph*(N: int, edgetype: typedesc = int, capacity: int = 0): WeightedUnDirectedStaticGraph[edgetype] =\n result = WeightedUnDirectedStaticGraph[edgetype](\n src: newSeqOfCap[int32](capacity*2),\n dst: newSeqOfCap[int32](capacity*2),\n cost: newSeqOfCap[edgetype](capacity*2),\n elist: newSeq[(int32, edgetype)](0),\n start: newSeq[int32](0),\n len: N\n )\n proc add_edge*[T](g: var WeightedUnDirectedStaticGraph[T], u, v: int, cost: T) =\n g.add_edge_static_impl(u, v, cost, false)\n \n proc initUnWeightedDirectedStaticGraph*(N: int, capacity: int = 0): UnWeightedDirectedStaticGraph =\n result = UnWeightedDirectedStaticGraph(\n src: newSeqOfCap[int32](capacity),\n dst: newSeqOfCap[int32](capacity),\n cost: newSeqOfCap[int](capacity),\n elist: newSeq[(int32, int)](0),\n start: newSeq[int32](0),\n len: N\n )\n proc add_edge*(g: var UnWeightedDirectedStaticGraph, u, v: int) =\n g.add_edge_static_impl(u, v, 1, true)\n \n proc initUnWeightedUnDirectedStaticGraph*(N: int, capacity: int = 0): UnWeightedUnDirectedStaticGraph =\n result = UnWeightedUnDirectedStaticGraph(\n src: newSeqOfCap[int32](capacity*2),\n dst: newSeqOfCap[int32](capacity*2),\n cost: newSeqOfCap[int](capacity*2),\n elist: newSeq[(int32, int)](0),\n start: newSeq[int32](0),\n len: N\n )\n proc add_edge*(g: var UnWeightedUnDirectedStaticGraph, u, v: int) =\n g.add_edge_static_impl(u, v, 1, false)\n \n proc static_graph_initialized_check*[T](g: StaticGraph[T]) = assert g.start.len > 0, \"Static Graph must be initialized before use.\"\n \n iterator `[]`*[T](g: WeightedDirectedStaticGraph[T] or WeightedUnDirectedStaticGraph[T], x: int): (int, T) =\n g.static_graph_initialized_check()\n for i in g.start[x]..<g.start[x+1]: yield (g.elist[i][0].int, g.elist[i][1])\n iterator `[]`*(g: UnWeightedDirectedStaticGraph or UnWeightedUnDirectedStaticGraph, x: int): int =\n g.static_graph_initialized_check()\n for i in g.start[x]..<g.start[x+1]: yield g.elist[i][0].int\n \n iterator to_and_cost*[T](g: DynamicGraph[T], x: int): (int, T) =\n for e in g.edges[x]: yield (e[0].int, e[1])\n iterator to_and_cost*[T](g: StaticGraph[T], x: int): (int, T) =\n g.static_graph_initialized_check()\n for i in g.start[x]..<g.start[x+1]: yield (g.elist[i][0].int, g.elist[i][1])\n \n import tables\n \n type UnWeightedUnDirectedTableGraph*[T] = object \n toi* : Table[T,int]\n v* : seq[T]\n graph* : UnWeightedUnDirectedGraph\n \n type UnWeightedDirectedTableGraph*[T] = object \n toi* : Table[T,int]\n v* : seq[T]\n graph* : UnWeightedDirectedGraph\n \n type WeightedUnDirectedTableGraph*[T,S] = object \n toi* : Table[T,int]\n v* : seq[T]\n graph* : WeightedUnDirectedGraph[S]\n \n type WeightedDirectedTableGraph*[T,S] = object \n toi* : Table[T,int]\n v* : seq[T]\n graph* : WeightedDirectedGraph[S]\n \n type UnWeightedTableGraph*[T] = UnWeightedUnDirectedTableGraph[T] or UnWeightedDirectedTableGraph[T]\n type WeightedTableGraph*[T,S] = WeightedUnDirectedTableGraph[T,S] or WeightedDirectedTableGraph[T,S]\n \n proc initUnWeightedUnDirectedTableGraph*[T](V:seq[T]):UnWeightedUnDirectedTableGraph[T]=\n for i in 0..<len(V):\n result.toi[V[i]] = i\n result.graph = initUnWeightedUnDirectedGraph(len(V))\n result.v = V\n \n proc initUnWeightedDirectedTableGraph*[T](V:seq[T]):UnWeightedDirectedTableGraph[T]=\n for i in 0..<len(V):\n result.toi[V[i]] = i\n result.graph = initUnWeightedDirectedGraph(len(V))\n result.v = V\n \n proc initWeightedUnDirectedTableGraph*[T](V:seq[T],S:typedesc = int):WeightedUnDirectedTableGraph[T,S]=\n for i in 0..<len(V):\n result.toi[V[i]] = i\n result.graph = initWeightedUnDirectedGraph(len(V),S)\n result.v = V\n \n proc initWeightedDirectedTableGraph*[T](V:seq[T],S:typedesc = int):WeightedDirectedTableGraph[T,S]=\n for i in 0..<len(V):\n result.toi[V[i]] = i\n result.graph = initWeightedDirectedGraph(len(V),S)\n result.v = V\n \n proc add_edge*[T](g: var UnWeightedTableGraph[T],u,v:int)=\n g.graph.add_edge(g.toi[u],g.toi[v])\n \n proc add_edge*[T,S](g: var WeightedTableGraph[T,S],u,v:int,cost:S)=\n g.graph.add_edge(g.toi[u],g.toi[v],cost)\n \n iterator `[]`*[T,S](g: WeightedDirectedTableGraph[T,S] or WeightedUnDirectedTableGraph[T,S], x: T): (T, S) = \n for (x,y) in g.graph[g.toi[x]]:\n yield (g.v[x],y)\n iterator `[]`*[T](g: UnWeightedDirectedTableGraph[T] or UnWeightedUnDirectedTableGraph[T], x: T): T = \n for x in g.graph[g.toi[x]]:\n yield g.v[x]\n \n # https://atcoder.jp/contests/abc337/submissions/50216964\n # ↑上記の提出より引用\n type HeavyLightDecomposition* = ref object \n N*: int\n P*, PP*, PD*, D*, I*, rangeL*, rangeR*: seq[int]\n proc initHld*(g: UnDirectedGraph, root: int): HeavyLightDecomposition =\n var hld = HeavyLightDecomposition()\n var n: int = g.len\n hld.N = n\n hld.P = newSeqWith(n, -1)\n hld.I = newSeqWith(n, 0)\n hld.I[0] = root\n var iI = 1\n for i in 0..<n:\n var p = hld.I[i]\n for e in g[p]:\n if hld.P[p] != e:\n hld.I[iI] = e\n hld.P[e] = p\n iI += 1\n var Z = newSeqWith(n, 1)\n var nx = newSeqWith(n, -1)\n hld.PP = newSeqWith(n, 0)\n for i in 0..<n:\n hld.PP[i] = i\n for i in 1..<n:\n var p = hld.I[n-i]\n Z[hld.P[p]] += Z[p]\n if nx[hld.P[p]] == -1 or Z[nx[hld.P[p]]] < Z[p]:\n nx[hld.P[p]] = p\n for p in hld.I:\n if nx[p] != -1:\n hld.PP[nx[p]] = p\n hld.PD = newSeqWith(n, n)\n hld.PD[root] = 0\n hld.D = newSeqWith(n, 0)\n for p in hld.I:\n if p != root:\n hld.PP[p] = hld.PP[hld.PP[p]]\n hld.PD[p] = min(hld.PD[hld.PP[p]], hld.PD[hld.P[p]]+1)\n hld.D[p] = hld.D[hld.P[p]]+1\n hld.rangeL = newSeqWith(n, 0)\n hld.rangeR = newSeqWith(n, 0)\n for p in hld.I:\n hld.rangeR[p] = hld.rangeL[p] + Z[p]\n var ir = hld.rangeR[p]\n for e in g[p]:\n if hld.P[p] != e and e != nx[p]:\n ir -= Z[e]\n hld.rangeL[e] = ir\n if nx[p] != -1:\n hld.rangeL[nx[p]] = hld.rangeL[p] + 1\n for i in 0..<n:\n hld.I[hld.rangeL[i]] = i\n return hld\n proc initHld*(g: DirectedGraph, root: int): HeavyLightDecomposition =\n var n = g.len\n var gn = initUnWeightedUnDirectedStaticGraph(n)\n var seen = initHashSet[(int, int)]()\n for i in 0..<n:\n for (j, _) in g[i]:\n if (i, j) notin seen:\n gn.add_edge(i, j)\n seen.incl((i, j))\n seen.incl((j, i))\n gn.build\n return initHld(gn, root)\n proc initHld*(adj: seq[seq[int]], root: int): HeavyLightDecomposition =\n var n = adj.len\n var gn = initUnWeightedUnDirectedStaticGraph(n)\n var seen = initHashSet[(int, int)]()\n for i in 0..<n:\n for j in adj[i]:\n if (i, j) notin seen:\n gn.add_edge(i, j)\n seen.incl((i, j))\n seen.incl((j, i))\n gn.build\n return initHld(gn, root)\n proc numVertices*(hld: HeavyLightDecomposition): int = hld.N\n proc depth*(hld: HeavyLightDecomposition, p: int): int = hld.D[p]\n proc toSeq*(hld: HeavyLightDecomposition, vtx: int): int = hld.rangeL[vtx]\n proc toVtx*(hld: HeavyLightDecomposition, seqidx: int): int = hld.I[seqidx]\n proc toSeq2In*(hld: HeavyLightDecomposition, vtx: int): int = hld.rangeL[vtx] * 2 - hld.D[vtx]\n proc toSeq2Out*(hld: HeavyLightDecomposition, vtx: int): int = hld.rangeR[vtx] * 2 - hld.D[vtx] - 1\n proc parentOf*(hld: HeavyLightDecomposition, v: int): int = hld.P[v]\n proc heavyRootOf*(hld: HeavyLightDecomposition, v: int): int = hld.PP[v]\n proc heavyChildOf*(hld: HeavyLightDecomposition, v: int): int =\n if hld.toSeq(v) == hld.N-1:\n return -1\n var cand = hld.toVtx(hld.toSeq(v) + 1)\n if hld.PP[v] == hld.PP[cand]:\n return cand\n -1\n proc lca*(hld: HeavyLightDecomposition, u: int, v: int): int =\n var (u, v) = (u, v)\n if hld.PD[u] < hld.PD[v]:\n swap(u, v)\n while hld.PD[u] > hld.PD[v]:\n u = hld.P[hld.PP[u]]\n while hld.PP[u] != hld.PP[v]:\n u = hld.P[hld.PP[u]]\n v = hld.P[hld.PP[v]]\n if hld.D[u] > hld.D[v]:\n return v\n u\n proc dist*(hld: HeavyLightDecomposition, u: int, v: int): int =\n hld.depth(u) + hld.depth(v) - hld.depth(hld.lca(u, v)) * 2\n proc path*(hld: HeavyLightDecomposition, r: int, c: int, include_root: bool, reverse_path: bool): seq[(int, int)] =\n var (r, c) = (r, c)\n var k = hld.PD[c] - hld.PD[r] + 1\n if k <= 0:\n return @[]\n var res = newSeqWith(k, (0, 0))\n for i in 0..<k-1:\n res[i] = (hld.rangeL[hld.PP[c]], hld.rangeL[c] + 1)\n c = hld.P[hld.PP[c]]\n if hld.PP[r] != hld.PP[c] or hld.D[r] > hld.D[c]:\n return @[]\n var root_off = int(not include_root)\n res[^1] = (hld.rangeL[r]+root_off, hld.rangeL[c]+1)\n if res[^1][0] == res[^1][1]:\n discard res.pop()\n k -= 1\n if reverse_path:\n for i in 0..<k:\n res[i] = (hld.N - res[i][1], hld.N - res[i][0])\n else:\n res.reverse()\n res\n proc subtree*(hld: HeavyLightDecomposition, p: int): (int, int) = (hld.rangeL[p], hld.rangeR[p])\n proc median*(hld: HeavyLightDecomposition, x: int, y: int, z: int): int =\n hld.lca(x, y) xor hld.lca(y, z) xor hld.lca(x, z)\n proc la*(hld: HeavyLightDecomposition, starting: int, goal: int, d: int): int =\n var (u, v, d) = (starting, goal, d)\n if d < 0:\n return -1\n var g = hld.lca(u, v)\n var dist0 = hld.D[u] - hld.D[g] * 2 + hld.D[v]\n if dist0 < d:\n return -1\n var p = u\n if hld.D[u] - hld.D[g] < d:\n p = v\n d = dist0 - d\n while hld.D[p] - hld.D[hld.PP[p]] < d:\n d -= hld.D[p] - hld.D[hld.PP[p]] + 1\n p = hld.P[hld.PP[p]]\n hld.I[hld.rangeL[p] - d]\n iterator children*(hld: HeavyLightDecomposition, v: int): int =\n var s = hld.rangeL[v] + 1\n while s < hld.rangeR[v]:\n var w = hld.toVtx(s)\n yield w\n s += hld.rangeR[w] - hld.rangeL[w]\n \n\n proc initAuxiliaryTree*(hld:HeavyLightDecomposition,v:seq[int]):UnWeightedUnDirectedTableGraph[int]=\n ## 根が欲しかったらG.v[0]を使ってください けむにく\n var v = v.sortedByit(hld.toseq(it))\n for i in 0..<(len(v)-1):\n v.add(hld.lca(v[i],v[i+1]))\n v = v.sortedByIt(hld.toseq(it)).deduplicate(true)\n var stack :seq[int]\n result = initUnWeightedUnDirectedTableGraph[int](v)\n stack.add(v[0])\n \n for i in 1..<len(v):\n while len(stack) > 0 and hld.toSeq2Out(stack[^1]) < hld.toseq2In(v[i]):\n discard stack.pop()\n if len(stack) != 0:\n result.add_edge(stack[^1],v[i])\n stack.add(v[i])\n \n proc initAuxiliaryWeightedTree*(hld:HeavyLightDecomposition,v:seq[int]):WeightedUnDirectedTableGraph[int,int]=\n ## 根が欲しかったらG.v[0]を使ってください けむにく\n var v = v.sortedByit(hld.toseq(it))\n for i in 0..<(len(v)-1):\n v.add(hld.lca(v[i],v[i+1]))\n v = v.sortedByIt(hld.toseq(it)).deduplicate(true)\n var stack :seq[int]\n result = initWeightedUnDirectedTableGraph(v,int)\n stack.add(v[0])\n for i in 1..<len(v):\n while len(stack) > 0 and hld.toSeq2Out(stack[^1]) < hld.toseq2In(v[i]):\n discard stack.pop()\n if len(stack) != 0:\n result.add_edge(stack[^1],v[i],hld.depth(v[i])-hld.depth(stack[^1]))\n stack.add(v[i])\n\n" ImportExpand "cplib/graph/graph.nim" <=== "" import sequtils type NamoriGraph = ref object tree : HeavyLightDecomposition rootNo : seq[int] roots : seq[int] cyclesize : int proc initNamoriGraph(graph:UnWeightedUnDirectedGraph):NamoriGraph= var stack : seq[int] var sizes = newseqwith(len(graph),0) var rootNo = newseqwith(len(graph),-1) var tree = initUnWeightedUnDirectedGraph(len(graph)+1) var roots = newseqwith(len(graph),0) for i in 0..<len(graph): sizes[i] = len(graph.edges[i]) if sizes[i] == 1: stack.add(i) while len(stack) != 0: var i = stack.pop() for j in graph[i]: if sizes[j] != 1: tree.add_edge(i,j) sizes[j] -= 1 if sizes[j] == 1: stack.add(j) for i in 0..<len(graph): if sizes[i] != 1: var now = i var bef = -1 var tmp = 1 rootNo[i] = 0 while true: tree.add_edge(now,len(graph)) var next = -1 proc dfs(x,p:int)= roots[x] = now for y in graph[x]: if p != y: if sizes[y] == 1: dfs(y,x) elif bef != y: next = y dfs(now,-1) if next == -1 or next == i: break bef = now now = next rootNo[now] = tmp tmp += 1 #dump_graph(tree) return NamoriGraph(tree:tree.initHld(len(graph)),rootNo:rootNo,roots:roots,cyclesize:tmp) proc dist(namori:NamoriGraph,u,v:int):(int,int)= ## u,v間の距離を二通り返します ## (小さいほう,大きいほう) ## ただし、一通りしか存在しない場合二つ目のintは-1を返します。 var lca = namori.tree.lca(u,v) if lca != len(namori.rootNo): return (namori.tree.dist(u,v),-1) else: var x = namori.roots[u] var y = namori.roots[v] var a = abs(namori.rootNo[x]-namori.rootNo[y]) var b = namori.cyclesize - a var tmp = namori.tree.depth(u) + namori.tree.depth(v) - 2 return (min(a,b)+tmp,max(a,b)+tmp) proc incycle(namori:NamoriGraph,x:int):bool= return namori.rootNo[x] != -1 ImportExpand "cplib/tmpl/sheep.nim" <=== "when not declared CPLIB_TMPL_SHEEP:\n const CPLIB_TMPL_SHEEP* = 1\n {.warning[UnusedImport]: off.}\n {.hint[XDeclaredButNotUsed]: off.}\n import algorithm\n import sequtils\n import tables\n import macros\n import math\n import sets\n import strutils\n import strformat\n import sugar\n import heapqueue\n import streams\n import deques\n import bitops\n import std/lenientops\n import options\n #入力系\n proc scanf(formatstr: cstring){.header: \"<stdio.h>\", varargs.}\n proc getchar(): char {.importc: \"getchar_unlocked\", header: \"<stdio.h>\", discardable.}\n proc ii(): int {.inline.} = scanf(\"%lld\\n\", addr result)\n proc lii(N: int): seq[int] {.inline.} = newSeqWith(N, ii())\n proc si(): string {.inline.} =\n result = \"\"\n var c: char\n while true:\n c = getchar()\n if c == ' ' or c == '\\n':\n break\n result &= c\n #chmin,chmax\n template `max=`(x, y) = x = max(x, y)\n template `min=`(x, y) = x = min(x, y)\n #bit演算\n proc `%`*(x: int, y: int): int =\n result = x mod y\n if y > 0 and result < 0: result += y\n if y < 0 and result > 0: result += y\n proc `//`*(x: int, y: int): int{.inline.} =\n result = x div y\n if y > 0 and result * y > x: result -= 1\n if y < 0 and result * y < x: result -= 1\n proc `%=`(x: var int, y: int): void = x = x%y\n proc `//=`(x: var int, y: int): void = x = x//y\n proc `**`(x: int, y: int): int = x^y\n proc `**=`(x: var int, y: int): void = x = x^y\n proc `^`(x: int, y: int): int = x xor y\n proc `|`(x: int, y: int): int = x or y\n proc `&`(x: int, y: int): int = x and y\n proc `>>`(x: int, y: int): int = x shr y\n proc `<<`(x: int, y: int): int = x shl y\n proc `~`(x: int): int = not x\n proc `^=`(x: var int, y: int): void = x = x ^ y\n proc `&=`(x: var int, y: int): void = x = x & y\n proc `|=`(x: var int, y: int): void = x = x | y\n proc `>>=`(x: var int, y: int): void = x = x >> y\n proc `<<=`(x: var int, y: int): void = x = x << y\n proc `[]`(x: int, n: int): bool = (x and (1 shl n)) != 0\n #便利な変換\n proc `!`(x: char, a = '0'): int = int(x)-int(a)\n #定数\n #[ include cplib/utils/constants ]#\n when not declared CPLIB_UTILS_CONSTANTS:\n const CPLIB_UTILS_CONSTANTS* = 1\n const INF32*: int32 = 100100111.int32\n const INF64*: int = int(3300300300300300491)\n const INF = INF64\n #converter\n\n #range\n iterator range(start: int, ends: int, step: int): int =\n var i = start\n if step < 0:\n while i > ends:\n yield i\n i += step\n elif step > 0:\n while i < ends:\n yield i\n i += step\n iterator range(ends: int): int = (for i in 0..<ends: yield i)\n iterator range(start: int, ends: int): int = (for i in\n start..<ends: yield i)\n\n #joinが非stringでめちゃくちゃ遅いやつのパッチ\n proc join*[T: not string](a: openArray[T], sep: string = \"\"): string = a.mapit($it).join(sep)\n" var N = ii() var G = initUnWeightedUnDirectedGraph(N) var edges : seq[(int,int)] for i in range(N): var a,b = ii()-1 G.add_edge(a,b) edges.add (a,b) var namori = G.initNamoriGraph() var ans : seq[int] for i in range(N): var (a,b) = edges[i] if namori.incycle(a) and namori.incycle(b): ans.add(i+1) echo len(ans) echo ans.join(" ")