結果
| 問題 |
No.1266 7 Colors
|
| コンテスト | |
| ユーザー |
tyawanmusi
|
| 提出日時 | 2020-10-23 22:15:22 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 1,175 ms / 3,000 ms |
| コード長 | 1,730 bytes |
| コンパイル時間 | 196 ms |
| コンパイル使用メモリ | 82,304 KB |
| 実行使用メモリ | 113,836 KB |
| 最終ジャッジ日時 | 2024-07-21 10:51:25 |
| 合計ジャッジ時間 | 15,962 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
import sys
input = lambda:sys.stdin.readline().rstrip()
class UnionFind:
def __init__(self, n):
self.n = [-1]*n
self.r = [0]*n
self.siz = n
def find_root(self, x):
if self.n[x] < 0:
return x
else:
self.n[x] = self.find_root(self.n[x])
return self.n[x]
def unite(self, x, y):
x = self.find_root(x)
y = self.find_root(y)
if x == y:
return
elif self.r[x] > self.r[y]:
self.n[x] += self.n[y]
self.n[y] = x
else:
self.n[y] += self.n[x]
self.n[x] = y
if self.r[x] == self.r[y]:
self.r[y] += 1
self.siz -= 1
def root_same(self, x, y):
return self.find_root(x) == self.find_root(y)
def count(self, x):
return -self.n[self.find_root(x)]
def size(self):
return self.siz
n,m,q=map(int,input().split())
color=[[0]*7 for _ in range(n)]
for i in range(n):
s=input()
for j in range(7):
if s[j]=="1":
color[i][j]=1
uf=UnionFind(n*7)
edge=[[]for _ in range(n)]
for _ in range(m):
u,v=map(int,input().split())
u-=1
v-=1
edge[u].append(v)
edge[v].append(u)
f=lambda x,c: x*7+c
for i in range(n):
for j in range(7):
if color[i][j]==color[i][(j+1)%7]==1:
uf.unite(f(i,j),f(i,(j+1)%7))
for j in edge[i]:
for k in range(7):
if color[i][k]==color[j][k]==1:
uf.unite(f(i,k),f(j,k))
for _ in range(q):
qq=list(map(int,input().split()))
if qq[0]==1:
x,y=qq[1:]
x-=1
y-=1
color[x][y]=1
if color[x][(y-1)%7]==1:
uf.unite(f(x,y),f(x,(y-1)%7))
if color[x][(y+1)%7]==1:
uf.unite(f(x,y),f(x,(y+1)%7))
for i in edge[x]:
if color[i][y]==1:
uf.unite(f(x,y),f(i,y))
else:
x=qq[1]-1
print(uf.count(f(x,0)))
tyawanmusi