結果

問題 No.1640 簡単な色塗り
ユーザー ansainansain
提出日時 2021-08-06 22:42:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 680 ms / 2,000 ms
コード長 3,716 bytes
コンパイル時間 558 ms
コンパイル使用メモリ 87,316 KB
実行使用メモリ 155,084 KB
最終ジャッジ日時 2023-09-12 02:41:19
合計ジャッジ時間 30,728 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
71,624 KB
testcase_01 AC 101 ms
71,684 KB
testcase_02 AC 101 ms
71,800 KB
testcase_03 AC 100 ms
71,808 KB
testcase_04 AC 262 ms
134,404 KB
testcase_05 AC 303 ms
151,040 KB
testcase_06 AC 102 ms
71,692 KB
testcase_07 AC 100 ms
71,784 KB
testcase_08 AC 99 ms
71,464 KB
testcase_09 AC 99 ms
71,924 KB
testcase_10 AC 452 ms
123,816 KB
testcase_11 AC 412 ms
115,252 KB
testcase_12 AC 390 ms
108,536 KB
testcase_13 AC 567 ms
146,352 KB
testcase_14 AC 566 ms
144,480 KB
testcase_15 AC 330 ms
100,732 KB
testcase_16 AC 362 ms
105,888 KB
testcase_17 AC 497 ms
127,244 KB
testcase_18 AC 206 ms
81,636 KB
testcase_19 AC 383 ms
109,376 KB
testcase_20 AC 458 ms
122,348 KB
testcase_21 AC 410 ms
115,496 KB
testcase_22 AC 182 ms
81,220 KB
testcase_23 AC 448 ms
119,016 KB
testcase_24 AC 276 ms
91,084 KB
testcase_25 AC 394 ms
109,468 KB
testcase_26 AC 507 ms
130,848 KB
testcase_27 AC 330 ms
99,324 KB
testcase_28 AC 556 ms
143,528 KB
testcase_29 AC 498 ms
130,632 KB
testcase_30 AC 240 ms
84,420 KB
testcase_31 AC 505 ms
119,536 KB
testcase_32 AC 476 ms
114,880 KB
testcase_33 AC 390 ms
103,256 KB
testcase_34 AC 428 ms
108,776 KB
testcase_35 AC 378 ms
102,952 KB
testcase_36 AC 246 ms
84,460 KB
testcase_37 AC 269 ms
88,444 KB
testcase_38 AC 474 ms
116,432 KB
testcase_39 AC 331 ms
95,372 KB
testcase_40 AC 313 ms
94,828 KB
testcase_41 AC 439 ms
110,980 KB
testcase_42 AC 350 ms
97,620 KB
testcase_43 AC 350 ms
97,632 KB
testcase_44 AC 344 ms
98,264 KB
testcase_45 AC 308 ms
93,744 KB
testcase_46 AC 247 ms
84,996 KB
testcase_47 AC 218 ms
82,772 KB
testcase_48 AC 484 ms
116,936 KB
testcase_49 AC 170 ms
80,176 KB
testcase_50 AC 100 ms
71,620 KB
testcase_51 AC 100 ms
71,820 KB
testcase_52 AC 665 ms
153,520 KB
testcase_53 AC 680 ms
155,084 KB
07_evil_01.txt AC 1,112 ms
232,520 KB
07_evil_02.txt AC 1,565 ms
260,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
import sys
from collections import defaultdict, Counter, deque
from itertools import permutations, combinations, product, combinations_with_replacement, groupby, accumulate
import operator
from math import sqrt, gcd, factorial
# from math import isqrt, prod, comb  #python3.8用(notpypy)
#from bisect import bisect_left, bisect_right
#from functools import lru_cache, reduce
#from heapq import heappush, heappop, heapify, heappushpop, heapreplace
#import numpy as np
#import networkx as nx
#from networkx.utils import UnionFind
#from numba import njit, b1, i1, i4, i8, f8
# numba例 @njit(i1(i4[:], i8[:, :]),cache=True) 引数i4配列、i8 2次元配列,戻り値i1
#from scipy.sparse import csr_matrix
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, NegativeCycleError, maximum_bipartite_matching, maximum_flow, minimum_spanning_tree
def input(): return sys.stdin.readline().rstrip()
def divceil(n, k): return 1+(n-1)//k  # n/kの切り上げを返す
def yn(hantei, yes='Yes', no='No'): print(yes if hantei else no)


# https://note.nkmk.me/python-union-find/から一部改変


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group_num = n
        self.check = [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 x == y:
            return

        self.group_num -= 1
        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x
        self.check[x] |= self.check[y]

    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 self.group_num

    def all_group_members(self):
        self.all_group_member = defaultdict(list)
        for i in range(self.n):
            self.all_group_member[self.find(i)].append(i)
        return self.all_group_member

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def main():
    mod = 10**9+7
    mod2 = 998244353
    n = int(input())
    uf = UnionFind(n)
    ab = [list(map(lambda x: int(x)-1, input().split())) for i in range(n)]
    ans = [None]*n
    left = []
    edges = [[] for i in range(n)]
    for i, (a, b) in enumerate(ab):
        if uf.same(a, b):
            if uf.check[uf.find(a)]:
                yn(0)
                return
            uf.check[uf.find(a)] = 1
            left.append(a)
            ans[i] = a+1
        else:
            uf.union(a, b)
            edges[b].append([a, i])
            edges[a].append([b, i])
    if 0 != 0:
        pass
    else:
        #yn(1)
        d = deque()
        dist = [-1]*n
        for l in left:
            d.append(l)
            dist[l] = 0
        while d:
            v = d.popleft()
            for i, edgenum in edges[v]:
                if dist[i] != -1:
                    continue
                dist[i] = dist[v] + 1
                ans[edgenum] = i+1
                d.append(i)
        if set(ans)==set(range(1,n+1)):
            yn(1)
            print(*ans, sep="\n")
        else:
            yn(0)


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