結果
問題 | No.1266 7 Colors |
ユーザー | 👑 Kazun |
提出日時 | 2020-10-23 23:08:57 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,380 ms / 3,000 ms |
コード長 | 2,895 bytes |
コンパイル時間 | 288 ms |
コンパイル使用メモリ | 87,248 KB |
実行使用メモリ | 143,192 KB |
最終ジャッジ日時 | 2023-09-28 17:49:45 |
合計ジャッジ時間 | 21,121 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge12 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 78 ms
71,060 KB |
testcase_01 | AC | 78 ms
71,052 KB |
testcase_02 | AC | 78 ms
71,040 KB |
testcase_03 | AC | 783 ms
98,904 KB |
testcase_04 | AC | 1,267 ms
124,596 KB |
testcase_05 | AC | 838 ms
100,260 KB |
testcase_06 | AC | 1,357 ms
129,220 KB |
testcase_07 | AC | 1,380 ms
136,452 KB |
testcase_08 | AC | 1,300 ms
126,616 KB |
testcase_09 | AC | 1,181 ms
113,756 KB |
testcase_10 | AC | 1,152 ms
113,196 KB |
testcase_11 | AC | 927 ms
101,196 KB |
testcase_12 | AC | 1,013 ms
104,760 KB |
testcase_13 | AC | 1,026 ms
106,860 KB |
testcase_14 | AC | 837 ms
100,744 KB |
testcase_15 | AC | 1,370 ms
137,480 KB |
testcase_16 | AC | 1,051 ms
106,084 KB |
testcase_17 | AC | 1,333 ms
132,956 KB |
testcase_18 | AC | 490 ms
143,192 KB |
testcase_19 | AC | 468 ms
131,968 KB |
testcase_20 | AC | 475 ms
132,224 KB |
testcase_21 | AC | 288 ms
90,164 KB |
ソースコード
class Union_Find(): def __init__(self,N): """0,1,...,n-1を要素として初期化する. n:要素数 """ self.n=N self.parents=[-1]*N self.rank=[0]*N def find(self, x): """要素xの属している族を調べる. x:要素 """ if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): """要素x,yを同一視する. x,y:要素 """ x = self.find(x) y = self.find(y) if x == y: return if self.rank[x]>self.rank[y]: self.parents[x] += self.parents[y] self.parents[y] = x else: self.parents[y] += self.parents[x] self.parents[x] = y if self.rank[x]==self.rank[y]: self.rank[y]+=1 def size(self, x): """要素xの属している要素の数. x:要素 """ return -self.parents[self.find(x)] def same(self, x, y): """要素x,yは同一視されているか? x,y:要素 """ return self.find(x) == self.find(y) def members(self, x): """要素xが属している族の要素. ※族の要素の個数が欲しいときはsizeを使うこと!! x:要素 """ root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): """族の名前のリスト """ return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): """族の個数 """ return len(self.roots()) def all_group_members(self): """全ての族の出力 """ return {r: self.members(r) for r in self.roots()} def __str__(self): return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots()) #================================================ N,M,Q=map(int,input().split()) C=7 U=Union_Find(N*C) S=["" for _ in range(N)] for i in range(N): S[i]=list(map(int,input())) for j in range(C): if S[i][j]==S[i][(j+1)%C]==1: U.union(i*C+j,i*C+(j+1)%C) E=[set() for _ in range(N)] for _ in range(M): u,v=map(int,input().split()) u-=1;v-=1 E[u].add(v) E[v].add(u) for j in range(C): if S[u][j]==S[v][j]==1: U.union(u*C+j,v*C+j) X=[] for _ in range(Q): m,x,y=map(int,input().split()) x-=1 y-=1 if m==1: for w in E[x]: if S[w][y]==1: U.union(x*C+y,w*C+y) if S[x][(y-1)%C]==1: U.union(x*C+y,x*C+(y-1)%C) if S[x][(y+1)%C]==1: U.union(x*C+y,x*C+(y+1)%C) S[x][y]=1 else: X.append(U.size(x*C)) print(*X,sep="\n")