結果

問題 No.2301 Namorientation
ユーザー ShirotsumeShirotsume
提出日時 2023-05-09 00:36:58
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,161 bytes
コンパイル時間 150 ms
コンパイル使用メモリ 82,268 KB
実行使用メモリ 158,448 KB
最終ジャッジ日時 2024-05-04 06:02:23
合計ジャッジ時間 17,190 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
68,920 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 59 ms
68,612 KB
testcase_08 AC 57 ms
67,752 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 59 ms
67,432 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 254 ms
158,448 KB
testcase_23 AC 252 ms
157,792 KB
testcase_24 AC 221 ms
142,212 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque, Counter
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
inf = 2 ** 63 - 1
mod = 998244353

from typing import List, Tuple
 
class BipartiteMatching:
    '''二部マッチング問題のソルバー。
    https://snuke.hatenablog.com/entry/2019/05/07/013609 のコードをPythonにベタ移植したもの。
    snuke is God.
    '''
 
    def __init__(self, n: int, m: int) -> None:
        self._n = n
        self._m = m
        self._to: List[List[int]] = [[] for _ in range(n)]
 
    def add_edge(self, a: int, b: int) -> None:
        self._to[a].append(b)
 
    def solve(self) -> List[Tuple[int, int]]:
        n, m, to = self._n, self._m, self._to
        pre = [-1] * n
        root = [-1] * n
        p = [-1] * n
        q = [-1] * m
        upd = True
        while upd:
            upd = False
            s = []
            s_front = 0
            for i in range(n):
                if p[i] == -1:
                    root[i] = i
                    s.append(i)
            while s_front < len(s):
                v = s[s_front]
                s_front += 1
                if p[root[v]] != -1: continue
                for u in to[v]:
                    if q[u] == -1:
                        while u != -1:
                            q[u] = v
                            p[v], u = u, p[v]
                            v = pre[v]
                        upd = True
                        break
                    u = q[u]
                    if pre[u] != -1: continue
                    pre[u] = v
                    root[u] = root[v]
                    s.append(u)
            if upd:
                for i in range(n):
                    pre[i] = -1
                    root[i] = -1
        return [(v, p[v]) for v in range(n) if p[v] != -1]


n = ii()

G = BipartiteMatching(n, n)
e = []
for i in range(n):
    u, v = mi()
    e.append(u - 1)
    G.add_edge(u - 1, i)
    G.add_edge(v - 1, i)

E = G.solve()

for i in range(n):
    if E[i][1] == e[i]:
        print('->')
    else:
        print('<-')

0