結果

問題 No.2301 Namorientation
ユーザー i_takui_taku
提出日時 2023-05-18 10:06:14
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,079 bytes
コンパイル時間 243 ms
コンパイル使用メモリ 82,572 KB
実行使用メモリ 341,988 KB
最終ジャッジ日時 2024-05-09 13:03:09
合計ジャッジ時間 20,600 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,888 KB
testcase_01 AC 41 ms
53,376 KB
testcase_02 AC 41 ms
54,400 KB
testcase_03 AC 41 ms
53,888 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 41 ms
53,120 KB
testcase_08 AC 40 ms
54,016 KB
testcase_09 AC 40 ms
53,632 KB
testcase_10 AC 42 ms
53,888 KB
testcase_11 AC 41 ms
53,376 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 862 ms
341,988 KB
testcase_23 AC 844 ms
338,796 KB
testcase_24 AC 713 ms
294,448 KB
testcase_25 AC 225 ms
109,244 KB
testcase_26 AC 272 ms
118,512 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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


def main():
    N = int(input())
    AB = []
    g = [[] 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].append(b)
        g[b].append(a)

    def dfs(u, prev, target=-1):
        for v in g[u]:
            if v in (prev, target):
                continue
            if distance[v] == INF:
                distance[v] = distance[u] + 1
                dfs(v, u)
            elif distance[v] < distance[u]:
                is_loop[u] = is_loop[v] = True
                if target == -1:
                    target = v
                dfs(prev, u, target)

    distance = [INF] * N
    distance[0] = 0
    is_loop = [False] * N
    dfs(0, -1)

    def bfs(u):
        que = deque([u])
        while que:
            u = que.popleft()
            for v in g[u]:
                if is_loop[v]:
                    continue
                if distance[v] == INF:
                    distance[v] = distance[u] + 1
                    que.append(v)

    distance = [INF] * N
    # 端点からbfs
    for i in range(N):
        if len(g[i]) == 1:
            distance[i] = 0
            bfs(i)

    def dfs2(u, prev=-1):
        for v in g[u]:
            if v == prev:
                continue
            if distance[v] == INF:
                distance[v] = distance[u] + 1
                dfs2(v, u)

    # 残った点からdfs
    for i in range(N):
        if distance[i] == INF:
            # 閉路判断用にバカでか初期値
            distance[i] = 1 << 18
            dfs2(i)

    # 答え出力
    for a, b in AB:
        if is_loop[a] and is_loop[b]:
            if distance[b] - distance[a] == 1 or distance[a] - distance[b] > 1:
                print('->')
            else:
                print('<-')
        else:
            if distance[a] < distance[b]:
                print('->')
            else:
                print('<-')


main()
0