結果

問題 No.1266 7 Colors
ユーザー dot_haraaidot_haraai
提出日時 2021-06-04 12:16:29
言語 Nim
(2.0.2)
結果
WA  
実行時間 -
コード長 3,055 bytes
コンパイル時間 4,607 ms
コンパイル使用メモリ 88,004 KB
実行使用メモリ 22,784 KB
最終ジャッジ日時 2024-11-18 18:55:25
合計ジャッジ時間 10,225 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 RE -
testcase_04 WA -
testcase_05 RE -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 277 ms
22,784 KB
testcase_19 AC 173 ms
21,888 KB
testcase_20 AC 193 ms
21,888 KB
testcase_21 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(2, 18) Warning: Use the new 'sugar' module instead; future is deprecated [Deprecated]
/home/judge/data/code/Main.nim(1, 8) Warning: imported and not used: 'times' [UnusedImport]
/home/judge/data/code/Main.nim(2, 26) Warning: imported and not used: 'strformat' [UnusedImport]
/home/judge/data/code/Main.nim(2, 18) Warning: imported and not used: 'future' [UnusedImport]
/home/judge/data/code/Main.nim(2, 37) Warning: imported and not used: 'deques' [UnusedImport]
/home/judge/data/code/Main.nim(1, 41) Warning: imported and not used: 'algorithm' [UnusedImport]
/home/judge/data/code/Main.nim(1, 52) Warning: imported and not used: 'tables' [UnusedImport]
/home/judge/data/code/Main.nim(1, 60) Warning: imported and not used: 'sets' [UnusedImport]
/home/judge/data/code/Main.nim(1, 66) Warning: imported and not used: 'lists' [UnusedImport]
/home/judge/data/code/Main.nim(1, 73) Warning: imported and not used: 'intsets' [UnusedImport]
/home/judge/data/code/Main.nim(2, 8) Warning: imported and not used: 'critbits' [UnusedImport]

ソースコード

diff #

import times, strutils, sequtils, math, algorithm, tables, sets, lists, intsets
import critbits, future, strformat, deques
template `max=`(x,y) = x = max(x,y)
template `min=`(x,y) = x = min(x,y)
template `mod=`(x,y) = x = x mod y
template scan2 = (scan(), scan())
template scan3 = (scan(), scan())
let read* = iterator: string {.closure.} =
    while true: (for s in stdin.readLine.split: yield s)
proc scan(): int = read().parseInt
proc scanf(): float = read().parseFloat
proc toInt(c:char): int =
    return int(c) - int('0')




proc solve()=
  var
    n = scan()
    m = scan()
    query = scan()
    s = newseqwith(n,read().parseBinInt)
    nodeNum = n*7
    baseEs = newseqwith(n,newseq[int]())
    roots = newseqwith(nodeNum,0)
    size = newseqwith(nodeNum,1)
  for i in 0..<m:
    var
      (u,v) = (scan()-1,scan()-1)
    baseEs[u].add(v)
    baseEs[v].add(u)
  # 都市u,vが同じ色jを持っていたらu*7+j,v*7+j間でunite
  var
    queries = newseqwith(query,(scan(),scan()-1,scan()))

  # i番目の都市の色jの仮想都市のインデックス:i*7+j
  for i in 0..<nodeNum:
    roots[i] = i

  proc find(x:int):int=
    if roots[x] == x:
      return x
    else:
      result = find(roots[x])
      roots[x] = result


  proc unite(x,y:int)=
    #echo "unite ", x, ", ", y
    var
      rx = find(x)
      ry = find(y)
    if rx == ry:
      return
    elif rx<ry:
      (size[rx],size[ry]) = (size[rx]+size[ry],0)
      roots[ry]=rx
      return 
    else:
      (size[rx],size[ry]) = (0,size[rx]+size[ry])
      roots[rx]=ry
      return 

  # もともとの辺を張っていく
  # 1. 同一都市内での辺
  for idx,colors  in s:
    for j in 0..<7:
      var
        left = 7-j-1
        right = ((7-j-2).mod(7)+7).mod(7)
      # 色j,j+1の間に辺が張れる
      if ((colors and (1 shl left)) > 0) and ((colors and (1 shl right)) > 0):
        unite(idx*7+j,idx*7+(j+1).mod(7))
  # 2. 隣接都市が同じ色を持っていたら辺を張る
  for town in 0..<n:
    for nxt in baseEs[town]:
      var
        townC = s[town]
        nxtC = s[nxt]
      for j in 0..<6:
        if (townC and (1 shl (6-j)))>0 and (nxtC and (1 shl (6-j)))>0:
          unite(town*7+j,nxt*7+j)
  #echo roots
  for (q,x,y) in queries:
    if q == 1:
      # 1.自分自身の上下の色とのunite
      var
        p = (7-y)
        upper = ((7-(y-1)-1).mod(7)+7).mod(7)
        lower = ((7-(y+1)-1).mod(7)+7).mod(7)
      #echo s[x].toBin(7)
      #echo p, ", ", lower, ", ", upper
      s[x] += (1 shl p)
      if ((s[x] and (1 shl upper)) > 0) and ((s[x] and (1 shl p)) > 0):
        unite(x*7+y,x*7+((y-1).mod(7)+7).mod(7))
      if ((s[x] and (1 shl lower)) > 0) and ((s[x] and (1 shl p)) > 0):
        unite(x*7+y,x*7+((y+1).mod(7)+7).mod(7))
      # 2. 隣接する都市の同一の色とのunite
      for nxt in baseEs[x]:
        if (s[nxt] and (1 shl p)) > 0:
          unite(x*7+y,nxt*7+y)
    elif q == 2:
      #echo "x: ",x
      #echo roots
      echo size[find(x*7)]
  #echo roots
  #echo size


  
  



solve()
0