結果

問題 No.1266 7 Colors
ユーザー tanon710tanon710
提出日時 2020-10-24 00:30:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,237 ms / 3,000 ms
コード長 1,718 bytes
コンパイル時間 341 ms
コンパイル使用メモリ 87,000 KB
実行使用メモリ 139,592 KB
最終ジャッジ日時 2023-09-28 19:40:53
合計ジャッジ時間 28,748 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,392 KB
testcase_01 AC 76 ms
71,144 KB
testcase_02 AC 75 ms
71,164 KB
testcase_03 AC 1,074 ms
91,444 KB
testcase_04 AC 1,816 ms
126,152 KB
testcase_05 AC 1,119 ms
94,992 KB
testcase_06 AC 1,884 ms
129,500 KB
testcase_07 AC 2,162 ms
139,180 KB
testcase_08 AC 1,794 ms
127,080 KB
testcase_09 AC 1,588 ms
115,816 KB
testcase_10 AC 1,573 ms
112,612 KB
testcase_11 AC 1,230 ms
100,504 KB
testcase_12 AC 1,450 ms
103,272 KB
testcase_13 AC 1,452 ms
106,612 KB
testcase_14 AC 1,179 ms
94,516 KB
testcase_15 AC 2,237 ms
137,548 KB
testcase_16 AC 1,330 ms
103,772 KB
testcase_17 AC 1,864 ms
133,288 KB
testcase_18 AC 747 ms
139,592 KB
testcase_19 AC 566 ms
136,176 KB
testcase_20 AC 572 ms
136,056 KB
testcase_21 AC 463 ms
81,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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))
0