#{.checks:off.} import macros;macro ImportExpand(s:untyped):untyped = parseStmt($s[2]) import macros 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: \"\", varargs.}\n proc getchar(): char {.importc: \"getchar_unlocked\", header: \"\", 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 = (((x mod y)+y) mod y)\n proc `//`(x: int, y: int): int = (((x) - (x%y)) div (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, 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 const INF = int(3300300300300300491)\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.. 0:\n if n mod 2 != 0: rev = mul(rev, a, m)\n if n > 1: a = mul(a, a, m)\n n = n shr 1\n return rev\n" ImportExpand "atcoder/modint.nim" <=== "when not declared ATCODER_MODINT_HPP:\n const ATCODER_MODINT_HPP* = 1\n import std/macros\n #[ import atcoder/generate_definitions ]#\n when not declared ATCODER_GENERATE_DEFINITIONS_NIM:\n const ATCODER_GENERATE_DEFINITIONS_NIM* = 1\n import std/macros\n \n type hasInv* = concept x\n x.inv()\n \n template generateDefinitions*(name, l, r, typeObj, typeBase, body: untyped): untyped {.dirty.} =\n proc name*(l, r: typeObj): auto {.inline.} =\n type T = l.type\n body\n proc name*(l: typeBase; r: typeObj): auto {.inline.} =\n type T = r.type\n body\n proc name*(l: typeObj; r: typeBase): auto {.inline.} =\n type T = l.type\n body\n \n template generatePow*(name) {.dirty.} =\n proc pow*(m: name; p: SomeInteger): name {.inline.} =\n when name is hasInv:\n if p < 0: return pow(m.inv(), -p)\n else:\n doAssert p >= 0\n if (p.type)(0) <= p:\n var\n p = p.uint\n m = m\n result = m.unit()\n while p > 0'u:\n if (p and 1'u) != 0'u: result *= m\n m *= m\n p = p shr 1'u\n proc `^`*[T:name](m: T; p: SomeInteger): T {.inline.} = m.pow(p)\n \n macro generateConverter*(name, from_type, to_type) =\n let fname = ident(\"to\" & $`name` & \"OfGenerateConverter\")\n quote do:\n type `name`* = `to_type`\n converter `fname`*(a:`from_type`):`name` {.used.} =\n `name`.init(a)\n discard\n\n type\n StaticModInt*[M: static[int]] = object\n a:uint32\n DynamicModInt*[T: static[int]] = object\n a:uint32\n\n type ModInt* = StaticModInt or DynamicModInt\n# type ModInt* = concept x, type T\n# T is StaticModInt or T is DynamicModInt\n\n proc isStaticModInt*(T:typedesc[ModInt]):bool = T is StaticModInt\n proc isDynamicModInt*(T:typedesc[ModInt]):bool = T is DynamicModInt\n #proc isModInt*(T:typedesc):bool = T.isStaticModInt or T.isDynamicModInt\n proc isStatic*(T:typedesc[ModInt]):bool = T is StaticModInt\n proc getMod*[M:static[int]](t:typedesc[StaticModInt[M]]):int {.inline.} = M\n\n\n #[ import atcoder/internal_math ]#\n when not declared ATCODER_INTERNAL_MATH_HPP:\n const ATCODER_INTERNAL_MATH_HPP* = 1\n import std/math\n \n # Fast moduler by barrett reduction\n # Reference: https:#en.wikipedia.org/wiki/Barrett_reduction\n # NOTE: reconsider after Ice Lake\n type Barrett* = object\n m*, im*:uint\n \n # @param m `1 <= m`\n proc initBarrett*(m:uint):auto = Barrett(m:m, im:cast[uint](-1) div m + 1)\n \n # @return m\n proc umod*(self: Barrett):uint =\n self.m\n \n {.emit: \"\"\"\n #include\n inline unsigned long long calc_mul(const unsigned long long &a, const unsigned long long &b){\n return (unsigned long long)(((unsigned __int128)(a)*b) >> 64);\n }\n \"\"\".}\n proc calc_mul*(a,b:culonglong):culonglong {.importcpp: \"calc_mul(#,#)\", nodecl, inline.}\n # @param a `0 <= a < m`\n # @param b `0 <= b < m`\n # @return `a * b % m`\n proc quo*(self: Barrett, n:int | uint):int =\n let n = n.uint\n let x = calc_mul(n.culonglong, self.im.culonglong).uint\n let r = n - x * self.m\n return int(if self.m <= r: x - 1 else: x)\n proc rem*(self: Barrett, n:int | uint):int =\n let n = n.uint\n let x = calc_mul(n.culonglong, self.im.culonglong).uint\n let r = n - x * self.m\n return int(if self.m <= r: r + self.m else: r)\n proc quorem*(self: Barrett, n:int | uint):(int, int) =\n let n = n.uint\n let x = calc_mul(n.culonglong, self.im.culonglong).uint\n let r = n - x * self.m\n return if self.m <= r: (int(x - 1), int(r + self.m)) else: (int(x), int(r))\n \n proc pow*(self: Barrett, n:uint | int, p:int):int =\n var\n a = self.rem(n)\n r:uint = if self.m == 1: 0 else: 1\n p = p\n while p > 0:\n if (p and 1) != 0: r = self.mul(r, a.uint)\n a = self.mul(a.uint, a.uint).int\n p = p shr 1\n return int(r)\n \n proc mul*(self: Barrett, a:uint, b:uint):uint {.inline.} =\n # [1] m = 1\n # a = b = im = 0, so okay\n \n # [2] m >= 2\n # im = ceil(2^64 / m)\n # -> im * m = 2^64 + r (0 <= r < m)\n # let z = a*b = c*m + d (0 <= c, d < m)\n # a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im\n # c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2\n # ((ab * im) >> 64) == c or c + 1\n let z = a * b\n # #ifdef _MSC_VER\n # unsigned long long x;\n # _umul128(z, im, &x);\n # #else\n ##TODO\n # unsigned long long x =\n # (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n # #endif\n #let x = calc_mul(z.culonglong, self.im.culonglong).uint\n #result = z - x * self.m\n #if self.m <= result: result += self.m\n return self.rem(z).uint\n \n # @param n `0 <= n`\n # @param m `1 <= m`\n # @return `(x ** n) % m`\n proc pow_mod_constexpr*(x, n, m:int):int =\n if m == 1: return 0\n var\n r = 1\n y = floorMod(x, m)\n n = n\n while n != 0:\n if (n and 1) != 0: r = (r * y) mod m\n y = (y * y) mod m\n n = n shr 1\n return r.int\n \n # Reference:\n # M. Forisek and J. Jancina,\n # Fast Primality Testing for Integers That Fit into a Machine Word\n # @param n `0 <= n`\n proc is_prime_constexpr*(n:int):bool =\n if n <= 1: return false\n if n == 2 or n == 7 or n == 61: return true\n if n mod 2 == 0: return false\n var d = n - 1\n while d mod 2 == 0: d = d div 2\n for a in [2, 7, 61]:\n var\n t = d\n y = pow_mod_constexpr(a, t, n)\n while t != n - 1 and y != 1 and y != n - 1:\n y = y * y mod n\n t = t shl 1\n if y != n - 1 and t mod 2 == 0:\n return false\n return true\n proc is_prime*[n:static[int]]():bool = is_prime_constexpr(n)\n # \n # # @param b `1 <= b`\n # # @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g\n proc inv_gcd*(a, b:int):(int,int) =\n var a = floorMod(a, b)\n if a == 0: return (b, 0)\n \n # Contracts:\n # [1] s - m0 * a = 0 (mod b)\n # [2] t - m1 * a = 0 (mod b)\n # [3] s * |m1| + t * |m0| <= b\n var\n s = b\n t = a\n m0 = 0\n m1 = 1\n \n while t != 0:\n var u = s div t\n s -= t * u;\n m0 -= m1 * u; # |m1 * u| <= |m1| * s <= b\n \n # [3]:\n # (s - t * u) * |m1| + t * |m0 - m1 * u|\n # <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)\n # = s * |m1| + t * |m0| <= b\n \n var tmp = s\n s = t;t = tmp;\n tmp = m0;m0 = m1;m1 = tmp;\n # by [3]: |m0| <= b/g\n # by g != b: |m0| < b/g\n if m0 < 0: m0 += b div s\n return (s, m0)\n \n # Compile time primitive root\n # @param m must be prime\n # @return primitive root (and minimum in now)\n proc primitive_root_constexpr*(m:int):int =\n if m == 2: return 1\n if m == 167772161: return 3\n if m == 469762049: return 3\n if m == 754974721: return 11\n if m == 998244353: return 3\n var divs:array[20, int]\n divs[0] = 2\n var cnt = 1\n var x = (m - 1) div 2\n while x mod 2 == 0: x = x div 2\n var i = 3\n while i * i <= x:\n if x mod i == 0:\n divs[cnt] = i\n cnt.inc\n while x mod i == 0:\n x = x div i\n i += 2\n if x > 1:\n divs[cnt] = x\n cnt.inc\n var g = 2\n while true:\n var ok = true\n for i in 0..= m:\n result += n * (n - 1) div 2 * (a div m)\n a = a mod m\n if b >= m:\n result += n * (b div m)\n b = b mod m\n \n let y_max = a * n + b\n if y_max < m: break\n # y_max < m * (n + 1)\n # floor(y_max / m) <= n\n n = y_max div m\n b = y_max mod m\n swap(m, a)\n discard\n\n proc getBarrett*[T:static[int]](t:typedesc[DynamicModInt[T]]):ptr Barrett =\n var Barrett_of_DynamicModInt {.global.} = initBarrett(998244353.uint)\n return Barrett_of_DynamicModInt.addr\n \n proc getMod*[T:static[int]](t:typedesc[DynamicModInt[T]]):uint32 {.inline.} =\n (t.getBarrett)[].m.uint32\n proc setMod*[T:static[int]](t:typedesc[DynamicModInt[T]], M:SomeInteger){.inline.} =\n (t.getBarrett)[] = initBarrett(M.uint)\n\n proc val*(m: ModInt): int {.inline.} = int(m.a)\n\n proc `$`*(m: StaticModInt or DynamicModInt): string {.inline.} = $(m.val())\n\n template umod*[T:ModInt](self: typedesc[T] or T):uint32 =\n when T is typedesc:\n when T is StaticModInt:\n T.M.uint32\n elif T is DynamicModInt:\n T.getMod()\n else:\n static: assert false\n else: T.umod\n\n template `mod`*[T:ModInt](self:typedesc[T] or T):int = T.umod.int\n\n proc init*[T:ModInt](t:typedesc[T], v: SomeInteger or T): auto {.inline.} =\n when v is T: return v\n else:\n when v is SomeUnsignedInt:\n if v.uint < T.umod:\n return T(a:v.uint32)\n else:\n return T(a:(v.uint mod T.umod.uint).uint32)\n else:\n var v = v.int\n if 0 <= v:\n if v < T.mod: return T(a:v.uint32)\n else: return T(a:(v mod T.mod).uint32)\n else:\n v = v mod T.mod\n if v < 0: v += T.mod\n return T(a:v.uint32)\n proc unit*[T:ModInt](t:typedesc[T] or T):T = T.init(1)\n\n template initModInt*(v: SomeInteger or ModInt; M: static[int] = 1_000_000_007): auto =\n StaticModInt[M].init(v)\n\n# TODO\n# converter toModInt[M:static[int]](n:SomeInteger):StaticModInt[M] {.inline.} = initModInt(n, M)\n\n# proc initModIntRaw*(v: SomeInteger; M: static[int] = 1_000_000_007): auto {.inline.} =\n# ModInt[M](v.uint32)\n proc raw*[T:ModInt](t:typedesc[T], v:SomeInteger):auto = T(a:v)\n\n proc inv*[T:ModInt](v:T):T {.inline.} =\n var\n a = v.a.int\n b = T.mod\n u = 1\n v = 0\n while b > 0:\n let t = a div b\n a -= t * b;swap(a, b)\n u -= t * v;swap(u, v)\n return T.init(u)\n\n\n proc `-`*[T:ModInt](m: T): T {.inline.} =\n if int(m.a) == 0: return m\n else: return T(a:m.umod() - m.a)\n\n proc `+=`*[T:ModInt](m: var T; n: SomeInteger | T):T {.inline discardable.} =\n m.a += T.init(n).a\n if m.a >= T.umod: m.a -= T.umod\n return m\n\n proc `-=`*[T:ModInt](m: var T; n: SomeInteger | T):T {.inline discardable.} =\n m.a -= T.init(n).a\n if m.a >= T.umod: m.a += T.umod\n return m\n\n proc `*=`*[T:ModInt](m: var T; n: SomeInteger | T):T {.inline discardable.} =\n when T is StaticModInt:\n m.a = (m.a.uint * T.init(n).a.uint mod T.umod).uint32\n elif T is DynamicModInt:\n m.a = T.getBarrett[].mul(m.a.uint, T.init(n).a.uint).uint32\n else:\n static: assert false\n return m\n\n proc `/=`*[T:ModInt](m: var T; n: SomeInteger | T):T {.inline discardable.} =\n m.a = (m.a.uint * T.init(n).inv().a.uint mod T.umod).uint32\n return m\n\n generateDefinitions(`+`, m, n, ModInt, SomeInteger):\n result = T.init(m)\n result += n\n\n generateDefinitions(`-`, m, n, ModInt, SomeInteger):\n result = T.init(m)\n result -= n\n\n generateDefinitions(`*`, m, n, ModInt, SomeInteger):\n result = T.init(m)\n result *= n\n\n generateDefinitions(`/`, m, n, ModInt, SomeInteger):\n result = T.init(m)\n result /= n\n\n generateDefinitions(`==`, m, n, ModInt, SomeInteger):\n result = (T.init(m).val() == T.init(n).val())\n\n proc inc*(m: var ModInt):ModInt {.inline discardable.} =\n m.a.inc\n if m.a == m.umod.uint32:\n m.a = 0\n return m\n proc `++`*(m: var ModInt):ModInt {.inline discardable.} = m.inc\n\n proc dec*(m: var ModInt):ModInt {.inline discardable.} =\n if m.a == 0.uint32:\n m.a = m.umod - 1\n else:\n m.a.dec\n return m\n proc `--`*(m: var ModInt):ModInt {.inline discardable.} = m.dec\n\n generatePow(ModInt)\n \n # TODO: intのところはSomeIntegerに拡張したいがそうするとSystem.nimのuintのconverterとバッティングする。。。\n template useStaticModint*(name, M) =\n generateConverter(name, int, StaticModInt[M])\n template useDynamicModInt*(name, M) =\n generateConverter(name, int, DynamicModInt[M])\n\n # TODO: Nimのstatic[int]を使うconverterがバグっていて個々に宣言しないとconverterが使えない\n # したがって、下記以外のmodintを使う場合はuseStaticModIntあるいはuseDynamicModIntで宣言が必要\n useStaticModInt(modint998244353, 998244353)\n useStaticModInt(modint1000000007, 1000000007)\n useDynamicModInt(modint, -1)\n\n import std/math as math_lib_modint\n proc estimateRational*(a:ModInt, ub:int = int(sqrt(float(ModInt.mod))), output_stderr:static[bool] = false):string =\n var v:seq[tuple[s, n, d: int]]\n for d in 1 .. ub:\n var n = (a * d).val\n # n or mod - n\n if n * 2 > a.mod:\n n = - (a.mod - n)\n if gcd(n, d) > 1: continue\n v.add((n.abs + d, n, d))\n v.sort\n when output_stderr:\n stderr.write \"estimation result: \", v\n return $v[0].n & \"/\" & $v[0].d\n\n # TODO:\n # Modint -> intのconverterあるとmint(2) * 3みたいなのがintになっちゃう\n # converter toInt*(m: ModInt):int {.inline.} = m.val\n\n\n discard\n" # see https://github.com/zer0-star/Nim-ACL/tree/master/src/atcoder/extra/graph/bellman_ford.nim ImportExpand "atcoder/extra/graph/bellman_ford.nim" <=== "when not declared ATCODER_BELLMAN_FORD_HPP:\n const ATCODER_BELLMAN_FORD_HPP* = 1\n import std/sequtils\n import std/algorithm\n #[ import atcoder/extra/other/inf ]#\n when not declared ATCODER_INF_HPP:\n const ATCODER_INF_HPP* = 1\n import sequtils\n template inf*(T: typedesc): untyped =\n when T is SomeFloat: T(Inf)\n elif T is SomeInteger: T.high div 2\n else:\n static: assert(false)\n template infRepr*[T](x: T): string =\n when T is seq or T is array:\n \"@[\" & x.mapIt(it.infRepr).join(\", \") & \"]\"\n elif x is SomeInteger or x is SomeFloat:\n when x is SomeUnsignedInt:\n if x >= T.inf: \"inf\"\n else: $x\n else:\n if x >= T.inf: \"inf\"\n elif x <= -T.inf: \"-inf\"\n else: $x\n else:\n $x\n proc isInf*[T](x: T): bool = x >= T.inf\n proc `∞`*(T: typedesc): T = T.inf\n proc `*!`*[T: SomeInteger](a, b: T): T =\n if a == T(0) or b == T(0): return T(0)\n var sgn = T(1)\n if a < T(0): sgn = -sgn\n if b < T(0): sgn = -sgn\n let a = abs(a)\n let b = abs(b)\n if b > T.inf div a: result = T.inf\n else: result = min(T.inf, a * b)\n result *= sgn\n proc `+!`*[T: SomeInteger](a, b: T): T =\n result = a + b\n result = min(T.inf, result)\n result = max(-T.inf, result)\n proc `-!`*[T: SomeInteger](a, b: T): T =\n result = a - b\n result = min(T.inf, result)\n result = max(-T.inf, result)\n discard\n #[ import atcoder/extra/graph/graph_template ]#\n when not declared ATCODER_GRAPH_TEMPLATE_HPP:\n const ATCODER_GRAPH_TEMPLATE_HPP* = 1\n import std/sequtils\n import std/tables\n \n type\n ADJTYPE_SEQ* = object\n ADJTYPE_TABLE* = object\n ADJTYPE_PROC* = object\n ADJTYPE_ITER* = object\n USEID_TRUE* = object\n USEID_FALSE* = object\n # Edge*[T] = ref object\n Edge*[T, U] = object\n src*,dst*:U\n weight*:T\n rev*:int\n Edges*[T, U] = seq[Edge[T, U]]\n Graph*[T, U, adjType, useId] = object\n len*:int\n when adjType is ADJTYPE_SEQ:\n adj*: seq[seq[Edge[T, U]]]\n elif adjType is ADJTYPE_TABLE:\n adj*: Table[U, seq[Edge[T, U]]]\n elif adjType is ADJTYPE_ITER:\n adj_iter*: iterator(u:U):tuple[dst:U, weight:T]\n elif adjType is ADJTYPE_PROC:\n adj*: proc(u:U):seq[tuple[dst:U, weight:T]]\n else:\n discard\n when useId is USEID_TRUE:\n id*:proc(u:U):int\n Matrix*[T] = seq[seq[T]]\n \n proc `@`*(e:Edge):auto = e.weight\n \n proc initEdge*[T, U](src,dst:U,weight:T = 1,rev:int = -1):Edge[T, U] =\n return Edge[T, U](src:src, dst:dst, weight:weight, rev:rev)\n proc `<`*[T, U](a, b:Edge[T, U]):bool = a.weight < b.weight\n \n proc initGraph*(n:int, T:typedesc = int, U:typedesc[int] = int):Graph[T, U, ADJTYPE_SEQ, USEID_FALSE] =\n return Graph[T, int, ADJTYPE_SEQ, USEID_FALSE](len:n, adj:newSeqWith(n, newSeq[Edge[T, U]]()))\n proc initGraph*(T:typedesc = int, U:typedesc = int):Graph[T, U, ADJTYPE_TABLE, USEID_FALSE] =\n return Graph[T, U, ADJTYPE_TABLE, USEID_FALSE](len: 0, adj:initTable[U, seq[Edge[T, U]]]())\n \n proc initGraph*[U](n:int, id:proc(u:U):int, T:typedesc = int):Graph[T, U, ADJTYPE_SEQ, USEID_TRUE] =\n return Graph[T, U, ADJTYPE_SEQ, USEID_TRUE](len:n, adj:newSeqWith(n,newSeq[Edge[T, U]]()), id:id)\n proc initGraph*[T, U](n:int, id:proc(u:U):int, adj:proc(u:U):seq[(U, T)]):Graph[T, U, ADJTYPE_PROC, USEID_TRUE] =\n return Graph[T, U, ADJTYPE_PROC, USEID_TRUE](len:n, adj:adj, id:id)\n proc initGraph*[T, U](n:int, id:proc(u:U):int, adj_iter:iterator(u:U):(U, T)):Graph[T, U, ADJTYPE_ITER, USEID_TRUE] =\n return Graph[T, U, ADJTYPE_ITER, USEID_TRUE](len:n, adj_iter:adj_iter, id:id)\n proc initGraph*[T, U](adj:proc(u:U):seq[(U, T)]):auto =\n return Graph[T, U, ADJTYPE_PROC, USEID_FALSE](len:0, adj:adj)\n proc initGraph*[T, U](adj_iter:iterator(u:U):(U, T)):auto =\n return Graph[T, U, ADJTYPE_ITER, USEID_FALSE](len:0, adj_iter:adj_iter)\n \n template `[]`*[G:Graph](g:G, u:G.U):auto =\n when G.adjType is ADJTYPE_SEQ:\n when u is int: g.adj[u]\n else: g.adj[g.id(u)]\n elif G.adjType is ADJTYPE_TABLE:\n if u notin g.adj:\n g.adj[u] = newSeq[Edge[G.T, G.U]]()\n g.adj[u]\n else:\n g.adj(u)\n \n proc addBiEdge*[T, U, adjType, useId](g:var Graph[T, U, adjType, useId], e:Edge[T, U]):void =\n when adjType is ADJTYPE_SEQ | ADJTYPE_TABLE:\n # var e_rev = initEdge[T](e.src, e.dst, e.weight, e.rev)\n if e.src != e.dst:\n var e_rev = e\n swap(e_rev.src, e_rev.dst)\n let (r, s) = (g[e.src].len, g[e.dst].len)\n g[e.src].add(e)\n g[e.dst].add(e_rev)\n g[e.src][^1].rev = s\n g[e.dst][^1].rev = r\n else:\n let r = g[e.src].len\n g[e.src].add(e)\n g[e.src][^1].rev = r\n else:\n static_assert false\n \n proc addBiEdge*[T, U, adjType, useId](g:var Graph[T, U, adjType, useId],src,dst:U,weight:T = 1):void =\n g.addBiEdge(initEdge(src, dst, weight))\n \n proc addEdge*[T, U, adjType, useId](g:var Graph[T, U, adjType, useId], e:Edge[T, U]) = g[e.src].add(e)\n proc addEdge*[T, U, adjType, useId](g:var Graph[T, U, adjType, useId], src, dst:U, weight:T = 1):void =\n g.addEdge(initEdge[T, U](src, dst, weight, -1))\n \n proc initUndirectedGraph*[T, U](n:int, a,b:seq[U], c:seq[T]):Graph[T, U, ADJTYPE_SEQ, USEID_FALSE] =\n result = initGraph[T](n, U)\n for i in 0.. 0:\n result.v = newSeqWith(len, default_val)\n when g.useId is USEID_TRUE:\n result.id = g.id\n \n proc `[]`*[U, useId, VAL](a:var NodeArray[U, VAL, useId], u:U):ptr[VAL] =\n when useId is USEID_TRUE or U is int:\n when U is int:\n var i = u\n else:\n var i = a.id(u)\n while i >= a.v.len:\n a.v.add a.default_val\n a.v[i].addr\n else:\n if u notin a.v:\n (a.v)[u] = a.default_val\n a.v[u].addr\n \n proc contains*[U, useId, VAL](a:var NodeArray[U, VAL, useId], u:U):bool =\n when useId is USEID_TRUE or U is int:\n when U is int:\n var i = u\n else:\n var i = a.id(u)\n return i < a.v.len\n else:\n return u in a.v\n discard\n\n type BellmanFordResult*[T, U] = object\n negative_cycle*:bool\n dist*: seq[T]\n prev*: seq[U]\n when U isnot int:\n id*: proc(u:U):int\n proc `[]`*[T, U](d:BellmanFordResult[T, U], u:U):T =\n let u = when U isnot int: d.id(u) else: u\n d.dist[u]\n proc path*[T, U](d:BellmanFordResult[T, U], t:U): seq[U] = \n var u = t\n while u >= 0:\n result.add(u)\n if u == d.prev[u]: break\n u = d.prev[u]\n result.reverse()\n\n proc bellman_ford*[G:Graph](g:G, s:G.U or seq[G.U]): auto =\n let n = g.len\n var\n dist = newSeqWith(n, G.T.inf)\n prev = newSeq[G.U](n)\n negative_cycle = false\n when s is G.U:\n dist[g.id(s)] = G.T(0)\n prev[g.id(s)] = s\n else:\n for s in s:\n dist[g.id(s)] = G.T(0)\n prev[g.id(s)] = s\n for k in 0.. t:\n dist[idst] = t\n prev[idst] = e.src\n if k == n-1:\n dist[idst] = -G.T.inf\n negative_cycle = true\n if negative_cycle:\n for k in 0..= 10**18: echo "inf" else: echo ans