結果

問題 No.2301 Namorientation
ユーザー 👑 rin204rin204
提出日時 2024-03-16 16:04:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 775 ms / 3,000 ms
コード長 1,170 bytes
コンパイル時間 225 ms
コンパイル使用メモリ 81,928 KB
実行使用メモリ 155,640 KB
最終ジャッジ日時 2024-09-30 04:05:02
合計ジャッジ時間 19,388 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
deg = [0] * n
edges = [[] for _ in range(n)]
E = {}
for i in range(n):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    E[(u, v)] = i
    edges[u].append(v)
    edges[v].append(u)
    deg[u] += 1
    deg[v] += 1

ans = [""] * n
st = []
for i in range(n):
    if deg[i] == 1:
        st.append(i)

while st:
    pos = st.pop()
    for npos in edges[pos]:
        if deg[npos] == 0:
            continue

        if (pos, npos) in E:
            ans[E[(pos, npos)]] = "->"
        else:
            ans[E[(npos, pos)]] = "<-"

        deg[pos] -= 1
        deg[npos] -= 1

        if deg[npos] == 1:
            st.append(npos)

u = -1
for i in range(n):
    if deg[i] > 0:
        u = i
        deg[i] -= 1
        break

cycle = [u]
while 1:
    for v in edges[u]:
        if deg[v] > 0:
            cycle.append(v)
            deg[u] -= 1
            deg[v] -= 1
            u = v
            break
    else:
        break

for i in range(len(cycle)):
    pos = cycle[i]
    npos = cycle[(i + 1) % len(cycle)]
    if (pos, npos) in E:
        ans[E[(pos, npos)]] = "->"
    else:
        ans[E[(npos, pos)]] = "<-"

print(*ans, sep="\n")
0