結果

問題 No.1266 7 Colors
ユーザー oevloevl
提出日時 2020-10-27 13:58:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,422 ms / 3,000 ms
コード長 1,680 bytes
コンパイル時間 647 ms
コンパイル使用メモリ 87,068 KB
実行使用メモリ 117,444 KB
最終ジャッジ日時 2023-09-29 03:17:04
合計ジャッジ時間 21,991 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,260 KB
testcase_01 AC 75 ms
71,592 KB
testcase_02 AC 76 ms
71,332 KB
testcase_03 AC 885 ms
90,988 KB
testcase_04 AC 1,280 ms
109,496 KB
testcase_05 AC 900 ms
94,164 KB
testcase_06 AC 1,309 ms
112,168 KB
testcase_07 AC 1,422 ms
115,096 KB
testcase_08 AC 1,284 ms
108,932 KB
testcase_09 AC 1,158 ms
104,568 KB
testcase_10 AC 1,147 ms
102,744 KB
testcase_11 AC 938 ms
93,992 KB
testcase_12 AC 1,047 ms
97,928 KB
testcase_13 AC 1,105 ms
98,556 KB
testcase_14 AC 913 ms
92,960 KB
testcase_15 AC 1,343 ms
117,444 KB
testcase_16 AC 1,044 ms
98,036 KB
testcase_17 AC 1,334 ms
115,752 KB
testcase_18 AC 551 ms
114,544 KB
testcase_19 AC 409 ms
111,732 KB
testcase_20 AC 405 ms
112,076 KB
testcase_21 AC 443 ms
82,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Unionfind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def leader (self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.leader(self.parents[x])
            return self.parents[x]

    def merge(self, x, y):
        x = self.leader(x)
        y = self.leader(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def same(self, x):
        return self.leader(x) == self.leader(y)

    def size(self, x):
        return -self.parents[self.leader(x)]

n, m, q = map(int, input().split())
s = [input() for _ in range(n)]

uf = Unionfind(7 * n)
graph = [[] for _ in range(n)]
exist = [[s[i][j] == '1' for j in range(7)] for i in range(n)]

for i in range(n):
    for j in range(7):
        if exist[i][j] and exist[i][(j + 1) % 7]:
            uf.merge(7 * i + j, 7 * i + (j + 1) % 7)

for i in range(m):
    u, v = map(lambda x: int(x) - 1, input().split())
    for j in range(7):
        if exist[u][j] and exist[v][j]:
            uf.merge(7 * u + j, 7 * v + j)
    graph[u].append(v)
    graph[v].append(u)

for _ in range(q):
    t, x, y = map(lambda x: int(x) - 1, input().split())
    if t == 0:
        exist[x][y] = True
        if exist[x][(y + 6) % 7]:
            uf.merge(7 * x + (y + 6) % 7, 7 * x + y)
        if exist[x][(y + 8) % 7]:
            uf.merge(7 * x + y, 7 * x + (y + 8) % 7)
        for z in graph[x]:
            if exist[z][y]:
                uf.merge(7 * x + y, 7 * z + y)
    else:
        print(uf.size(7 * x))
0