結果

問題 No.1254 補強への架け橋
ユーザー 👑 rin204rin204
提出日時 2022-01-27 22:16:41
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,168 bytes
コンパイル時間 540 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 108,220 KB
最終ジャッジ日時 2024-06-07 19:55:07
合計ジャッジ時間 31,732 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
51,968 KB
testcase_01 AC 37 ms
52,864 KB
testcase_02 AC 39 ms
52,224 KB
testcase_03 AC 37 ms
52,224 KB
testcase_04 WA -
testcase_05 AC 39 ms
52,480 KB
testcase_06 AC 37 ms
52,480 KB
testcase_07 WA -
testcase_08 AC 37 ms
52,224 KB
testcase_09 AC 38 ms
52,096 KB
testcase_10 AC 38 ms
52,480 KB
testcase_11 AC 37 ms
52,352 KB
testcase_12 AC 39 ms
51,968 KB
testcase_13 AC 40 ms
52,864 KB
testcase_14 AC 40 ms
52,608 KB
testcase_15 AC 38 ms
52,608 KB
testcase_16 AC 38 ms
52,352 KB
testcase_17 AC 38 ms
52,608 KB
testcase_18 WA -
testcase_19 AC 38 ms
52,736 KB
testcase_20 AC 39 ms
52,480 KB
testcase_21 AC 37 ms
52,224 KB
testcase_22 AC 39 ms
52,224 KB
testcase_23 AC 39 ms
52,608 KB
testcase_24 AC 39 ms
52,992 KB
testcase_25 AC 40 ms
52,864 KB
testcase_26 AC 42 ms
53,248 KB
testcase_27 WA -
testcase_28 AC 41 ms
52,864 KB
testcase_29 AC 39 ms
52,480 KB
testcase_30 AC 39 ms
52,608 KB
testcase_31 AC 40 ms
52,608 KB
testcase_32 AC 39 ms
52,352 KB
testcase_33 AC 40 ms
52,736 KB
testcase_34 AC 41 ms
52,992 KB
testcase_35 AC 40 ms
53,248 KB
testcase_36 AC 40 ms
52,992 KB
testcase_37 AC 39 ms
53,120 KB
testcase_38 AC 40 ms
52,992 KB
testcase_39 AC 38 ms
52,224 KB
testcase_40 AC 40 ms
52,736 KB
testcase_41 AC 38 ms
52,608 KB
testcase_42 AC 39 ms
52,608 KB
testcase_43 AC 67 ms
67,328 KB
testcase_44 AC 48 ms
60,544 KB
testcase_45 AC 47 ms
59,776 KB
testcase_46 AC 58 ms
65,408 KB
testcase_47 AC 51 ms
61,184 KB
testcase_48 AC 57 ms
64,896 KB
testcase_49 AC 40 ms
53,504 KB
testcase_50 AC 69 ms
68,480 KB
testcase_51 AC 64 ms
67,328 KB
testcase_52 AC 64 ms
67,584 KB
testcase_53 AC 60 ms
65,280 KB
testcase_54 AC 64 ms
67,200 KB
testcase_55 AC 59 ms
65,536 KB
testcase_56 AC 43 ms
53,632 KB
testcase_57 AC 59 ms
64,512 KB
testcase_58 AC 56 ms
63,616 KB
testcase_59 WA -
testcase_60 AC 47 ms
59,264 KB
testcase_61 AC 61 ms
65,536 KB
testcase_62 AC 42 ms
53,504 KB
testcase_63 AC 168 ms
78,464 KB
testcase_64 AC 115 ms
77,056 KB
testcase_65 AC 143 ms
78,208 KB
testcase_66 AC 135 ms
78,080 KB
testcase_67 AC 100 ms
76,544 KB
testcase_68 AC 146 ms
77,952 KB
testcase_69 AC 163 ms
77,952 KB
testcase_70 AC 116 ms
76,672 KB
testcase_71 AC 110 ms
77,056 KB
testcase_72 AC 163 ms
78,080 KB
testcase_73 AC 115 ms
76,920 KB
testcase_74 AC 149 ms
78,088 KB
testcase_75 AC 141 ms
77,952 KB
testcase_76 AC 88 ms
76,160 KB
testcase_77 AC 130 ms
77,440 KB
testcase_78 AC 163 ms
78,336 KB
testcase_79 AC 177 ms
78,620 KB
testcase_80 AC 163 ms
78,080 KB
testcase_81 AC 199 ms
79,432 KB
testcase_82 AC 163 ms
78,208 KB
testcase_83 AC 443 ms
96,128 KB
testcase_84 AC 451 ms
97,100 KB
testcase_85 AC 350 ms
89,556 KB
testcase_86 AC 358 ms
93,312 KB
testcase_87 AC 356 ms
94,720 KB
testcase_88 AC 181 ms
80,408 KB
testcase_89 AC 464 ms
97,932 KB
testcase_90 AC 340 ms
90,132 KB
testcase_91 AC 267 ms
86,656 KB
testcase_92 AC 213 ms
82,176 KB
testcase_93 AC 340 ms
92,544 KB
testcase_94 AC 351 ms
91,900 KB
testcase_95 AC 342 ms
91,776 KB
testcase_96 AC 400 ms
96,556 KB
testcase_97 AC 296 ms
86,328 KB
testcase_98 AC 439 ms
96,748 KB
testcase_99 AC 369 ms
89,736 KB
testcase_100 AC 476 ms
98,312 KB
testcase_101 AC 233 ms
82,156 KB
testcase_102 AC 179 ms
78,840 KB
testcase_103 AC 220 ms
81,536 KB
testcase_104 AC 274 ms
84,736 KB
testcase_105 AC 331 ms
92,544 KB
testcase_106 AC 297 ms
86,784 KB
testcase_107 AC 386 ms
95,104 KB
testcase_108 AC 381 ms
95,744 KB
testcase_109 AC 402 ms
93,152 KB
testcase_110 AC 342 ms
91,136 KB
testcase_111 AC 338 ms
90,752 KB
testcase_112 AC 282 ms
85,412 KB
testcase_113 AC 371 ms
91,496 KB
testcase_114 AC 298 ms
87,128 KB
testcase_115 AC 213 ms
80,500 KB
testcase_116 AC 295 ms
87,552 KB
testcase_117 AC 260 ms
83,424 KB
testcase_118 AC 414 ms
95,008 KB
testcase_119 AC 327 ms
89,472 KB
testcase_120 AC 406 ms
95,164 KB
testcase_121 AC 226 ms
83,072 KB
testcase_122 AC 303 ms
87,680 KB
testcase_123 AC 41 ms
51,968 KB
testcase_124 AC 466 ms
108,104 KB
testcase_125 AC 483 ms
108,220 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