結果

問題 No.1640 簡単な色塗り
ユーザー rin204rin204
提出日時 2022-01-26 16:19:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 450 ms / 2,000 ms
コード長 2,078 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 101,660 KB
最終ジャッジ日時 2024-06-02 10:38:33
合計ジャッジ時間 23,409 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
52,352 KB
testcase_01 AC 36 ms
52,864 KB
testcase_02 AC 35 ms
52,224 KB
testcase_03 AC 34 ms
52,608 KB
testcase_04 AC 150 ms
97,424 KB
testcase_05 AC 175 ms
99,968 KB
testcase_06 AC 34 ms
52,736 KB
testcase_07 AC 34 ms
53,056 KB
testcase_08 AC 34 ms
52,352 KB
testcase_09 AC 34 ms
52,736 KB
testcase_10 AC 318 ms
92,160 KB
testcase_11 AC 281 ms
88,036 KB
testcase_12 AC 272 ms
87,552 KB
testcase_13 AC 414 ms
97,444 KB
testcase_14 AC 374 ms
96,904 KB
testcase_15 AC 230 ms
84,780 KB
testcase_16 AC 243 ms
85,504 KB
testcase_17 AC 357 ms
95,076 KB
testcase_18 AC 141 ms
78,484 KB
testcase_19 AC 265 ms
88,228 KB
testcase_20 AC 302 ms
91,340 KB
testcase_21 AC 270 ms
87,884 KB
testcase_22 AC 125 ms
78,208 KB
testcase_23 AC 321 ms
92,416 KB
testcase_24 AC 188 ms
80,852 KB
testcase_25 AC 275 ms
87,868 KB
testcase_26 AC 342 ms
93,000 KB
testcase_27 AC 233 ms
84,156 KB
testcase_28 AC 395 ms
98,228 KB
testcase_29 AC 335 ms
93,056 KB
testcase_30 AC 168 ms
80,948 KB
testcase_31 AC 368 ms
96,896 KB
testcase_32 AC 342 ms
94,080 KB
testcase_33 AC 269 ms
89,356 KB
testcase_34 AC 304 ms
92,552 KB
testcase_35 AC 279 ms
89,032 KB
testcase_36 AC 165 ms
81,024 KB
testcase_37 AC 176 ms
81,788 KB
testcase_38 AC 325 ms
95,616 KB
testcase_39 AC 216 ms
85,872 KB
testcase_40 AC 211 ms
85,364 KB
testcase_41 AC 295 ms
92,052 KB
testcase_42 AC 241 ms
86,448 KB
testcase_43 AC 245 ms
86,704 KB
testcase_44 AC 248 ms
86,760 KB
testcase_45 AC 207 ms
84,224 KB
testcase_46 AC 161 ms
80,660 KB
testcase_47 AC 143 ms
79,192 KB
testcase_48 AC 338 ms
95,596 KB
testcase_49 AC 102 ms
78,132 KB
testcase_50 AC 33 ms
52,736 KB
testcase_51 AC 32 ms
52,608 KB
testcase_52 AC 414 ms
101,280 KB
testcase_53 AC 450 ms
101,660 KB
07_evil_01.txt AC 774 ms
126,576 KB
07_evil_02.txt AC 1,126 ms
149,000 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