結果

問題 No.1266 7 Colors
ユーザー marroncastlemarroncastle
提出日時 2020-10-23 23:59:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,206 ms / 3,000 ms
コード長 2,335 bytes
コンパイル時間 1,417 ms
コンパイル使用メモリ 86,808 KB
実行使用メモリ 136,188 KB
最終ジャッジ日時 2023-09-28 19:20:21
合計ジャッジ時間 19,172 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,024 KB
testcase_01 AC 77 ms
71,220 KB
testcase_02 AC 77 ms
71,228 KB
testcase_03 AC 647 ms
90,936 KB
testcase_04 AC 1,043 ms
121,764 KB
testcase_05 AC 676 ms
93,140 KB
testcase_06 AC 1,078 ms
127,712 KB
testcase_07 AC 1,206 ms
134,100 KB
testcase_08 AC 1,059 ms
122,236 KB
testcase_09 AC 992 ms
113,592 KB
testcase_10 AC 928 ms
110,540 KB
testcase_11 AC 802 ms
97,824 KB
testcase_12 AC 884 ms
100,944 KB
testcase_13 AC 915 ms
105,844 KB
testcase_14 AC 711 ms
93,220 KB
testcase_15 AC 1,192 ms
133,996 KB
testcase_16 AC 909 ms
101,856 KB
testcase_17 AC 1,162 ms
131,960 KB
testcase_18 AC 342 ms
136,188 KB
testcase_19 AC 335 ms
133,612 KB
testcase_20 AC 331 ms
133,792 KB
testcase_21 AC 189 ms
80,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
  def __init__(self, n):
    self.n = n
    self.parents = [-1] * n

  def find(self, x):
    if self.parents[x] < 0:
      return x
    else:
      self.parents[x] = self.find(self.parents[x])
    return self.parents[x]

  def union(self, x, y):
    x = self.find(x)
    y = self.find(y)

    if x == y:
      return

    if self.parents[x] > self.parents[y]:
      x, y = y, x

    self.parents[x] += self.parents[y]
    self.parents[y] = x

  def same(self, x, y):
    return self.find(x) == self.find(y)

  def roots(self):
    return [i for i, x in enumerate(self.parents) if x < 0]

  def num_roots(self):
    return len([i for i, x in enumerate(self.parents) if x < 0])

  def members(self, x):
    root = self.find(x)
    return [i for i in range(self.n) if self.find(i) == root]

  def num_members(self,x):
    return abs(self.parents[self.find(x)])

  def __str__(self):
    return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

import sys,os,io
input = sys.stdin.readline
N, M, Q = map(int, input().split())
uf = UnionFind(N*7)
S = [list(input()) for _ in range(N)]
for i in range(N):
  for j in range(7):
    if S[i][j] == '1' and S[i][(j + 1) % 7] == '1':
      uf.union(i * 7 + j, i * 7 + (j + 1) % 7)
      # print(i * 7 + j, i * 7 + (j + 1) % 7)
edge = [[] for _ in range(N)]
for i in range(M):
  x, y = map(int, input().split())
  x -= 1; y -= 1;
  edge[x].append(y)
  edge[y].append(x)
  for j in range(7):
    if S[x][j] == '1' and S[y][j] == '1':
      uf.union(x * 7 + j, y * 7 + j);
      # print(x * 7 + j, y * 7 + j)
for i in range(Q):
  t, a, b = map(int, input().split())
  a -= 1; b -= 1;
  if t == 1:
    S[a][b] = '1'
    if S[a][(b - 1) % 7] == '1':
      uf.union(a * 7 + (b - 1) % 7, a * 7 + b)
      # print(a * 7 + (b - 1) % 7, a * 7 + b)
    if S[a][(b + 1) % 7] == '1':
      uf.union(a * 7 + (b + 1) % 7, a * 7 + b)
      # print(a * 7 + (b + 1) % 7, a * 7 + b)
    for v in edge[a]:
      if S[v][b] == '1':
        uf.union(a * 7 + b, v * 7 + b)
        # print(a * 7 + b, v * 7 + b)
  else:
    print(uf.num_members(a * 7))

# 0 1
# 9 10
# 21 22
# 25 26
# 26 27
# 27 21
# 31 32
# 32 33
# 39 40
# 42 43
# 14 21
# 7 35
# 9 37
# 7 28
# 10 31
# 14 35
# 14 42
# 0 14
# 21 28
# 25 32
# 26 33
# 19
# 17 18
# 19
# 19
# 25 24
# 24 17
# 24 31
# 22 23
# 24 23
# 23
0