結果
問題 | No.2494 Sum within Components |
ユーザー |
![]() |
提出日時 | 2023-10-07 09:02:20 |
言語 | Nim (2.2.0) |
結果 |
AC
|
実行時間 | 93 ms / 2,000 ms |
コード長 | 4,977 bytes |
コンパイル時間 | 3,628 ms |
コンパイル使用メモリ | 74,036 KB |
実行使用メモリ | 15,568 KB |
最終ジャッジ日時 | 2024-07-26 17:44:10 |
合計ジャッジ時間 | 5,046 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 |
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 8) Warning: imported and not used: 'algorithm' [UnusedImport] /home/judge/data/code/Main.nim(6, 11) Warning: imported and not used: 'math' [UnusedImport] /home/judge/data/code/Main.nim(8, 8) Warning: imported and not used: 'strformat' [UnusedImport] /home/judge/data/code/Main.nim(9, 8) Warning: imported and not used: 'strutils' [UnusedImport]
ソースコード
import algorithmimport optionsimport sequtilsimport setsimport std/heapqueueimport std/mathimport std/tablesimport strformatimport strutilsconst letter: string = "abcdefghijklmnopqrstuvwxyz"const Letter: string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"const MAX_INT: int = high(int)const MIN_INT: int = low(int)proc scanf(formatstr: cstring){.header: "<stdio.h>", varargs.}proc getchar(): char {.header: "<stdio.h>", varargs.}proc nextInt(): int = scanf("%lld", addr result)proc nextInts(n: int): seq[int] =var res = newSeq[int](n)for i in 0..<n:res[i] = nextInt()return resproc nextInt64(): int64 = scanf("%lld", addr result)proc nextUInt64(): uint64 = scanf("%lld", addr result)proc nextFloat(): float = scanf("%lf", addr result)proc nextString(): string =var get = falseresult = ""while true:var c = getchar()if int(c) > int(' '):get = trueresult.add(c)else:if get: breakget = falsefunc toInt(b: bool): int =if b:return 1else:return 0func `+`(a: int, b: bool): int = a + (b.toInt)func `+`(a: bool, b: int): int = (a.toInt) + bfunc `-`(a: int, b: bool): int = a - (b.toInt)func `-`(a: bool, b: int): int = (a.toInt) - bfunc `*`(a: int, b: bool): int = a * (b.toInt)func `*`(a: bool, b: int): int = (a.toInt) * bproc `|=`(a: var bool, b: bool) =a = a or bfunc `|`(c1, c2: int): int = c1 or c2proc `&=`(a: var bool, b: bool) =a = a and bfunc `&`(c1, c2: int): int = c1 and c2func `//`(a, b: int): int = a div bproc `//=`(a: var int, b: int) =a = a div bfunc `%`(a: int, b: Positive): int =result = a mod bif result < 0:result += bproc `%=`(a: var int, b: int) =a = a mod bfunc `<<`(a: int, s: int): int = a shl sfunc `>>`(a: int, s: int): int = a shr sproc `>>=`(a: var int, b: int) =a = a shr b# Z/mo Z荳翫・a縺ョ騾・・func inv(a, mo: int): int =var r = (mo, a)var x = (0, 1)while r[1] != 0:x = (x[1], (x[0] - (r[0] div r[1]) * x[1]) % mo)r = (r[1], r[0] % r[1])if r[0] == 1:return x[0]else:return -1func bitLength(n: Natural): int =result = 0var num = abs(n)while num > 0:result += 1num = num shr 1proc chmax[T](a: var T, vars: varargs[T]): bool {.discardable.} =let target = max(vars)if a < target:a = targetreturn trueelse:return falseproc chmin[T](a: var T, vars: varargs[T]): bool {.discardable.} =let target = min(vars)if a > target:a = targetreturn trueelse:return falsetemplate present(a: typed): bool = a.len != 0template empty(a: typed): bool = a.len == 0template rep(idx: untyped, n: SomeOrdinal, act: untyped): untyped =for idx in 0 ..< n:acttemplate loop(body: untyped): untyped =while true:bodyfunc Yn(cond: bool, yes: string = "Yes", no: string = "No"): string =if cond:yeselse:nofunc `<`[T](u, v: openArray[T]): bool =assert u.len == v.lenfor j in 0..<u.len:if u[j] != v[j]:return u[j]-v[j] < 0return true# start coding# DSU {{{const ATCODER_DSU_HPP* = 1import std/sequtilstypeDSU* = ref objectn: intpar_or_siz: seq[int]proc initDSU*(n: int): DSU {.inline.} =return DSU(n: n, par_or_siz: newSeqWith(n, -1))proc leader*(self: DSU; a: int): int {.inline.} =## Path compressionif self.par_or_siz[a] < 0: return aself.par_or_siz[a] = self.leader(self.par_or_siz[a])return self.par_or_siz[a]proc same*(self: DSU; a, b: int): bool {.inline.} =self.leader(a) == self.leader(b)proc size*(self: DSU; a: int): int {.inline.} =- self.par_or_siz[self.leader(a)]proc merge*(self: DSU; a, b: int): int {.inline, discardable.} =varx = self.leader(a)y = self.leader(b)if x == y: return xif self.par_or_siz[x] > self.par_or_siz[y]: swap(x, y)self.par_or_siz[x] += self.par_or_siz[y]self.par_or_siz[y] = xreturn xproc groups*(self: DSU): seq[seq[int]] {.inline.} =varleaderBuf = newSeq[int](self.n)groupsize = newSeq[int](self.n)for i in 0 ..< self.n:leaderBuf[i] = self.leader(i)groupsize[leaderBuf[i]].incresult = (0 ..< self.n).mapIt(newSeqOfCap[int](groupsize[it]))for i, ldr in leaderBuf:result[ldr].add iresult.keepItIf(it.len > 0)# }}}letN = nextInt()M = nextInt()let A = nextInts(N)var dsu = initDSU(N)for _ in 0..<M:letu = nextInt()-1v = nextInt()-1dsu.merge(u, v)var res = 1for gr in dsu.groups():var s = 0for u in gr:s += A[u]s %= 998244353var v = 1for i in 0..<gr.len:v *= sv %= 998244353res *= vres %= 998244353echo res