結果

問題 No.1640 簡単な色塗り
ユーザー kept1994kept1994
提出日時 2021-09-08 02:11:55
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,542 bytes
コンパイル時間 938 ms
コンパイル使用メモリ 87,096 KB
実行使用メモリ 109,164 KB
最終ジャッジ日時 2023-09-12 04:12:01
合計ジャッジ時間 22,456 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 93 ms
71,768 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 90 ms
71,756 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 227 ms
82,256 KB
testcase_31 AC 431 ms
101,876 KB
testcase_32 AC 380 ms
99,892 KB
testcase_33 AC 322 ms
92,736 KB
testcase_34 AC 347 ms
97,552 KB
testcase_35 AC 317 ms
93,152 KB
testcase_36 AC 219 ms
82,264 KB
testcase_37 AC 235 ms
82,980 KB
testcase_38 AC 413 ms
101,004 KB
testcase_39 AC 287 ms
88,220 KB
testcase_40 AC 294 ms
87,056 KB
testcase_41 AC 367 ms
97,088 KB
testcase_42 AC 300 ms
88,172 KB
testcase_43 AC 309 ms
90,456 KB
testcase_44 AC 293 ms
89,516 KB
testcase_45 AC 278 ms
87,392 KB
testcase_46 AC 219 ms
82,348 KB
testcase_47 AC 197 ms
81,080 KB
testcase_48 AC 391 ms
101,768 KB
testcase_49 AC 168 ms
79,560 KB
testcase_50 AC 88 ms
71,884 KB
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
07_evil_01.txt WA -
07_evil_02.txt WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import sys
from collections import defaultdict
from collections import deque

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    """ 要素xの値を取得。"""
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    """ 2つの要素の併合。"""
    def union(self, x, y):
        x = self.find(x)
        y = self.find(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
        return

    """ 要素xの属する集合の要素数を取得。"""
    def size(self, x):
        return -self.parents[self.find(x)]

    """ 2つの要素が同一の集合に属するか。"""
    def same(self, x, y):
        return self.find(x) == self.find(y)

    """ 要素xと同一の集合の要素を全取得。
    計算量 : O(N)
    """
    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    """ 各集合の根を全取得。
    計算量 : O(N)
    """
    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    """ 集合の個数を取得。
    計算量 : O(N)
    """
    def group_count(self):
        return len(self.roots())

    """ 全集合の要素一覧を取得。
    計算量 : O(N)
    """
    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())

def main():
    N = int(input())
    uf = UnionFind(N)
    G = [[] for _ in range(N)]
    for nn in range(N):
        a, b = map(lambda i: int(i) - 1, input().split())
        G[a].append((b, nn)) # 答えは順番に出力するので保持しておく必要がある。
        if a != b:
            G[b].append((a, nn))
        uf.union(a, b)
    
    seen=[0]*N
    ans=[-1]*N # 答え出力用
    for _, linked in uf.all_group_members().items():
        E = 0
        for i in linked:
            E = len(G[i])
        if E != len(linked):
            print("No")
            return
        
    ans = 0
    print(ans)
    return
    

if __name__ == '__main__':
    main()
0