結果
問題 | No.1605 Matrix Shape |
ユーザー | buey_t |
提出日時 | 2023-04-16 18:29:59 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,264 bytes |
コンパイル時間 | 344 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 186,388 KB |
最終ジャッジ日時 | 2024-10-12 01:30:03 |
合計ジャッジ時間 | 13,783 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 136 ms
97,536 KB |
testcase_01 | AC | 133 ms
92,432 KB |
testcase_02 | AC | 140 ms
92,544 KB |
testcase_03 | WA | - |
testcase_04 | AC | 135 ms
92,544 KB |
testcase_05 | AC | 137 ms
92,632 KB |
testcase_06 | RE | - |
testcase_07 | AC | 145 ms
92,656 KB |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | AC | 137 ms
92,672 KB |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | AC | 139 ms
92,632 KB |
testcase_17 | AC | 140 ms
92,424 KB |
testcase_18 | AC | 142 ms
92,544 KB |
testcase_19 | AC | 1,072 ms
186,388 KB |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | AC | 1,088 ms
173,712 KB |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | TLE | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
ソースコード
from math import sqrt,sin,cos,tan,ceil,radians,floor,gcd,exp,log,log10,log2,factorial,fsum from heapq import heapify, heappop, heappush from bisect import bisect_left, bisect_right from copy import deepcopy import copy import random from collections import deque,Counter,defaultdict from itertools import permutations,combinations from decimal import Decimal,ROUND_HALF_UP #tmp = Decimal(mid).quantize(Decimal('0'), rounding=ROUND_HALF_UP) from functools import lru_cache, reduce #@lru_cache(maxsize=None) from operator import add,sub,mul,xor,and_,or_,itemgetter INF = 10**18 mod1 = 10**9+7 mod2 = 998244353 #DecimalならPython #再帰ならPython!!!!!!!!!!!!!!!!!!!!!!!!!! class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n #親だけ self.edge = [0]*n def find(self, 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 = self.find(x) y = self.find(y) if self.parents[x] > self.parents[y]: x, y = y, x self.edge[x] += 1 if x == y: return self.parents[x] += self.parents[y] self.edge[x] += self.edge[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()) ''' グラフっぽく考えられそう 最初と終わりさえ決めれば間は関係ない 全部を通って終わりまでたどりつけるか?という問題 sccして1個なら全部行ける 1個じゃなくて、パスがあったらその組はいける 入次数0から始める そこから全部通れるか サイクルはSCCで対処可能 ''' M = int(input()) lim = 2*(10**5)+1 lef = [[] for _ in range(lim)] rig = [[] for _ in range(lim)] uf = UnionFind(M+1) hw = [] st = set() for i in range(1,M+1): h,w = map(int, input().split()) if (h,w) in st: continue st.add((h,w)) lef[h].append(i) rig[w].append(i) hw.append((h,w)) N = len(hw) memo1 = [0]*(N+1) memo2 = [0]*(N+1) edge = [] for i in range(1,N+1): h,w = hw[i-1] for j in lef[w]: memo1[j] += 1 memo2[i] += 1 uf.union(i,j) for j in rig[h]: memo1[i] += 1 memo2[j] += 1 uf.union(i,j) if uf.group_count() != 2: print(0) exit() ls = [] for i in range(1,N+1): ls.append(memo1[i]-memo2[i]) if ls[0] == ls[-1] == 0: print(N) elif ls[0] == -ls[-1] and ls[1] == ls[-2] == 0: print(1) else: print(0)