# source: src/cplib/utils/constants.nim when not declared CPLIB_UTILS_CONSTANTS: const CPLIB_UTILS_CONSTANTS* = 1 const INF32*: int32 = 1001000027.int32 const INF64*: int = int(3300300300300300491) # source: src/cplib/tmpl/sheep.nim when not declared CPLIB_TMPL_SHEEP: const CPLIB_TMPL_SHEEP* = 1 {.warning[UnusedImport]: off.} {.hint[XDeclaredButNotUsed]: off.} import algorithm import sequtils import tables import macros import math import sets import strutils import strformat import sugar import heapqueue import streams import deques import bitops import std/lenientops import options #入力系 proc scanf(formatstr: cstring){.header: "", varargs.} proc getchar(): char {.importc: "getchar_unlocked", header: "", discardable.} proc ii(): int {.inline.} = scanf("%lld\n", addr result) proc lii(N: int): seq[int] {.inline.} = newSeqWith(N, ii()) proc si(): string {.inline.} = result = "" var c: char while true: c = getchar() if c == ' ' or c == '\n' or c == '\255': break result &= c #chmin,chmax template `max=`(x, y) = x = max(x, y) template `min=`(x, y) = x = min(x, y) proc chmin[T](x: var T, y: T):bool= if x > y: x = y return true return false proc chmax[T](x: var T, y: T):bool= if x < y: x = y return true return false #bit演算 proc `%`*(x: int, y: int): int = result = x mod y if y > 0 and result < 0: result += y if y < 0 and result > 0: result += y proc `//`*(x: int, y: int): int{.inline.} = result = x div y if y > 0 and result * y > x: result -= 1 if y < 0 and result * y < x: result -= 1 proc `%=`(x: var int, y: int): void = x = x%y proc `//=`(x: var int, y: int): void = x = x//y proc `**`(x: int, y: int): int = x^y proc `**=`(x: var int, y: int): void = x = x^y proc `^`(x: int, y: int): int = x xor y proc `|`(x: int, y: int): int = x or y proc `&`(x: int, y: int): int = x and y proc `>>`(x: int, y: int): int = x shr y proc `<<`(x: int, y: int): int = x shl y proc `~`(x: int): int = not x proc `^=`(x: var int, y: int): void = x = x ^ y proc `&=`(x: var int, y: int): void = x = x & y proc `|=`(x: var int, y: int): void = x = x | y proc `>>=`(x: var int, y: int): void = x = x >> y proc `<<=`(x: var int, y: int): void = x = x << y proc `[]`(x: int, n: int): bool = (x and (1 shl n)) != 0 #便利な変換 proc `!`(x: char, a = '0'): int = int(x)-int(a) #定数 const INF = INF64 #converter #range iterator range(start: int, ends: int, step: int): int = var i = start if step < 0: while i > ends: yield i i += step elif step > 0: while i < ends: yield i i += step iterator range(ends: int): int = (for i in 0.. r[i]: return false elif l[i] < r[i]: return true return len(l) < len(r) # source: src/cplib/graph/graph.nim when not declared CPLIB_GRAPH_GRAPH: const CPLIB_GRAPH_GRAPH* = 1 import sequtils import math type DynamicGraph*[T] = ref object of RootObj edges*: seq[seq[(int32, T)]] len*: int type StaticGraph*[T] = ref object of RootObj src*, dst*: seq[int32] cost*: seq[T] elist*: seq[(int32, T)] start*: seq[int32] len*: int type WeightedDirectedGraph*[T] = ref object of DynamicGraph[T] type WeightedUnDirectedGraph*[T] = ref object of DynamicGraph[T] type UnWeightedDirectedGraph* = ref object of DynamicGraph[int] type UnWeightedUnDirectedGraph* = ref object of DynamicGraph[int] type WeightedDirectedStaticGraph*[T] = ref object of StaticGraph[T] type WeightedUnDirectedStaticGraph*[T] = ref object of StaticGraph[T] type UnWeightedDirectedStaticGraph* = ref object of StaticGraph[int] type UnWeightedUnDirectedStaticGraph* = ref object of StaticGraph[int] type GraphTypes*[T] = DynamicGraph[T] or StaticGraph[T] type DirectedGraph* = WeightedDirectedGraph or UnWeightedDirectedGraph or WeightedDirectedStaticGraph or UnWeightedDirectedStaticGraph type UnDirectedGraph* = WeightedUnDirectedGraph or UnWeightedUnDirectedGraph or WeightedUnDirectedStaticGraph or UnWeightedUnDirectedStaticGraph type WeightedGraph*[T] = WeightedDirectedGraph[T] or WeightedUnDirectedGraph[T] or WeightedDirectedStaticGraph[T] or WeightedUnDirectedStaticGraph[T] type UnWeightedGraph* = UnWeightedDirectedGraph or UnWeightedUnDirectedGraph or UnWeightedDirectedStaticGraph or UnWeightedUnDirectedStaticGraph type DynamicGraphTypes* = WeightedDirectedGraph or UnWeightedDirectedGraph or WeightedUnDirectedGraph or UnWeightedUnDirectedGraph type StaticGraphTypes* = WeightedDirectedStaticGraph or UnWeightedDirectedStaticGraph or WeightedUnDirectedStaticGraph or UnWeightedUnDirectedStaticGraph proc add_edge_dynamic_impl*[T](g: DynamicGraph[T], u, v: int, cost: T, directed: bool) = g.edges[u].add((v.int32, cost)) if not directed: g.edges[v].add((u.int32, cost)) proc initWeightedDirectedGraph*(N: int, edgetype: typedesc = int): WeightedDirectedGraph[edgetype] = result = WeightedDirectedGraph[edgetype](edges: newSeq[seq[(int32, edgetype)]](N), len: N) proc add_edge*[T](g: var WeightedDirectedGraph[T], u, v: int, cost: T) = g.add_edge_dynamic_impl(u, v, cost, true) proc initWeightedUnDirectedGraph*(N: int, edgetype: typedesc = int): WeightedUnDirectedGraph[edgetype] = result = WeightedUnDirectedGraph[edgetype](edges: newSeq[seq[(int32, edgetype)]](N), len: N) proc add_edge*[T](g: var WeightedUnDirectedGraph[T], u, v: int, cost: T) = g.add_edge_dynamic_impl(u, v, cost, false) proc initUnWeightedDirectedGraph*(N: int): UnWeightedDirectedGraph = result = UnWeightedDirectedGraph(edges: newSeq[seq[(int32, int)]](N), len: N) proc add_edge*(g: var UnWeightedDirectedGraph, u, v: int) = g.add_edge_dynamic_impl(u, v, 1, true) proc initUnWeightedUnDirectedGraph*(N: int): UnWeightedUnDirectedGraph = result = UnWeightedUnDirectedGraph(edges: newSeq[seq[(int32, int)]](N), len: N) proc add_edge*(g: var UnWeightedUnDirectedGraph, u, v: int) = g.add_edge_dynamic_impl(u, v, 1, false) proc len*[T](G: WeightedGraph[T]): int = G.len proc len*(G: UnWeightedGraph): int = G.len iterator `[]`*[T](g: WeightedDirectedGraph[T] or WeightedUnDirectedGraph[T], x: int): (int, T) = for e in g.edges[x]: yield (e[0].int, e[1]) iterator `[]`*(g: UnWeightedDirectedGraph or UnWeightedUnDirectedGraph, x: int): int = for e in g.edges[x]: yield e[0].int proc add_edge_static_impl*[T](g: StaticGraph[T], u, v: int, cost: T, directed: bool) = g.src.add(u.int32) g.dst.add(v.int32) g.cost.add(cost) if not directed: g.src.add(v.int32) g.dst.add(u.int32) g.cost.add(cost) proc build_impl*[T](g: StaticGraph[T]) = g.start = newSeqWith(g.len + 1, 0.int32) for i in 0.. 0, "Static Graph must be initialized before use." iterator `[]`*[T](g: WeightedDirectedStaticGraph[T] or WeightedUnDirectedStaticGraph[T], x: int): (int, T) = g.static_graph_initialized_check() for i in g.start[x].. hld.PD[v]: u = hld.P[hld.PP[u]] while hld.PP[u] != hld.PP[v]: u = hld.P[hld.PP[u]] v = hld.P[hld.PP[v]] if hld.D[u] > hld.D[v]: return v u proc dist*(hld: HeavyLightDecomposition, u: int, v: int): int = hld.depth(u) + hld.depth(v) - hld.depth(hld.lca(u, v)) * 2 proc path*(hld: HeavyLightDecomposition, r: int, c: int, include_root: bool, reverse_path: bool): seq[(int, int)] = var (r, c) = (r, c) var k = hld.PD[c] - hld.PD[r] + 1 if k <= 0: return @[] var res = newSeqWith(k, (0, 0)) for i in 0.. hld.D[c]: return @[] var root_off = int(not include_root) res[^1] = (hld.rangeL[r]+root_off, hld.rangeL[c]+1) if res[^1][0] == res[^1][1]: discard res.pop() k -= 1 if reverse_path: for i in 0.. 0 and hld.toSeq2Out(stack[^1]) < hld.toseq2In(v[i]): discard stack.pop() if len(stack) != 0: result.add_edge(stack[^1],v[i]) stack.add(v[i]) proc initAuxiliaryWeightedTree*(hld:HeavyLightDecomposition,v:seq[int]):WeightedUnDirectedTableGraph[int,int]= var v = v.sortedByit(hld.toseq(it)) for i in 0..<(len(v)-1): v.add(hld.lca(v[i],v[i+1])) v = v.sortedByIt(hld.toseq(it)).deduplicate(true) var stack :seq[int] result = initWeightedUnDirectedTableGraph(v,int) stack.add(v[0]) for i in 1.. 0 and hld.toSeq2Out(stack[^1]) < hld.toseq2In(v[i]): discard stack.pop() if len(stack) != 0: result.add_edge(stack[^1],v[i],hld.depth(v[i])-hld.depth(stack[^1])) stack.add(v[i]) # source: src/atcoder/internal_bit.nim when not declared ATCODER_INTERNAL_BITOP_HPP: const ATCODER_INTERNAL_BITOP_HPP* = 1 import std/bitops #ifdef _MSC_VER #include #endif # @param n `0 <= n` # @return minimum non-negative `x` s.t. `n <= 2**x` proc ceil_pow2*(n:SomeInteger):int = var x = 0 while (1.uint shl x) < n.uint: x.inc return x # @param n `1 <= n` # @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0` proc bsf*(n:SomeInteger):int = return countTrailingZeroBits(n) # source: src/atcoder/rangeutils.nim when not declared ATCODER_RANGEUTILS_HPP: const ATCODER_RANGEUTILS_HPP* = 1 type RangeType* = Slice[int] | HSlice[int, BackwardsIndex] | Slice[BackwardsIndex] type IndexType* = int | BackwardsIndex template halfOpenEndpoints*(p:Slice[int]):(int,int) = (p.a, p.b + 1) template `^^`*(s, i: untyped): untyped = (when i is BackwardsIndex: s.len - int(i) else: int(i)) template halfOpenEndpoints*[T](s:T, p:RangeType):(int,int) = (s^^p.a, s^^p.b + 1) # source: src/atcoder/lazysegtree.nim when not declared ATCODER_LAZYSEGTREE_HPP: const ATCODER_LAZYSEGTREE_HPP* = 1 import std/sequtils import std/algorithm {.push inline.} type LazySegTree*[S,F;p:static[tuple]] = object len*, size*, log*:int d*:seq[S] lz*:seq[F] template calc_op*[ST:LazySegTree](self:ST or typedesc[ST], a, b:ST.S):auto = block: let u = ST.p.op(a, b) u template calc_e*[ST:LazySegTree](self:ST or typedesc[ST]):auto = block: let u = ST.p.e() u template calc_mapping*[ST:LazySegTree](self:ST or typedesc[ST], a:ST.F, b:ST.S):auto = block: let u = ST.p.mapping(a, b) u template calc_composition*[ST:LazySegTree](self:ST or typedesc[ST], a, b:ST.F):auto = block: # こう書かないとバグる事象を検出 let u = ST.p.composition(a, b) u template calc_id*[ST:LazySegTree](self:ST or typedesc[ST]):auto = block: let u = ST.p.id() u proc update[ST:LazySegTree](self:var ST, k:int) = self.d[k] = ST.calc_op(self.d[2 * k], self.d[2 * k + 1]) proc all_apply*[ST:LazySegTree](self:var ST, k:int, f:ST.F) = self.d[k] = ST.calc_mapping(f, self.d[k]) if k < self.size: self.lz[k] = ST.calc_composition(f, self.lz[k]) proc all_apply*[ST:LazySegTree](self:var ST, f:ST.F) = self.all_apply(1, f) proc push*[ST:LazySegTree](self: var ST, k:int) = self.all_apply(2 * k, self.lz[k]) self.all_apply(2 * k + 1, self.lz[k]) self.lz[k] = ST.calc_id() proc init[ST:LazySegTree](self:var ST, v:seq[ST.S]) = let n = v.len log = ceil_pow2(n) size = 1 shl log (self.len, self.size, self.log) = (n, size, log) if self.d.len < 2 * size: self.d = newSeqWith(2 * size, ST.calc_e()) else: self.d.fill(0, 2 * size - 1, ST.calc_e()) for i in 0.. int max_right(int l) { # return max_right(l, [](S x) { return g(x); }); # } proc max_right*[ST:LazySegTree](self:var ST, l:IndexType, g:proc(s:ST.S):bool):int = var l = self^^l assert l in 0..self.len assert g(ST.calc_e()) if l == self.len: return self.len l += self.size for i in countdown(self.log, 1): self.push(l shr i) var sm = ST.calc_e() while true: while l mod 2 == 0: l = l shr 1 if not g(ST.calc_op(sm, self.d[l])): while l < self.size: self.push(l) l = (2 * l) if g(ST.calc_op(sm, self.d[l])): sm = ST.calc_op(sm, self.d[l]) l.inc return l - self.size sm = ST.calc_op(sm, self.d[l]) l.inc if not((l and -l) != l): break return self.len # template int min_left(int r) { # return min_left(r, [](S x) { return g(x); }); # } proc min_left*[ST:LazySegTree](self: var ST, r:IndexType, g:proc(s:ST.S):bool):int = var r = self^^r assert r in 0..self.len assert(g(ST.calc_e())) if r == 0: return 0 r += self.size for i in countdown(self.log, 1): self.push((r - 1) shr i) var sm = ST.calc_e() while true: r.dec while r > 1 and r mod 2 == 1: r = r shr 1 if not g(ST.calc_op(self.d[r], sm)): while r < self.size: self.push(r) r = (2 * r + 1) if g(ST.calc_op(self.d[r], sm)): sm = ST.calc_op(self.d[r], sm) r.dec return r + 1 - self.size sm = ST.calc_op(self.d[r], sm) if not ((r and -r) != r): break return 0 {.pop.} # source: src/cplib/utils/binary_search.nim when not declared CPLIB_UTILS_BINARY_SEARCH: const CPLIB_UTILS_BINARY_SEARCH* = 1 proc meguru_bisect*(ok, ng: int, is_ok: proc(x: int): bool): int = var ok = ok ng = ng while abs(ok - ng) > 1: var mid = (ok + ng) div 2 if is_ok(mid): ok = mid else: ng = mid return ok proc meguru_bisect*(ok, ng: SomeFloat, is_ok: proc(x: SomeFloat): bool, eps: SomeFloat = 1e-10): SomeFloat = var ok = ok ng = ng while abs(ok - ng) > eps and abs(ok - ng) / max(ok, ng) > eps: var mid = (ok + ng) / 2 if is_ok(mid): ok = mid else: ng = mid return ok # source: src/cplib/collections/segtree.nim when not declared CPLIB_COLLECTIONS_SEGTREE: const CPLIB_COLLECTIONS_SEGTREE* = 1 import algorithm import strutils import sequtils type SegmentTree*[T] = ref object default: T merge: proc(x: T, y: T): T arr*: seq[T] lastnode: int length: int proc initSegmentTree*[T](v: seq[T], merge: proc(x: T, y: T): T, default: T): SegmentTree[T] = var lastnode = 1 while lastnode < len(v): lastnode*=2 var arr = newSeq[T](2*lastnode) arr.fill(default) var self = SegmentTree[T](default: default, merge: merge, arr: arr, lastnode: lastnode, length: len(v)) #1-indexedで作成する for i in 0.. 1: x = x shr 1 self.arr[x] = self.merge(self.arr[2*x], self.arr[2*x+1]) proc get*[T](self: SegmentTree[T], q_left: Natural, q_right: Natural): T = assert q_left <= q_right and 0 <= q_left and q_right <= self.length var q_left = q_left var q_right = q_right q_left += self.lastnode q_right += self.lastnode var (lres, rres) = (self.default, self.default) while q_left < q_right: if (q_left and 1) > 0: lres = self.merge(lres, self.arr[q_left]) q_left += 1 if (q_right and 1) > 0: q_right -= 1 rres = self.merge(self.arr[q_right], rres) q_left = q_left shr 1 q_right = q_right shr 1 return self.merge(lres, rres) proc get*[T](self: SegmentTree[T], segment: HSlice[int, int]): T = assert segment.a <= segment.b + 1 and 0 <= segment.a and segment.b+1 <= self.length return self.get(segment.a, segment.b+1) proc `[]`*[T](self: SegmentTree[T], segment: HSlice[int, int]): T = self.get(segment) proc `[]`*[T](self: SegmentTree[T], index: Natural): T = assert index < self.length return self.arr[index+self.lastnode] proc `[]=`*[T](self: SegmentTree[T], index: Natural, val: T) = assert index < self.length self.update(index, val) proc get_all*[T](self: SegmentTree[T]): T = return self.arr[1] proc len*[T](self: SegmentTree[T]): int = return self.length proc `$`*[T](self: SegmentTree[T]): string = var s = self.arr.len div 2 return self.arr[s.. 1) and (r mod 2 != 0)): r = (r shr 1) if not f(self.merge(self.arr[r], sm)): while r < self.lastnode: r = 2 * r + 1 if f(self.merge(self.arr[r], sm)): sm = self.merge(self.arr[r], sm) r -= 1 return r + 1 - self.lastnode sm = self.merge(self.arr[r], sm) if (r and -r) == r: break return 0 proc initRangeAddRangeMinSegtree[T](v:seq[T]):auto= type S = (T,int) type F = T proc op(a,b:S):S= if a[0] == b[0]: return (a[0],a[1]+b[1]) elif a[0] < b[0]: return a else: return b proc e():S=(INF,1) proc mapping(f:F,x:S):S=(x[0]+f,x[1]) proc composition(f,g:F):F=f+g proc id():F=0 return LazySegTree.getType(S, F, op, e, mapping, composition, id).init(v.mapit((it,1))) var N = ii() var G = initUnWeightedUnDirectedGraph(N) # クエリ1 : 色反転 # クエリ2 : yを根としたときに、xが含まれないような部分木に含まれる黒色の頂点の数を出力 # クエリ2はyを根としたときにxが含まれるような部分木に含まれる黒色の頂点の数と解釈可能 # x = y : すべての黒色頂点 # yが黒 : それも含む # 部分木クエリといえばオイラーツアーだが... # range add range 最小値カウント # -> 遅延セグ木に乗る # hldするなどの行為により部分木クエリは可能になった。 # xがyの子孫にあるとき : yについて部分木クエリ - yからx方向に1進んだ頂点から部分木クエリ + 親方向 # 親方向ってどうやってやるんだ??????? # 親方向ではじめて別の部分木に頂点があるところを見つけて、そこの部分木クエリから - yからx方向に1進んだ頂点から部分木クエリ # yがxの子孫にあるとき : yについて部分木クエリ for _ in range(N-1): var a,b = ii()-1 G.add_edge(a,b) var tmp = lii(N) var C = newsegwith(N,l+r,0) var T = G.initHld(0) for i in range(N): C[T.toseq(i)] = tmp[i] var st = initRangeAddRangeMinSegtree(newseqwith(N,0)) for i in range(N): if C[T.toseq(i)] == 1: for (l,r) in T.path(0,i,true,false): st.apply(l..