結果

問題 No.1266 7 Colors
ユーザー ayaoniayaoni
提出日時 2020-10-24 00:23:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,429 ms / 3,000 ms
コード長 2,239 bytes
コンパイル時間 1,125 ms
コンパイル使用メモリ 86,916 KB
実行使用メモリ 131,584 KB
最終ジャッジ日時 2023-09-28 19:35:15
合計ジャッジ時間 19,074 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,372 KB
testcase_01 AC 75 ms
71,272 KB
testcase_02 AC 78 ms
70,988 KB
testcase_03 AC 662 ms
90,012 KB
testcase_04 AC 1,178 ms
116,792 KB
testcase_05 AC 652 ms
93,344 KB
testcase_06 AC 1,278 ms
122,956 KB
testcase_07 AC 1,399 ms
129,260 KB
testcase_08 AC 1,184 ms
118,320 KB
testcase_09 AC 1,000 ms
109,916 KB
testcase_10 AC 977 ms
106,420 KB
testcase_11 AC 846 ms
97,276 KB
testcase_12 AC 844 ms
100,828 KB
testcase_13 AC 905 ms
103,264 KB
testcase_14 AC 618 ms
91,976 KB
testcase_15 AC 1,429 ms
131,584 KB
testcase_16 AC 873 ms
100,128 KB
testcase_17 AC 1,387 ms
128,340 KB
testcase_18 AC 408 ms
128,044 KB
testcase_19 AC 371 ms
125,160 KB
testcase_20 AC 374 ms
125,604 KB
testcase_21 AC 201 ms
81,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**7)
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))


class UnionFind:
    def __init__(self,n):
        self.par = [i for i in range(n+1)]  # 親のノード番号
        self.rank = [0]*(n+1)
        self.X = [1]*(n+1)

    def find(self,x):  # xの根のノード番号
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def same_check(self,x,y):  # x,yが同じグループか否か
        return self.find(x) == self.find(y)

    def unite(self,x,y):  # x,yの属するグループの併合
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            x,y = y,x
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1
        self.par[y] = x
        self.X[x] += self.X[y]


N,M,Q = MI()
A = [[]]
for i in range(N):
    A.append([0]+LI2())

Graph = [[] for _ in range(N+1)]
for _ in range(M):
    u,v = MI()
    Graph[u].append(v)
    Graph[v].append(u)

# 都市i,色jを 7*(i-1)+j に対応させる

UF = UnionFind(7*N)
for i in range(1,N+1):
    for j in range(1,8):
        w = 7*(i-1)+j
        if A[i][j] == 1:
            for k in Graph[i]:
                if A[k][j] == 1:
                    UF.unite(w,7*(k-1)+j)

for i in range(1,N+1):
    for j in range(1,7):
        if A[i][j] == A[i][j+1] == 1:
            UF.unite(7*(i-1)+j,7*(i-1)+j+1)
    if A[i][1] == A[i][7] == 1:
        UF.unite(7*(i-1)+1,7*(i-1)+7)

for _ in range(Q):
    r,x,y = MI()
    if r == 1:
        A[x][y] = 1
        w = 7*(x-1)+y
        if y > 1:
            if A[x][y-1] == 1:
                UF.unite(w,7*(x-1)+y-1)
        if y < 7:
            if A[x][y+1] == 1:
                UF.unite(w,7*(x-1)+y+1)            
        if y == 1:
            if A[x][7] == 1:
                UF.unite(w,7*(x-1)+7)
        if y == 7:
            if A[x][1] == 1:
                UF.unite(w,7*(x-1)+1)
        for k in Graph[x]:
            if A[k][y] == 1:
                UF.unite(w,7*(k-1)+y)
    else:
        print(UF.X[UF.find(7*(x-1)+1)])
0