結果

問題 No.2301 Namorientation
ユーザー i_takui_taku
提出日時 2023-05-18 11:02:25
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,456 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 342,916 KB
最終ジャッジ日時 2024-05-09 13:31:24
合計ジャッジ時間 19,789 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
53,888 KB
testcase_01 AC 40 ms
54,144 KB
testcase_02 WA -
testcase_03 AC 47 ms
53,632 KB
testcase_04 WA -
testcase_05 AC 40 ms
53,888 KB
testcase_06 AC 44 ms
54,272 KB
testcase_07 WA -
testcase_08 AC 39 ms
54,400 KB
testcase_09 AC 41 ms
53,760 KB
testcase_10 AC 50 ms
54,144 KB
testcase_11 AC 46 ms
53,888 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 610 ms
342,916 KB
testcase_23 AC 557 ms
323,148 KB
testcase_24 AC 482 ms
288,852 KB
testcase_25 AC 235 ms
141,568 KB
testcase_26 AC 297 ms
164,432 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import deque
INF = float('inf')
sys.setrecursionlimit(10**6)


def main():
    N = int(input())
    AB = []
    g = [set() for _ in range(N)]
    for _ in range(N):
        a, b = map(int, input().split())
        a, b = a - 1, b - 1
        AB.append((a, b))
        g[a].add(b)
        g[b].add(a)

    depth = [INF] * N

    # トポロジカルソート風
    for i in range(N):
        if len(g[i]) == 1:
            que = deque([i])
            depth[i] = 0
            while que:
                u = que.popleft()
                for v in g[u]:
                    g[v].discard(u)
                    if len(g[v]) == 1:
                        que.append(v)
                        depth[v] = depth[u] + 1

    def dfs(u):
        is_loop[u] = True
        for v in g[u]:
            if depth[v] == INF:
                depth[v] = depth[u] + 1
                dfs(v)
    
    # 残った閉路を処理する
    is_loop = [False] * N
    for i in range(N):
        if depth[i] == INF:
            depth[i] = 1 << 18
            dfs(i)
    
    # 答え出力
    for a, b in AB:
        if is_loop[a] and is_loop[b]:
            if depth[b] - depth[a] == 1 or depth[a] - depth[b] > 1:
                print('->')
            else:
                print('<-')
        else:
            if depth[a] < depth[b]:
                print('->')
            else:
                print('<-')


main()
0