結果

問題 No.1254 補強への架け橋
ユーザー AEnAEn
提出日時 2022-12-12 23:33:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 891 ms / 2,000 ms
コード長 3,113 bytes
コンパイル時間 155 ms
コンパイル使用メモリ 82,136 KB
実行使用メモリ 241,140 KB
最終ジャッジ日時 2024-04-24 13:07:42
合計ジャッジ時間 33,566 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
67,072 KB
testcase_01 AC 66 ms
67,328 KB
testcase_02 AC 65 ms
67,072 KB
testcase_03 AC 63 ms
67,584 KB
testcase_04 AC 62 ms
67,712 KB
testcase_05 AC 66 ms
67,456 KB
testcase_06 AC 65 ms
67,200 KB
testcase_07 AC 65 ms
67,328 KB
testcase_08 AC 64 ms
67,584 KB
testcase_09 AC 64 ms
67,496 KB
testcase_10 AC 65 ms
67,200 KB
testcase_11 AC 66 ms
67,712 KB
testcase_12 AC 64 ms
67,584 KB
testcase_13 AC 64 ms
67,584 KB
testcase_14 AC 65 ms
67,968 KB
testcase_15 AC 66 ms
67,840 KB
testcase_16 AC 66 ms
67,968 KB
testcase_17 AC 65 ms
67,200 KB
testcase_18 AC 64 ms
67,328 KB
testcase_19 AC 65 ms
67,684 KB
testcase_20 AC 65 ms
67,456 KB
testcase_21 AC 63 ms
67,584 KB
testcase_22 AC 64 ms
68,096 KB
testcase_23 AC 63 ms
67,456 KB
testcase_24 AC 66 ms
67,712 KB
testcase_25 AC 64 ms
67,328 KB
testcase_26 AC 64 ms
68,352 KB
testcase_27 AC 64 ms
67,456 KB
testcase_28 AC 65 ms
67,840 KB
testcase_29 AC 65 ms
67,584 KB
testcase_30 AC 66 ms
67,200 KB
testcase_31 AC 65 ms
68,096 KB
testcase_32 AC 64 ms
67,584 KB
testcase_33 AC 67 ms
67,968 KB
testcase_34 AC 65 ms
68,096 KB
testcase_35 AC 63 ms
67,968 KB
testcase_36 AC 62 ms
67,712 KB
testcase_37 AC 66 ms
67,840 KB
testcase_38 AC 68 ms
67,968 KB
testcase_39 AC 64 ms
67,328 KB
testcase_40 AC 64 ms
67,968 KB
testcase_41 AC 63 ms
67,456 KB
testcase_42 AC 63 ms
67,200 KB
testcase_43 AC 76 ms
73,984 KB
testcase_44 AC 69 ms
69,888 KB
testcase_45 AC 68 ms
69,888 KB
testcase_46 AC 74 ms
73,344 KB
testcase_47 AC 70 ms
71,296 KB
testcase_48 AC 71 ms
72,320 KB
testcase_49 AC 68 ms
67,840 KB
testcase_50 AC 85 ms
78,208 KB
testcase_51 AC 75 ms
74,496 KB
testcase_52 AC 86 ms
78,080 KB
testcase_53 AC 73 ms
73,088 KB
testcase_54 AC 74 ms
74,240 KB
testcase_55 AC 76 ms
73,600 KB
testcase_56 AC 70 ms
68,096 KB
testcase_57 AC 70 ms
72,320 KB
testcase_58 AC 74 ms
72,448 KB
testcase_59 AC 67 ms
68,096 KB
testcase_60 AC 68 ms
69,888 KB
testcase_61 AC 73 ms
73,856 KB
testcase_62 AC 66 ms
68,480 KB
testcase_63 AC 146 ms
83,476 KB
testcase_64 AC 109 ms
78,848 KB
testcase_65 AC 141 ms
82,816 KB
testcase_66 AC 131 ms
80,640 KB
testcase_67 AC 105 ms
78,592 KB
testcase_68 AC 134 ms
80,892 KB
testcase_69 AC 141 ms
82,304 KB
testcase_70 AC 108 ms
79,360 KB
testcase_71 AC 105 ms
78,592 KB
testcase_72 AC 143 ms
82,816 KB
testcase_73 AC 119 ms
80,124 KB
testcase_74 AC 142 ms
82,816 KB
testcase_75 AC 134 ms
80,680 KB
testcase_76 AC 96 ms
78,828 KB
testcase_77 AC 128 ms
80,900 KB
testcase_78 AC 150 ms
82,936 KB
testcase_79 AC 145 ms
83,584 KB
testcase_80 AC 144 ms
83,200 KB
testcase_81 AC 157 ms
83,200 KB
testcase_82 AC 143 ms
83,100 KB
testcase_83 AC 528 ms
145,888 KB
testcase_84 AC 493 ms
140,336 KB
testcase_85 AC 356 ms
112,440 KB
testcase_86 AC 421 ms
129,896 KB
testcase_87 AC 466 ms
136,884 KB
testcase_88 AC 157 ms
85,760 KB
testcase_89 AC 511 ms
145,668 KB
testcase_90 AC 370 ms
116,748 KB
testcase_91 AC 297 ms
109,568 KB
testcase_92 AC 205 ms
93,256 KB
testcase_93 AC 408 ms
129,312 KB
testcase_94 AC 401 ms
122,496 KB
testcase_95 AC 401 ms
123,164 KB
testcase_96 AC 457 ms
139,560 KB
testcase_97 AC 285 ms
104,064 KB
testcase_98 AC 486 ms
139,728 KB
testcase_99 AC 349 ms
113,084 KB
testcase_100 AC 504 ms
146,276 KB
testcase_101 AC 210 ms
91,484 KB
testcase_102 AC 161 ms
85,636 KB
testcase_103 AC 194 ms
92,288 KB
testcase_104 AC 250 ms
98,536 KB
testcase_105 AC 412 ms
129,296 KB
testcase_106 AC 317 ms
105,600 KB
testcase_107 AC 508 ms
145,448 KB
testcase_108 AC 467 ms
139,604 KB
testcase_109 AC 426 ms
128,744 KB
testcase_110 AC 415 ms
122,176 KB
testcase_111 AC 443 ms
123,956 KB
testcase_112 AC 280 ms
97,840 KB
testcase_113 AC 391 ms
121,200 KB
testcase_114 AC 299 ms
105,868 KB
testcase_115 AC 195 ms
86,400 KB
testcase_116 AC 316 ms
109,568 KB
testcase_117 AC 239 ms
97,408 KB
testcase_118 AC 463 ms
138,736 KB
testcase_119 AC 328 ms
113,820 KB
testcase_120 AC 460 ms
136,432 KB
testcase_121 AC 221 ms
94,720 KB
testcase_122 AC 315 ms
110,600 KB
testcase_123 AC 65 ms
67,200 KB
testcase_124 AC 891 ms
241,140 KB
testcase_125 AC 843 ms
240,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from typing import List
import sys
sys.setrecursionlimit(10**8)
import pypyjit
pypyjit.set_param("max_unroll_recursion=-1")
input = sys.stdin.readline

