結果
| 問題 |
No.1266 7 Colors
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-10-24 00:30:20 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 2,041 ms / 3,000 ms |
| コード長 | 1,718 bytes |
| コンパイル時間 | 136 ms |
| コンパイル使用メモリ | 82,540 KB |
| 実行使用メモリ | 136,772 KB |
| 最終ジャッジ日時 | 2024-07-21 14:21:56 |
| 合計ジャッジ時間 | 24,417 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
class UnionFind():
def __init__(self,n):
self.n=n
self.root=[-1]*(n+1)
self.rank=[0]*(n+1)
def FindRoot(self,x):
if self.root[x]<0:
return x
else:
self.root[x]=self.FindRoot(self.root[x])
return self.root[x]
def Unite(self,x,y):
x=self.FindRoot(x)
y=self.FindRoot(y)
if x==y:
return
else:
if self.rank[x]>self.rank[y]:
self.root[x]+=self.root[y]
self.root[y]=x
elif self.rank[x]<=self.rank[y]:
self.root[y]+=self.root[x]
self.root[x]=y
if self.rank[x]==self.rank[y]:
self.rank[y]+=1
def isSameGroup(self,x,y):
return self.FindRoot(x)==self.FindRoot(y)
def Count(self,x):
return -self.root[self.FindRoot(x)]
n,m,q=map(int,input().split())
colors=[list(input()) for _ in range(n)]
g=[[] for _ in range(n)]
for _ in range(m):
u,v=map(int,input().split())
u-=1
v-=1
g[u].append(v)
g[v].append(u)
uf=UnionFind(7*n)
for v in range(n):
for c in range(7):
if colors[v][c]=='1' and colors[v][(c+1)%7]=='1':
uf.Unite(v+c*n,v+((c+1)%7)*n)
for c in range(7):
for v in range(n):
for u in g[v]:
if colors[u][c]=='1' and colors[v][c]=='1':
uf.Unite(v+n*c,u+n*c)
for _ in range(q):
f,x,y=map(int,input().split())
x-=1
y-=1
if f==1:
colors[x][y]='1'
for c in range(7):
if colors[x][c]=='1' and colors[x][(c+1)%7]=='1':
uf.Unite(x+c*n,x+((c+1)%7)*n)
for c in range(7):
for u in g[x]:
if colors[x][c]=='1' and colors[u][c]=='1':
uf.Unite(x+n*c,u+n*c)
elif f==2:
print(uf.Count(x))