結果

問題 No.1266 7 Colors
ユーザー titiatitia
提出日時 2020-10-23 23:49:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,893 ms / 3,000 ms
コード長 1,859 bytes
コンパイル時間 306 ms
コンパイル使用メモリ 87,360 KB
実行使用メモリ 132,088 KB
最終ジャッジ日時 2023-09-28 19:09:43
合計ジャッジ時間 32,468 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,388 KB
testcase_01 AC 77 ms
71,412 KB
testcase_02 AC 76 ms
71,452 KB
testcase_03 AC 2,389 ms
97,340 KB
testcase_04 AC 1,373 ms
119,844 KB
testcase_05 AC 2,065 ms
99,588 KB
testcase_06 AC 1,473 ms
124,584 KB
testcase_07 AC 1,456 ms
132,088 KB
testcase_08 AC 1,452 ms
122,732 KB
testcase_09 AC 1,439 ms
113,696 KB
testcase_10 AC 1,450 ms
110,656 KB
testcase_11 AC 1,701 ms
100,716 KB
testcase_12 AC 1,518 ms
103,400 KB
testcase_13 AC 1,493 ms
105,956 KB
testcase_14 AC 1,905 ms
96,532 KB
testcase_15 AC 1,381 ms
130,764 KB
testcase_16 AC 1,614 ms
105,144 KB
testcase_17 AC 1,444 ms
128,380 KB
testcase_18 AC 876 ms
129,004 KB
testcase_19 AC 1,044 ms
128,776 KB
testcase_20 AC 1,036 ms
127,920 KB
testcase_21 AC 2,893 ms
92,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N,M,Q=map(int,input().split())

ANS=[[0]*7 for i in range(N)]
S=[list(map(int,list(input().strip()))) for i in range(N)]
E=[[] for i in range(N)]

for i in range(M):
    x,y=map(int,input().split())
    x-=1
    y-=1
    E[x].append(y)
    E[y].append(x)
    
# UnionFind

Group = [i for i in range(N*7)] # グループ分け
Nodes = [1]*(N*7) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)


for i in range(N):
    for j in range(7):
        if S[i][j-1]==1 and S[i][j]==1:
            Union(i*7+(j-1)%7,i*7+j)

for i in range(N):
    for to in E[i]:
        for j in range(7):
            if S[i][j]==1 and S[to][j]==1:
                Union(i*7+j,to*7+j)

for i in range(Q):
    q,x,y=map(int,input().split())
    if q==1:
        x-=1
        y-=1
        S[x][y]=1

        Q=[x*7+y]

        while Q:
            town=Q.pop()
            x,y=divmod(town,7)
            town=find(town)

            if S[x][(y+1)%7]==1 and find(x*7+(y+1)%7)!=town:
                Union(x*7+y,x*7+(y+1)%7)
                Q.append(x*7+(y+1)%7)
            if S[x][(y-1)%7]==1 and find(x*7+(y-1)%7)!=town:
                Union(x*7+y,x*7+(y-1)%7)
                Q.append(x*7+(y-1)%7)

            for to in E[x]:
                if S[to][y]==1 and find(to*7+y)!=town:
                    Union(x*7+y,to*7+y)
                    Q.append(to*7+y)
        
    else:
        x-=1
        print(Nodes[find(x*7)])
        
0