結果

問題 No.1605 Matrix Shape
ユーザー rlangevinrlangevin
提出日時 2023-04-25 12:23:47
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,719 bytes
コンパイル時間 379 ms
コンパイル使用メモリ 81,804 KB
実行使用メモリ 446,208 KB
最終ジャッジ日時 2024-04-26 18:22:23
合計ジャッジ時間 21,661 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
54,400 KB
testcase_01 AC 48 ms
54,400 KB
testcase_02 AC 46 ms
54,016 KB
testcase_03 AC 44 ms
54,400 KB
testcase_04 AC 46 ms
54,400 KB
testcase_05 AC 43 ms
54,400 KB
testcase_06 AC 44 ms
54,400 KB
testcase_07 AC 42 ms
54,272 KB
testcase_08 AC 43 ms
54,144 KB
testcase_09 AC 45 ms
54,400 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 43 ms
54,144 KB
testcase_13 AC 43 ms
54,400 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 TLE -
testcase_20 AC 1,451 ms
252,812 KB
testcase_21 TLE -
testcase_22 AC 866 ms
172,464 KB
testcase_23 TLE -
testcase_24 WA -
testcase_25 AC 297 ms
115,672 KB
testcase_26 AC 306 ms
116,384 KB
testcase_27 WA -
testcase_28 AC 273 ms
114,108 KB
testcase_29 WA -
testcase_30 AC 1,009 ms
156,424 KB
testcase_31 WA -
testcase_32 AC 915 ms
164,196 KB
testcase_33 AC 727 ms
132,576 KB
testcase_34 AC 228 ms
135,860 KB
testcase_35 WA -
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**7)
 
def scc(N, G, RG):
    order = []
    used = [0]*N
    group = [None]*N
    def dfs(s):
        used[s] = 1
        for t in G[s]:
            if not used[t]:
                dfs(t)
        order.append(s)
    def rdfs(s, col):
        group[s] = col
        used[s] = 1
        for t in RG[s]:
            if not used[t]:
                rdfs(t, col)
    for i in range(N):
        if not used[i]:
            dfs(i)
    used = [0]*N
    label = 0
    for s in reversed(order):
        if not used[s]:
            rdfs(s, label)
            label += 1
    return label, group
 
# 縮約後のグラフを構築
def construct(N, G, label, group):
    G0 = [set() for i in range(label)]
    GP = [[] for i in range(label)]
    for v in range(N):
        lbs = group[v]
        for w in G[v]:
            lbt = group[w]
            if lbs == lbt:
                continue
            G0[lbs].add(lbt)
        GP[lbs].append(v)
    return G0, GP

from bisect import *
from copy import deepcopy
def compress(lst):
    """
    B: lstを座圧したリスト
    D: 元の値からindexを取得する辞書
    """
    B = []
    D = dict()
    vals = deepcopy(lst)
    vals = list(set(vals))
    vals.sort()
    for i in range(len(lst)):
        ind = bisect_left(vals, lst[i])
        B.append(ind)
    for i in range(len(B)):
        D[lst[i]] = B[i]
    return B, D

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]



N = int(readline())
H, W = [0] * N, [0] * N
for i in range(N):
    H[i], W[i] = map(int, readline().split())
D, _ = compress(H + W)
M = max(D) + 1
G = [[] for i in range(M)]
RG = [[] for i in range(M)]
U = UnionFind(M)
for i in range(N):
    G[D[i]].append(D[i + N])
    RG[D[i + N]].append(D[i])
    U.union(D[i], D[i + N])

if U.get_size(0) != M:
    print(0)
    exit()

label, group = scc(M, G, RG)
G0, GP = construct(M, G, label, group)
print(len(GP[0]))
0