class UnionFind:
    """0-indexed"""
    def __init__(self, n):
        self.n = n
        self.parent = [-1] * n
        self.__group_count = n

    def unite(self, x, y) -> bool:
        """xとyをマージ"""
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return False

        self.__group_count -= 1

        if self.parent[x] > self.parent[y]:
            x, y = y, x

        self.parent[x] += self.parent[y]
        self.parent[y] = x
        return True

    def is_same(self, x, y) -> bool:
        """xとyが同じ連結成分か判定"""
        return self.root(x) == self.root(y)

    def root(self, x) -> int:
        """xの根を取得"""
        if self.parent[x] < 0:
            return x
        else:
            # 経路圧縮あり
            # self.parent[x] = self.root(self.parent[x])
            # return self.parent[x]
            # 経路圧縮なし
            return self.root(self.parent[x])

    def size(self, x) -> int:
        """xが属する連結成分のサイズを取得"""
        return -self.parent[self.root(x)]

    def all_sizes(self) -> List[int]:
        """全連結成分のサイズのリストを取得 O(N)"""
        sizes = []
        for i in range(self.n):
            size = self.parent[i]
            if size < 0:
                sizes.append(-size)
        return sizes
    
    def members(self, x) -> List[int]:
        """xが属するグループのリストを返す O(N)"""
        mem = []
        r = self.root(x)
        for i in range(self.n):
            if self.root(i) == r:
                mem.append(i)
        return mem

    def groups(self) -> List[List[int]]:
        """全連結成分の内容のリストを取得 O(N・α(N))"""
        groups = dict()
        for i in range(self.n):
            p = self.root(i)
            if not groups.get(p):
                groups[p] = []
            groups[p].append(i)
        return list(groups.values())

    @property
    def group_count(self) -> int:
        """連結成分の数を取得 O(1)"""
        return self.__group_count

N = int(input())
uf = UnionFind(N)
G = [list() for _ in range(N)]
def dfs(v,p,start,f,edge):
    if v==start and f==True:
        return True
    elif v==start:
        f = True
    if len(G[v])==1:
        return False
    for nex in G[v]:
        if nex==p:
            continue
        if dfs(nex,v,start,f,edge):
            edge.append((nex,v))
            return True

d = defaultdict(int)
for i in range(N):
    a, b = map(int, input().split())
    a-=1;b-=1
    G[a].append(b)
    G[b].append(a)
    d[str(a)+'_'+str(b)] = i+1
    d[str(b)+'_'+str(a)] = i+1
    if uf.is_same(a,b):
        edge = []
        dfs(a,b,a,False,edge)
        print(len(edge))
        ans = []
        for u,v in edge:
            ans.append(d[str(u)+'_'+str(v)])
        ans.sort()
        print(*ans)

    else:
        uf.unite(a,b)

0