結果

問題 No.1605 Matrix Shape
ユーザー buey_tbuey_t
提出日時 2023-04-17 11:44:06
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,053 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 853,468 KB
最終ジャッジ日時 2024-04-20 17:26:08
合計ジャッジ時間 13,096 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 145 ms
92,652 KB
testcase_01 AC 144 ms
92,608 KB
testcase_02 AC 144 ms
92,800 KB
testcase_03 AC 146 ms
92,672 KB
testcase_04 AC 143 ms
92,416 KB
testcase_05 AC 146 ms
92,672 KB
testcase_06 AC 146 ms
92,340 KB
testcase_07 AC 143 ms
92,800 KB
testcase_08 AC 146 ms
92,776 KB
testcase_09 AC 147 ms
92,544 KB
testcase_10 AC 146 ms
92,672 KB
testcase_11 AC 150 ms
92,672 KB
testcase_12 WA -
testcase_13 AC 147 ms
92,544 KB
testcase_14 AC 146 ms
92,672 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 148 ms
92,600 KB
testcase_18 AC 149 ms
92,672 KB
testcase_19 AC 801 ms
169,332 KB
testcase_20 AC 864 ms
159,704 KB
testcase_21 AC 865 ms
158,488 KB
testcase_22 AC 778 ms
168,056 KB
testcase_23 WA -
testcase_24 AC 787 ms
169,208 KB
testcase_25 MLE -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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で対処可能
'''

N = int(input())
lim = 2*(10**5)+1
lef = [[] for _ in range(lim)]
rig = [[] for _ in range(lim)]

mem = set()
hw = []
for i in range(N):
    h,w = map(int, input().split())
    hw.append((h,w))
    lef[h].append((w,i))
    mem.add(h)
    mem.add(w)

M = len(mem)
uf = UnionFind(N)

memo1 = [0]*(N)
i = 0
edge = []
for h,w in hw:
    for l,j in lef[w]:
        memo1[i] += 1
        memo1[j] -= 1
        uf.union(i,j)
        edge.append((h,l))
    i += 1

if uf.group_count() != 1:
    print(0)
    exit()

memo1.sort()

if memo1.count(0) == len(memo1):
    print(M)
elif memo1[0] == -memo1[-1]:
    print(1)
else:
    print(0)
0