結果
問題 | No.13 囲みたい! |
ユーザー | vwxyz |
提出日時 | 2021-06-28 21:22:19 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 188 ms / 5,000 ms |
コード長 | 2,409 bytes |
コンパイル時間 | 470 ms |
コンパイル使用メモリ | 82,036 KB |
実行使用メモリ | 91,296 KB |
最終ジャッジ日時 | 2024-06-25 15:58:43 |
合計ジャッジ時間 | 3,679 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 138 ms
89,396 KB |
testcase_01 | AC | 160 ms
89,476 KB |
testcase_02 | AC | 137 ms
89,424 KB |
testcase_03 | AC | 155 ms
90,560 KB |
testcase_04 | AC | 188 ms
91,296 KB |
testcase_05 | AC | 158 ms
90,524 KB |
testcase_06 | AC | 163 ms
90,536 KB |
testcase_07 | AC | 164 ms
90,380 KB |
testcase_08 | AC | 144 ms
90,168 KB |
testcase_09 | AC | 144 ms
90,112 KB |
testcase_10 | AC | 142 ms
90,412 KB |
testcase_11 | AC | 145 ms
90,112 KB |
testcase_12 | AC | 139 ms
89,472 KB |
testcase_13 | AC | 146 ms
90,736 KB |
testcase_14 | AC | 144 ms
90,372 KB |
testcase_15 | AC | 137 ms
89,728 KB |
ソースコード
import bisect import copy import decimal import fractions import heapq import itertools import math import random import sys from collections import Counter, deque,defaultdict from functools import lru_cache,reduce from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max def _heappush_max(heap,item): heap.append(item) heapq._siftdown_max(heap, 0, len(heap)-1) def _heappushpop_max(heap, item): if heap and item < heap[0]: item, heap[0] = heap[0], item heapq._siftup_max(heap, 0) return item from math import gcd as GCD read=sys.stdin.read readline=sys.stdin.readline readlines=sys.stdin.readlines class UnionFind: def __init__(self,n): self.n=n self.parents=[-1]*n def Find(self,x): stack=[] while self.parents[x]>=0: stack.append(x) x=self.parents[x] for y in stack: self.parents[y]=x return x def Union(self,x,y): x=self.Find(x) y=self.Find(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 Size(self,x): return -self.parents[self.Find(x)] def Same(self,x,y): return self.Find(x)==self.Find(y) def Members(self,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): group_members = defaultdict(list) for member in range(self.n): group_members[self.Find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.All_Group_Members().items()) W,H=map(int,readline().split()) M=[list(map(int,readline().split())) for i in range(H)] UF=UnionFind(H*W) ans='impossible' for i in range(H): for j in range(W-1): if M[i][j]!=M[i][j+1]: continue if UF.Same(i*W+j,i*W+j+1): ans='possible' UF.Union(i*W+j,i*W+j+1) for i in range(H-1): for j in range(W): if M[i][j]!=M[i+1][j]: continue if UF.Same(i*W+j,(i+1)*W+j): ans='possible' UF.Union(i*W+j,(i+1)*W+j) print(ans)