結果

問題 No.1254 補強への架け橋
ユーザー 👑 rin204rin204
提出日時 2022-01-27 22:20:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 476 ms / 2,000 ms
コード長 2,177 bytes
コンパイル時間 682 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 108,724 KB
最終ジャッジ日時 2024-06-07 19:59:41
合計ジャッジ時間 28,919 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,096 KB
testcase_01 AC 41 ms
52,736 KB
testcase_02 AC 40 ms
52,480 KB
testcase_03 AC 40 ms
52,480 KB
testcase_04 AC 39 ms
51,968 KB
testcase_05 AC 41 ms
52,224 KB
testcase_06 AC 39 ms
52,352 KB
testcase_07 AC 39 ms
52,096 KB
testcase_08 AC 39 ms
52,096 KB
testcase_09 AC 40 ms
52,352 KB
testcase_10 AC 40 ms
52,224 KB
testcase_11 AC 40 ms
52,224 KB
testcase_12 AC 40 ms
52,480 KB
testcase_13 AC 41 ms
52,608 KB
testcase_14 AC 43 ms
52,864 KB
testcase_15 AC 40 ms
52,352 KB
testcase_16 AC 40 ms
52,352 KB
testcase_17 AC 38 ms
52,224 KB
testcase_18 AC 39 ms
52,224 KB
testcase_19 AC 38 ms
52,096 KB
testcase_20 AC 41 ms
52,736 KB
testcase_21 AC 39 ms
52,480 KB
testcase_22 AC 41 ms
52,736 KB
testcase_23 AC 41 ms
52,608 KB
testcase_24 AC 41 ms
52,736 KB
testcase_25 AC 40 ms
52,352 KB
testcase_26 AC 40 ms
52,864 KB
testcase_27 AC 42 ms
52,864 KB
testcase_28 AC 41 ms
52,864 KB
testcase_29 AC 40 ms
52,480 KB
testcase_30 AC 40 ms
52,736 KB
testcase_31 AC 41 ms
52,480 KB
testcase_32 AC 39 ms
52,352 KB
testcase_33 AC 43 ms
52,736 KB
testcase_34 AC 42 ms
53,376 KB
testcase_35 AC 42 ms
52,864 KB
testcase_36 AC 41 ms
52,864 KB
testcase_37 AC 41 ms
52,864 KB
testcase_38 AC 43 ms
52,736 KB
testcase_39 AC 41 ms
52,480 KB
testcase_40 AC 40 ms
52,864 KB
testcase_41 AC 41 ms
52,608 KB
testcase_42 AC 40 ms
52,608 KB
testcase_43 AC 70 ms
67,712 KB
testcase_44 AC 53 ms
60,288 KB
testcase_45 AC 51 ms
59,392 KB
testcase_46 AC 64 ms
65,152 KB
testcase_47 AC 56 ms
61,440 KB
testcase_48 AC 61 ms
63,872 KB
testcase_49 AC 42 ms
53,248 KB
testcase_50 AC 70 ms
67,456 KB
testcase_51 AC 68 ms
67,200 KB
testcase_52 AC 76 ms
67,200 KB
testcase_53 AC 62 ms
65,408 KB
testcase_54 AC 68 ms
66,944 KB
testcase_55 AC 64 ms
65,536 KB
testcase_56 AC 45 ms
53,504 KB
testcase_57 AC 63 ms
63,616 KB
testcase_58 AC 62 ms
63,872 KB
testcase_59 AC 43 ms
52,864 KB
testcase_60 AC 50 ms
59,008 KB
testcase_61 AC 67 ms
65,920 KB
testcase_62 AC 44 ms
53,632 KB
testcase_63 AC 168 ms
78,080 KB
testcase_64 AC 122 ms
77,056 KB
testcase_65 AC 147 ms
77,952 KB
testcase_66 AC 139 ms
77,568 KB
testcase_67 AC 110 ms
76,544 KB
testcase_68 AC 149 ms
77,824 KB
testcase_69 AC 167 ms
77,952 KB
testcase_70 AC 117 ms
76,544 KB
testcase_71 AC 109 ms
76,672 KB
testcase_72 AC 165 ms
78,208 KB
testcase_73 AC 116 ms
76,800 KB
testcase_74 AC 152 ms
78,208 KB
testcase_75 AC 148 ms
78,208 KB
testcase_76 AC 91 ms
76,032 KB
testcase_77 AC 137 ms
77,568 KB
testcase_78 AC 168 ms
78,592 KB
testcase_79 AC 165 ms
78,592 KB
testcase_80 AC 157 ms
78,208 KB
testcase_81 AC 188 ms
79,304 KB
testcase_82 AC 164 ms
78,592 KB
testcase_83 AC 382 ms
96,128 KB
testcase_84 AC 448 ms
97,376 KB
testcase_85 AC 350 ms
89,300 KB
testcase_86 AC 363 ms
93,568 KB
testcase_87 AC 367 ms
94,720 KB
testcase_88 AC 181 ms
79,772 KB
testcase_89 AC 476 ms
97,428 KB
testcase_90 AC 336 ms
89,616 KB
testcase_91 AC 276 ms
86,288 KB
testcase_92 AC 222 ms
82,440 KB
testcase_93 AC 343 ms
92,416 KB
testcase_94 AC 345 ms
91,936 KB
testcase_95 AC 342 ms
91,904 KB
testcase_96 AC 402 ms
96,304 KB
testcase_97 AC 299 ms
86,536 KB
testcase_98 AC 442 ms
96,840 KB
testcase_99 AC 380 ms
90,176 KB
testcase_100 AC 464 ms
98,204 KB
testcase_101 AC 238 ms
82,288 KB
testcase_102 AC 178 ms
79,224 KB
testcase_103 AC 220 ms
82,048 KB
testcase_104 AC 259 ms
84,992 KB
testcase_105 AC 333 ms
92,800 KB
testcase_106 AC 295 ms
86,656 KB
testcase_107 AC 386 ms
95,360 KB
testcase_108 AC 367 ms
95,616 KB
testcase_109 AC 395 ms
93,128 KB
testcase_110 AC 342 ms
91,264 KB
testcase_111 AC 313 ms
90,880 KB
testcase_112 AC 281 ms
84,756 KB
testcase_113 AC 372 ms
91,496 KB
testcase_114 AC 296 ms
86,360 KB
testcase_115 AC 220 ms
80,508 KB
testcase_116 AC 296 ms
87,680 KB
testcase_117 AC 252 ms
84,064 KB
testcase_118 AC 400 ms
95,648 KB
testcase_119 AC 324 ms
89,088 KB
testcase_120 AC 399 ms
94,740 KB
testcase_121 AC 221 ms
82,304 KB
testcase_122 AC 301 ms
87,680 KB
testcase_123 AC 40 ms
52,096 KB
testcase_124 AC 471 ms
107,856 KB
testcase_125 AC 466 ms
108,724 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] = True
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