結果

問題 No.483 マッチ並べ
ユーザー rpy3cpprpy3cpp
提出日時 2017-03-26 21:08:12
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 1,835 bytes
コンパイル時間 71 ms
コンパイル使用メモリ 11,004 KB
実行使用メモリ 8,256 KB
最終ジャッジ日時 2023-09-20 11:07:33
合計ジャッジ時間 3,231 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,056 KB
testcase_01 AC 18 ms
8,012 KB
testcase_02 AC 17 ms
8,108 KB
testcase_03 AC 17 ms
8,056 KB
testcase_04 AC 16 ms
8,056 KB
testcase_05 AC 17 ms
8,164 KB
testcase_06 AC 16 ms
8,108 KB
testcase_07 AC 16 ms
8,084 KB
testcase_08 AC 17 ms
8,164 KB
testcase_09 AC 16 ms
8,060 KB
testcase_10 AC 16 ms
8,096 KB
testcase_11 AC 16 ms
8,212 KB
testcase_12 AC 17 ms
8,052 KB
testcase_13 AC 17 ms
8,012 KB
testcase_14 AC 17 ms
8,100 KB
testcase_15 AC 17 ms
8,048 KB
testcase_16 AC 17 ms
8,156 KB
testcase_17 AC 16 ms
8,044 KB
testcase_18 AC 16 ms
8,056 KB
testcase_19 AC 17 ms
8,016 KB
testcase_20 AC 17 ms
8,012 KB
testcase_21 AC 16 ms
8,060 KB
testcase_22 AC 17 ms
8,100 KB
testcase_23 AC 16 ms
8,088 KB
testcase_24 AC 17 ms
8,096 KB
testcase_25 AC 17 ms
8,160 KB
testcase_26 AC 17 ms
8,092 KB
testcase_27 AC 17 ms
8,244 KB
testcase_28 AC 17 ms
8,092 KB
testcase_29 AC 17 ms
8,208 KB
testcase_30 AC 17 ms
8,196 KB
testcase_31 AC 17 ms
8,152 KB
testcase_32 AC 17 ms
8,232 KB
testcase_33 AC 16 ms
8,100 KB
testcase_34 AC 16 ms
8,056 KB
testcase_35 AC 17 ms
8,100 KB
testcase_36 AC 17 ms
8,224 KB
testcase_37 AC 16 ms
8,152 KB
testcase_38 AC 17 ms
8,104 KB
testcase_39 AC 17 ms
8,164 KB
testcase_40 AC 17 ms
8,016 KB
testcase_41 AC 17 ms
8,232 KB
testcase_42 AC 17 ms
8,040 KB
testcase_43 AC 17 ms
8,048 KB
testcase_44 AC 17 ms
8,052 KB
testcase_45 AC 17 ms
8,012 KB
testcase_46 AC 17 ms
8,256 KB
testcase_47 AC 17 ms
8,148 KB
testcase_48 AC 18 ms
8,052 KB
testcase_49 AC 17 ms
8,044 KB
testcase_50 AC 17 ms
8,084 KB
testcase_51 AC 17 ms
8,176 KB
testcase_52 AC 17 ms
8,044 KB
testcase_53 AC 16 ms
8,048 KB
testcase_54 AC 17 ms
8,156 KB
testcase_55 AC 17 ms
8,052 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class DisjointSet(object):
    def __init__(self, n):
        self.parent = list(range(n))
        self.rank = [0] * n
        self.num = n  # number of disjoint sets

    def union(self, x, y):
        self._link(self.find_set(x), self.find_set(y))

    def _link(self, x, y):
        if x == y:
            return
        self.num -= 1
        if self.rank[x] > self.rank[y]:
            self.parent[y] = x
        else:
            self.parent[x] = y
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

    def find_set(self, x):
        xp = self.parent[x]
        if xp != x:
            self.parent[x] = self.find_set(xp)
        return self.parent[x]

def read_data():
    N = int(input())
    edges = []
    rcs = set()
    for n in range(N):
        r0, c0, r1, c1 = map(int, input().split())
        (r0, c0), (r1, c1) = sorted([(r0, c0), (r1, c1)])
        edges.append((r0, c0, r1, c1))
        rcs.add((r0, c0))
        rcs.add((r1, c1))
    return N, edges, rcs

def solve(N, edges, rcs):
    np = len(rcs)
    rc2idx = dict()
    for i, (r, c) in enumerate(rcs):
        rc2idx[(r, c)] = i
    idx2e = []
    ds_v = DisjointSet(np)
    for i, (r0, c0, r1, c1) in enumerate(edges):
        p0 = rc2idx[(r0, c0)]
        p1 = rc2idx[(r1, c1)]
        idx2e.append((p0, p1))
        ds_v.union(p0, p1)
    
    ds_v_size = [0] * np
    for p in range(np):
        g = ds_v.find_set(p)
        ds_v_size[g] += 1
    
    e_size = [0] * np
    for i, (p0, p1) in enumerate(idx2e):
        e_size[ds_v.find_set(p0)] += 1
    
    for p in range(np):
        g = ds_v.find_set(p)        
        if g != p:
            continue
        if ds_v_size[g] < e_size[g]:
            return False
    return True

N, edges, rcs = read_data()
if solve(N, edges, rcs):
    print('YES')
else:
    print('NO')
0