結果
| 問題 | No.1254 補強への架け橋 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-11-15 12:45:18 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 537 ms / 2,000 ms | 
| コード長 | 1,068 bytes | 
| コンパイル時間 | 457 ms | 
| コンパイル使用メモリ | 82,436 KB | 
| 実行使用メモリ | 200,812 KB | 
| 最終ジャッジ日時 | 2024-07-23 01:11:42 | 
| 合計ジャッジ時間 | 23,913 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 123 | 
ソースコード
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5+10)
from collections import *
def dfs(v, pv):
    global pos
    
    seen[v] = True
    hist.append(v)
    
    for nv in G[v]:
        if nv==pv:
            continue
        
        if finished[nv]:
            continue
        
        if seen[nv] and not finished[nv]:
            pos = nv
            return
        
        dfs(nv, v)
        
        if pos!=-1:
            return
    
    hist.pop()
    finished[v] = True
N = int(input())
G = [[] for _ in range(N)]
edges = defaultdict(int)
for i in range(N):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
    edges[(min(a-1, b-1), max(a-1, b-1))] = i
pos = -1
seen = [False]*N
finished = [False]*N
hist = []
dfs(0, -1)
cycle = []
while hist:
    cycle.append(hist.pop())
    
    if cycle[-1]==pos:
        break
    
ans = []
for i in range(len(cycle)):
    u, v = cycle[i], cycle[(i+1)%len(cycle)]
    u, v = min(u, v), max(u, v)
    ans.append(edges[(u, v)]+1)
    
print(len(ans))
print(*ans)
            
            
            
        