結果
問題 | No.1605 Matrix Shape |
ユーザー | kyaneko999 |
提出日時 | 2021-07-16 22:10:15 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 555 ms / 2,000 ms |
コード長 | 4,553 bytes |
コンパイル時間 | 394 ms |
コンパイル使用メモリ | 82,316 KB |
実行使用メモリ | 119,156 KB |
最終ジャッジ日時 | 2024-07-06 09:27:09 |
合計ジャッジ時間 | 9,241 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
from sys import exit, stdin, setrecursionlimit from collections import deque, defaultdict, Counter from copy import deepcopy from bisect import bisect_left, bisect_right, insort_left, insort_right from heapq import heapify, heappop, heappush from itertools import product, permutations, combinations, combinations_with_replacement from functools import reduce from math import gcd, sin, cos, tan, asin, acos, atan, atan2, degrees, radians, ceil, floor, sqrt, factorial from math import pi as PI from random import randint # from decimal import Decimal setrecursionlimit(500000) INF = (1<<61)-1 EPS = 1e-10 MOD = 10**9+7 # MOD = 998244353 def input(): return stdin.readline()[:-1] def intput(): return int(stdin.readline()) def minput(): return stdin.readline()[:-1].split() def linput(): return stdin.readline()[:-1].split() def mint(): return map(int,stdin.readline().split()) def lint(): return list(map(int,stdin.readline().split())) def ilint(): return int(stdin.readline()), list(map(int,stdin.readline().split())) def lcm(x,y): return x*y//gcd(x,y) def lgcd(l): return reduce(gcd,l) def llcm(l): return reduce(lcm,l) def powmod(n,i,mod=MOD): return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod) def div2(x): return x.bit_length() def div10(x): return len(str(x))-(x==0) def popcount(x): return bin(x).count('1') def digit(x,i,max_len=None): s = str(x) if max_len: i -= max_len-len(s) return int(s[i-1]) if i>0 else 0 def digitsum(x): ans = 0 for i in range(div10(x)): ans += digit(x,i+1) return ans def pf(x,mode='counter'): C = Counter() p = 2 while x>1: k = 0 while x%p==0: x //= p k += 1 if k>0: C[p] += k p = p+2-(p==2) if p*p<x else x if mode=='counter': return C S = set([1]) for k in C: T = set() for x in S: for i in range(C[k]+1): T.add(x*(k**i)) S = T if mode=='set': return S if mode=='list': return sorted(S) def isprime(x): if x<2: return False return len(pf(x,'set'))==2 def matmul(A, B): # import numpy A1, A2 = A >> 15, A & (1 << 15) - 1 B1, B2 = B >> 15, B & (1 << 15) - 1 X = np.dot(A1, B1) % MOD Y = np.dot(A2, B2) Z = np.dot(A1 + A2, B1 + B2) - X - Y return ((X << 30) + (Z << 15) + Y) % MOD def matpow(A, N): P = np.eye(A.shape[0], dtype=np.int64) while N: if N & 1: P = matmul(P, A) A = matmul(A, A) N >>= 1 return P def zash(S): lis = sorted(S) dic = {} for i,x in enumerate(lis): dic[x] = i return lis, dic def pr(*x): print(*x, sep='', end='') if len(x) else print() def lprint(l): for x in l: print(x) def ston(c, c0='a'): return ord(c)-ord(c0) def ntos(x, c0='a'): return chr(x+ord(c0)) def judge(x, l=['Yes', 'No']): print(l[0] if x else l[1]) def debug(*x, flag=1): if flag: print(*x) ###################################################### class UnionFind(): # インデックスは0-start # 初期化 def __init__(self, n): self.n = n self.parents = [-1]*n self.group = n # private function def root(self, x): if self.parents[x]<0: return x else: self.parents[x] = self.root(self.parents[x]) return self.parents[x] # x,yが属するグループの結合 def union(self, x, y): x = self.root(x) y = self.root(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 self.group -= 1 # x,yが同グループか判定 def same(self, x, y): return self.root(x)==self.root(y) # xと同じグループの要素数を取得 def size(self, x): return -self.parents[self.root(x)] # xが親かを判定 def isparent(self, x): return self.parents[x]<0 N=intput() M=2*10**5 U=UnionFind(M+1) delta=Counter() S=set() for _ in range(N): H,W=mint() U.union(H,W) S.add(H) S.add(W) delta[H]+=1 delta[W]-=1 root=None for x in S: if root!=None and U.root(x)!=root: print(0) exit() if root is None: root=U.root(x) cnt=0 for v in delta.values(): cnt+=max(0,v) if cnt==0: print(len(S)) elif cnt==1: print(1) else: print(0)