結果

問題 No.1640 簡単な色塗り
ユーザー 👑 rin204rin204
提出日時 2022-01-26 16:19:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 565 ms / 2,000 ms
コード長 2,078 bytes
コンパイル時間 387 ms
コンパイル使用メモリ 87,008 KB
実行使用メモリ 104,888 KB
最終ジャッジ日時 2023-08-24 21:18:32
合計ジャッジ時間 31,270 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,092 KB
testcase_01 AC 71 ms
71,368 KB
testcase_02 AC 73 ms
71,020 KB
testcase_03 AC 70 ms
71,064 KB
testcase_04 AC 195 ms
95,488 KB
testcase_05 AC 240 ms
100,824 KB
testcase_06 AC 72 ms
71,372 KB
testcase_07 AC 74 ms
71,188 KB
testcase_08 AC 73 ms
71,312 KB
testcase_09 AC 72 ms
71,024 KB
testcase_10 AC 369 ms
93,048 KB
testcase_11 AC 343 ms
91,836 KB
testcase_12 AC 328 ms
90,868 KB
testcase_13 AC 468 ms
101,248 KB
testcase_14 AC 478 ms
101,244 KB
testcase_15 AC 291 ms
87,828 KB
testcase_16 AC 304 ms
88,188 KB
testcase_17 AC 421 ms
97,420 KB
testcase_18 AC 187 ms
81,124 KB
testcase_19 AC 319 ms
89,256 KB
testcase_20 AC 366 ms
92,308 KB
testcase_21 AC 336 ms
91,492 KB
testcase_22 AC 171 ms
80,128 KB
testcase_23 AC 391 ms
95,848 KB
testcase_24 AC 251 ms
84,368 KB
testcase_25 AC 336 ms
90,512 KB
testcase_26 AC 415 ms
96,732 KB
testcase_27 AC 295 ms
87,624 KB
testcase_28 AC 462 ms
100,352 KB
testcase_29 AC 407 ms
95,892 KB
testcase_30 AC 223 ms
82,572 KB
testcase_31 AC 448 ms
99,864 KB
testcase_32 AC 409 ms
96,548 KB
testcase_33 AC 335 ms
92,680 KB
testcase_34 AC 374 ms
94,824 KB
testcase_35 AC 344 ms
91,972 KB
testcase_36 AC 216 ms
82,336 KB
testcase_37 AC 235 ms
84,640 KB
testcase_38 AC 407 ms
97,260 KB
testcase_39 AC 293 ms
88,152 KB
testcase_40 AC 297 ms
88,188 KB
testcase_41 AC 391 ms
96,048 KB
testcase_42 AC 305 ms
88,956 KB
testcase_43 AC 302 ms
89,184 KB
testcase_44 AC 310 ms
88,732 KB
testcase_45 AC 281 ms
86,976 KB
testcase_46 AC 215 ms
83,316 KB
testcase_47 AC 198 ms
82,508 KB
testcase_48 AC 418 ms
98,960 KB
testcase_49 AC 154 ms
79,840 KB
testcase_50 AC 72 ms
71,156 KB
testcase_51 AC 70 ms
71,200 KB
testcase_52 AC 512 ms
104,576 KB
testcase_53 AC 565 ms
104,888 KB
07_evil_01.txt AC 912 ms
128,908 KB
07_evil_02.txt AC 1,280 ms
151,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.hasloop = [False] * n
        self.group = 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:
            self.hasloop[x] = True
            return
        self.group -= 1
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.hasloop[x] |= self.hasloop[y]
        self.parents[x] += self.parents[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 self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
        
n = int(input())
ab = []
UF = UnionFind(n)
edges = [[] for _ in range(n)]
ans = [-1] * n
used = [False] * n
stack = []
for i in range(n):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if UF.same(a, b):
        ans[i] = a + 1
        stack.append(a)
        used[i] = True
    else:
        edges[a].append((b, i))
        edges[b].append((a, i))
    
    UF.union(a, b)
    
for r in UF.roots():
    if not UF.hasloop[r]:
        print("No")
        exit()

print("Yes")

while stack:
    pos = stack.pop()
    for npos, i in edges[pos]:
        if used[i]:
            continue
        used[i] = True
        ans[i] = npos + 1
        stack.append(npos)

print(*ans, sep="\n")

0