結果

問題 No.1266 7 Colors
ユーザー dot_haraaidot_haraai
提出日時 2021-06-04 12:51:39
言語 Nim
(2.0.2)
結果
AC  
実行時間 304 ms / 3,000 ms
コード長 3,101 bytes
コンパイル時間 4,578 ms
コンパイル使用メモリ 87,424 KB
実行使用メモリ 33,280 KB
最終ジャッジ日時 2024-04-29 15:33:33
合計ジャッジ時間 10,030 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 219 ms
11,520 KB
testcase_04 AC 261 ms
25,344 KB
testcase_05 AC 232 ms
13,012 KB
testcase_06 AC 263 ms
27,520 KB
testcase_07 AC 279 ms
30,464 KB
testcase_08 AC 260 ms
25,984 KB
testcase_09 AC 256 ms
21,376 KB
testcase_10 AC 243 ms
19,712 KB
testcase_11 AC 233 ms
14,720 KB
testcase_12 AC 231 ms
16,396 KB
testcase_13 AC 231 ms
17,920 KB
testcase_14 AC 220 ms
12,928 KB
testcase_15 AC 285 ms
31,272 KB
testcase_16 AC 228 ms
16,768 KB
testcase_17 AC 281 ms
29,920 KB
testcase_18 AC 304 ms
33,280 KB
testcase_19 AC 187 ms
32,768 KB
testcase_20 AC 203 ms
32,896 KB
testcase_21 AC 210 ms
10,368 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/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]())
    es = newseqwith(nodeNum,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()-1))

  # 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-1)
        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