結果

問題 No.1605 Matrix Shape
ユーザー buey_tbuey_t
提出日時 2023-04-17 11:59:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 626 ms / 2,000 ms
コード長 2,858 bytes
コンパイル時間 200 ms
コンパイル使用メモリ 82,292 KB
実行使用メモリ 117,388 KB
最終ジャッジ日時 2024-04-20 17:34:49
合計ジャッジ時間 13,280 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 151 ms
105,472 KB
testcase_01 AC 150 ms
105,600 KB
testcase_02 AC 153 ms
105,472 KB
testcase_03 AC 153 ms
105,472 KB
testcase_04 AC 153 ms
105,496 KB
testcase_05 AC 154 ms
105,344 KB
testcase_06 AC 154 ms
105,428 KB
testcase_07 AC 148 ms
105,344 KB
testcase_08 AC 154 ms
105,360 KB
testcase_09 AC 151 ms
105,584 KB
testcase_10 AC 153 ms
105,344 KB
testcase_11 AC 151 ms
105,352 KB
testcase_12 AC 152 ms
105,344 KB
testcase_13 AC 153 ms
105,472 KB
testcase_14 AC 153 ms
105,344 KB
testcase_15 AC 152 ms
105,344 KB
testcase_16 AC 154 ms
105,472 KB
testcase_17 AC 149 ms
105,216 KB
testcase_18 AC 155 ms
105,344 KB
testcase_19 AC 623 ms
117,388 KB
testcase_20 AC 580 ms
112,948 KB
testcase_21 AC 575 ms
113,216 KB
testcase_22 AC 626 ms
115,748 KB
testcase_23 AC 626 ms
117,240 KB
testcase_24 AC 623 ms
117,356 KB
testcase_25 AC 278 ms
107,136 KB
testcase_26 AC 277 ms
106,880 KB
testcase_27 AC 278 ms
106,916 KB
testcase_28 AC 283 ms
106,880 KB
testcase_29 AC 283 ms
107,008 KB
testcase_30 AC 551 ms
114,332 KB
testcase_31 AC 555 ms
114,572 KB
testcase_32 AC 492 ms
111,992 KB
testcase_33 AC 466 ms
110,352 KB
testcase_34 AC 252 ms
106,452 KB
testcase_35 AC 526 ms
111,624 KB
testcase_36 AC 407 ms
109,560 KB
権限があれば一括ダウンロードができます

ソースコード

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
uf = UnionFind(lim)
memo = defaultdict(int)
for i in range(N):
    h,w = map(int, input().split())
    memo[h-1] += 1
    memo[w-1] -= 1
    uf.union(h,w)

M = len(memo.keys())

if uf.group_count() != lim-M+1:
    print(0)
    exit()

ls = list(memo.values())
ls.sort()

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