結果

問題 No.1254 補強への架け橋
ユーザー 👑 rin204rin204
提出日時 2022-01-27 22:16:41
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,168 bytes
コンパイル時間 1,316 ms
コンパイル使用メモリ 86,676 KB
実行使用メモリ 112,292 KB
最終ジャッジ日時 2023-08-27 00:33:59
合計ジャッジ時間 39,572 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,364 KB
testcase_01 AC 71 ms
71,632 KB
testcase_02 AC 71 ms
71,480 KB
testcase_03 AC 71 ms
71,480 KB
testcase_04 WA -
testcase_05 AC 72 ms
71,588 KB
testcase_06 AC 73 ms
71,404 KB
testcase_07 WA -
testcase_08 AC 72 ms
71,536 KB
testcase_09 AC 71 ms
71,432 KB
testcase_10 AC 71 ms
71,292 KB
testcase_11 AC 72 ms
71,148 KB
testcase_12 AC 74 ms
71,148 KB
testcase_13 AC 74 ms
71,584 KB
testcase_14 AC 72 ms
71,664 KB
testcase_15 AC 72 ms
71,456 KB
testcase_16 AC 72 ms
71,520 KB
testcase_17 AC 73 ms
71,364 KB
testcase_18 WA -
testcase_19 AC 74 ms
71,132 KB
testcase_20 AC 74 ms
71,620 KB
testcase_21 AC 73 ms
71,540 KB
testcase_22 AC 75 ms
71,152 KB
testcase_23 AC 73 ms
71,664 KB
testcase_24 AC 72 ms
71,376 KB
testcase_25 AC 73 ms
71,436 KB
testcase_26 AC 73 ms
71,484 KB
testcase_27 WA -
testcase_28 AC 74 ms
71,504 KB
testcase_29 AC 71 ms
71,372 KB
testcase_30 AC 73 ms
71,388 KB
testcase_31 AC 73 ms
71,280 KB
testcase_32 AC 73 ms
71,480 KB
testcase_33 AC 74 ms
71,560 KB
testcase_34 AC 75 ms
71,424 KB
testcase_35 AC 75 ms
71,372 KB
testcase_36 AC 75 ms
71,152 KB
testcase_37 AC 73 ms
71,572 KB
testcase_38 AC 75 ms
71,292 KB
testcase_39 AC 76 ms
71,380 KB
testcase_40 AC 73 ms
71,416 KB
testcase_41 AC 74 ms
71,572 KB
testcase_42 AC 74 ms
71,404 KB
testcase_43 AC 96 ms
76,640 KB
testcase_44 AC 81 ms
75,820 KB
testcase_45 AC 81 ms
75,160 KB
testcase_46 AC 91 ms
76,428 KB
testcase_47 AC 83 ms
75,776 KB
testcase_48 AC 91 ms
76,428 KB
testcase_49 AC 74 ms
71,268 KB
testcase_50 AC 96 ms
76,700 KB
testcase_51 AC 95 ms
76,596 KB
testcase_52 AC 99 ms
76,636 KB
testcase_53 AC 94 ms
76,476 KB
testcase_54 AC 95 ms
76,712 KB
testcase_55 AC 92 ms
76,280 KB
testcase_56 AC 74 ms
71,324 KB
testcase_57 AC 90 ms
76,528 KB
testcase_58 AC 89 ms
76,204 KB
testcase_59 WA -
testcase_60 AC 80 ms
75,492 KB
testcase_61 AC 92 ms
76,552 KB
testcase_62 AC 76 ms
71,120 KB
testcase_63 AC 207 ms
80,684 KB
testcase_64 AC 149 ms
79,372 KB
testcase_65 AC 181 ms
80,336 KB
testcase_66 AC 170 ms
80,548 KB
testcase_67 AC 127 ms
79,184 KB
testcase_68 AC 176 ms
80,348 KB
testcase_69 AC 203 ms
81,764 KB
testcase_70 AC 147 ms
79,424 KB
testcase_71 AC 135 ms
79,344 KB
testcase_72 AC 202 ms
81,168 KB
testcase_73 AC 143 ms
79,328 KB
testcase_74 AC 186 ms
80,372 KB
testcase_75 AC 177 ms
80,608 KB
testcase_76 AC 118 ms
79,132 KB
testcase_77 AC 166 ms
80,484 KB
testcase_78 AC 206 ms
81,780 KB
testcase_79 AC 204 ms
80,704 KB
testcase_80 AC 195 ms
81,092 KB
testcase_81 AC 231 ms
82,360 KB
testcase_82 AC 198 ms
80,772 KB
testcase_83 AC 493 ms
98,932 KB
testcase_84 AC 558 ms
100,804 KB
testcase_85 AC 422 ms
92,816 KB
testcase_86 AC 445 ms
96,636 KB
testcase_87 AC 439 ms
98,184 KB
testcase_88 AC 216 ms
81,364 KB
testcase_89 AC 565 ms
101,044 KB
testcase_90 AC 417 ms
93,488 KB
testcase_91 AC 336 ms
90,276 KB
testcase_92 AC 265 ms
83,576 KB
testcase_93 AC 420 ms
95,980 KB
testcase_94 AC 426 ms
95,556 KB
testcase_95 AC 419 ms
94,824 KB
testcase_96 AC 476 ms
100,084 KB
testcase_97 AC 360 ms
89,820 KB
testcase_98 AC 537 ms
100,612 KB
testcase_99 AC 445 ms
93,196 KB
testcase_100 AC 552 ms
101,052 KB
testcase_101 AC 287 ms
84,384 KB
testcase_102 AC 215 ms
80,712 KB
testcase_103 AC 273 ms
84,904 KB
testcase_104 AC 327 ms
88,092 KB
testcase_105 AC 406 ms
96,256 KB
testcase_106 AC 373 ms
90,300 KB
testcase_107 AC 475 ms
99,220 KB
testcase_108 AC 470 ms
99,740 KB
testcase_109 AC 486 ms
97,600 KB
testcase_110 AC 419 ms
95,348 KB
testcase_111 AC 399 ms
95,304 KB
testcase_112 AC 338 ms
86,884 KB
testcase_113 AC 472 ms
95,772 KB
testcase_114 AC 359 ms
90,228 KB
testcase_115 AC 260 ms
81,788 KB
testcase_116 AC 353 ms
91,248 KB
testcase_117 AC 307 ms
86,788 KB
testcase_118 AC 496 ms
99,072 KB
testcase_119 AC 390 ms
91,844 KB
testcase_120 AC 474 ms
98,768 KB
testcase_121 AC 273 ms
84,128 KB
testcase_122 AC 357 ms
91,580 KB
testcase_123 AC 72 ms
71,616 KB
testcase_124 AC 583 ms
112,292 KB
testcase_125 AC 601 ms
111,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

        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())
edges = [[] for _ in range(n)]
UF = UnionFind(n)

for i in range(1, n + 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if UF.same(a, b):
        aa, bb, ii = a, b, i
    else:
        UF.union(a, b)
        edges[a].append((b, i))
        edges[b].append((a, i))

used = [False] * n
used[0] = 0
stack = [0]
parent = [-1] * n
bind = [-1] * n
dist = [0] * n

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

ans = [ii]
if dist[aa] < dist[bb]:
    aa, bb = bb, aa
while dist[aa] != dist[bb]:
    ans.append(bind[aa])
    aa = parent[aa]

while aa != bb:
    ans.append(bind[aa])
    aa = parent[aa]
    ans.append(bind[bb])
    bb = parent[bb]

print(len(ans))
print(*ans)
0