結果

問題 No.1266 7 Colors
ユーザー brthyyjpbrthyyjp
提出日時 2020-10-23 23:28:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,182 ms / 3,000 ms
コード長 2,513 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 87,300 KB
実行使用メモリ 175,024 KB
最終ジャッジ日時 2023-09-28 18:45:09
合計ジャッジ時間 25,610 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,200 KB
testcase_01 AC 73 ms
71,016 KB
testcase_02 AC 74 ms
71,256 KB
testcase_03 AC 776 ms
96,184 KB
testcase_04 AC 1,741 ms
152,400 KB
testcase_05 AC 906 ms
100,596 KB
testcase_06 AC 1,898 ms
158,160 KB
testcase_07 AC 2,091 ms
172,200 KB
testcase_08 AC 1,784 ms
152,900 KB
testcase_09 AC 1,478 ms
133,180 KB
testcase_10 AC 1,477 ms
128,176 KB
testcase_11 AC 942 ms
107,052 KB
testcase_12 AC 1,099 ms
113,900 KB
testcase_13 AC 1,203 ms
120,196 KB
testcase_14 AC 766 ms
99,056 KB
testcase_15 AC 2,182 ms
175,024 KB
testcase_16 AC 1,214 ms
114,220 KB
testcase_17 AC 2,085 ms
169,072 KB
testcase_18 AC 531 ms
171,444 KB
testcase_19 AC 469 ms
168,436 KB
testcase_20 AC 470 ms
168,104 KB
testcase_21 AC 176 ms
79,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import io, os
input = sys.stdin.buffer.readline

n,m,q = map(int, input().split())
S = [list(str(input())) for _ in range(n)]
#print(S)
S = [s[2:-3] for s in S]
g = [[] for _ in range(n)]

for i in range(m):
    u, v = map(int, input().split())
    u, v = u-1, v-1
    g[u].append(v)
    g[v].append(u)

def Find(x, par):
    if par[x] < 0:
        return x
    else:
        par[x] = Find(par[x], par)
        return par[x]

def Unite(x, y, par, rank):
    x = Find(x, par)
    y = Find(y, par)

    if x != y:
        if rank[x] < rank[y]:
            par[y] += par[x]
            par[x] = y
        else:
            par[x] += par[y]
            par[y] = x
            if rank[x] == rank[y]:
                rank[x] += 1

def Same(x, y, par):
    return Find(x, par) == Find(y, par)

def Size(x, par):
    return -par[Find(x, par)]

N = n*7
par = [-1]*N
rank = [0]*N

for c in range(7):
    for v in range(n):
        for u in g[v]:
            if S[v][c] == '1' and S[u][c] == '1':
                Unite(n*c+v, n*c+u, par, rank)

for c in range(7):
    for v in range(n):
        if S[v][c] == '1':
            if c == 0:
                if S[v][6] == '1':
                    Unite(v, n*6+v, par, rank)
                if S[v][1] == '1':
                    Unite(v, n*1+v, par, rank)
            elif c == 6:
                if S[v][0] == '1':
                    Unite(n*6+v, v, par, rank)
                if S[v][5] == '1':
                    Unite(n*6+v, n*5+v, par, rank)
            else:
                if S[v][c-1] == '1':
                    Unite(n*c+v, n*(c-1)+v, par, rank)
                if S[v][c+1] == '1':
                    Unite(n*c+v, n*(c+1)+v, par, rank)

for i in range(q):
    t, x, y = map(int, input().split())
    if t == 2:
        x -= 1
        print(Size(x, par))
    else:
        x, y = x-1, y-1
        S[x][y] = '1'
        for u in g[x]:
            if S[u][y] == '1':
                Unite(n*y+x, n*y+u, par, rank)
        if y == 0:
            if S[x][6] == '1':
                Unite(x, n*6+x, par, rank)
            if S[x][1] == '1':
                Unite(x, n*1+x, par, rank)
        elif y == 6:
            if S[x][0] == '1':
                Unite(n*6+x, x, par, rank)
            if S[x][5] == '1':
                Unite(n*6+x, n*5+x, par, rank)
        else:
            if S[x][y-1] == '1':
                Unite(n*y+x, n*(y-1)+x, par, rank)
            if S[x][y+1] == '1':
                Unite(n*y+x, n*(y+1)+x, par, rank)
        #print(par)
0