結果
問題 | No.697 池の数はいくつか |
ユーザー | vwxyz |
提出日時 | 2022-01-05 07:09:48 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
MLE
|
実行時間 | - |
コード長 | 3,636 bytes |
コンパイル時間 | 93 ms |
コンパイル使用メモリ | 13,056 KB |
実行使用メモリ | 818,148 KB |
最終ジャッジ日時 | 2024-05-04 07:27:39 |
合計ジャッジ時間 | 22,304 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 25 ms
11,264 KB |
testcase_01 | AC | 25 ms
11,264 KB |
testcase_02 | AC | 25 ms
11,136 KB |
testcase_03 | AC | 27 ms
11,136 KB |
testcase_04 | AC | 26 ms
11,264 KB |
testcase_05 | AC | 26 ms
11,264 KB |
testcase_06 | AC | 26 ms
11,264 KB |
testcase_07 | AC | 26 ms
11,136 KB |
testcase_08 | AC | 26 ms
11,136 KB |
testcase_09 | AC | 26 ms
11,136 KB |
testcase_10 | AC | 25 ms
11,264 KB |
testcase_11 | AC | 26 ms
11,264 KB |
testcase_12 | AC | 25 ms
11,136 KB |
testcase_13 | AC | 26 ms
11,136 KB |
testcase_14 | AC | 25 ms
11,136 KB |
testcase_15 | AC | 26 ms
11,136 KB |
testcase_16 | AC | 26 ms
11,136 KB |
testcase_17 | AC | 26 ms
11,264 KB |
testcase_18 | AC | 25 ms
11,136 KB |
testcase_19 | AC | 26 ms
11,264 KB |
testcase_20 | AC | 26 ms
11,264 KB |
testcase_21 | AC | 26 ms
11,264 KB |
testcase_22 | AC | 26 ms
11,136 KB |
testcase_23 | AC | 26 ms
11,136 KB |
testcase_24 | AC | 2,841 ms
271,764 KB |
testcase_25 | AC | 2,867 ms
271,808 KB |
testcase_26 | AC | 2,820 ms
271,796 KB |
testcase_27 | AC | 2,824 ms
271,532 KB |
testcase_28 | AC | 2,894 ms
271,812 KB |
testcase_29 | MLE | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
ソースコード
import sys readline=sys.stdin.readline from collections import deque class Graph: def __init__(self,V,edges=False,graph=False,directed=False,weighted=False,inf=float("inf")): self.V=V self.directed=directed self.weighted=weighted self.inf=inf if not graph: self.edges=edges self.graph=[[] for i in range(self.V)] if weighted: for i,j,d in self.edges: self.graph[i].append((j,d)) if not self.directed: self.graph[j].append((i,d)) else: for i,j in self.edges: self.graph[i].append(j) if not self.directed: self.graph[j].append(i) else: self.graph=graph self.edges=[] for i in range(self.V): if self.weighted: for j,d in self.graph[i]: if self.directed or not self.directed and i<=j: self.edges.append((i,j,d)) else: for j in self.graph[i]: if self.directed or not self.directed and i<=j: self.edges.append((i,j)) def MIV_BFS(self,initial_vertices=False,bipartite_graph=False,linked_components=False,parents=False): if not initial_vertices: initial_vertices=[i for i in range(self.V)] seen=[False]*self.V if bipartite_graph: bg=[None]*self.V cnt=-1 if linked_components: lc=[] if parents: ps=[None]*self.V for s in initial_vertices: if seen[s]: continue seen[s]=True if bipartite_graph: cnt+=1 bg[s]=(cnt,0) if linked_components: lc.append([s]) queue=deque([s]) while queue: x=queue.popleft() for y in self.graph[x]: if self.weighted: y,d=y if not seen[y]: seen[y]=True queue.append(y) if bipartite_graph: bg[y]=(cnt,bg[x][1]^1) if linked_components: lc[-1].append(y) if parents: ps[y]=x if bipartite_graph: bg_=bg bg=[[[],[]] for i in range(cnt+1)] for tpl in self.edges: i,j=tpl[:2] if self.weighted else tpl if not bg_[i][1]^bg_[j][1]: bg[bg_[i][0]]=False for x in range(self.V): if bg[bg_[x][0]]: bg[bg_[x][0]][bg_[x][1]].append(x) retu=() if bipartite_graph: retu+=(bg,) if linked_components: retu+=(lc,) if parents: retu=(ps,) if len(retu)==1: retu=retu[0] return retu H,W=map(int,readline().split()) A=[list(map(int,readline().split())) for h in range(H)] edges=[] for h in range(H-1): for w in range(W): if A[h][w] and A[h+1][w]: edges.append((h*W+w,(h+1)*W+w)) for h in range(H): for w in range(W-1): if A[h][w] and A[h][w+1]: edges.append((h*W+w,h*W+w+1)) G=Graph(H*W,edges=edges) ans=len(G.MIV_BFS(linked_components=True)) for h in range(H): for w in range(W): if A[h][w]==0: ans-=1 print(ans)