結果

問題 No.1266 7 Colors
ユーザー stnkien
提出日時 2020-10-23 23:14:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,003 ms / 3,000 ms
コード長 1,644 bytes
コンパイル時間 761 ms
コンパイル使用メモリ 82,516 KB
実行使用メモリ 124,924 KB
最終ジャッジ日時 2024-07-21 13:06:02
合計ジャッジ時間 15,529 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

import sys
read=sys.stdin.read
readlines=sys.stdin.readlines
input=sys.stdin.readline
N, M, Q = map(int, input().split())
S = [list(map(int, input().rstrip())) for _ in [0]*N]
class UnionFind:
def __init__(self, n=0):
self.d = [-1]*n
self.u = n
def root(self, x):
if self.d[x] < 0:
return x
self.d[x] = self.root(self.d[x])
return self.d[x]
def unite(self, x, y):
x, y = self.root(x), self.root(y)
if x == y:
return False
if x > y:
x, y = y, x
self.d[x] += self.d[y]
self.d[y] = x
self.u -= 1
return True
def same(self, x, y):
return self.root(x) == self.root(y)
def size(self, x):
return -self.d[self.root(x)]
def num_union(self):
return self.u
uf = UnionFind(N*7)
for i in range(N):
for c in range(7):
if S[i][c] and S[i][c-1]:
uf.unite(i*7+c, i*7+(c-1)%7)
G = [[] for _ in range(N)]
for _ in [0]*M:
a, b = map(int, input().split())
a -= 1; b -= 1
G[a].append(b)
G[b].append(a)
for c in range(7):
if S[a][c] == 0 or S[b][c] == 0: continue
uf.unite(a*7+c, b*7+c)
ans = []
for line in readlines():
k, x, c = map(int, line.split())
x -= 1; c -= 1
if k == 1:
S[x][c] = 1
for v in G[x]:
if S[v][c] == 0: continue
uf.unite(x*7+c, v*7+c)
if S[x][(c+1)%7]:
uf.unite(x*7+c, x*7+(c+1)%7)
if S[x][c-1]:
uf.unite(x*7+c, x*7+(c-1)%7)
else:
ans.append(uf.size(x*7))
print('\n'.join(map(str, ans)))
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0