結果

問題 No.2301 Namorientation
ユーザー ShirotsumeShirotsume
提出日時 2023-05-09 00:38:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 418 ms / 3,000 ms
コード長 2,160 bytes
コンパイル時間 349 ms
コンパイル使用メモリ 82,472 KB
実行使用メモリ 143,280 KB
最終ジャッジ日時 2024-05-04 06:04:10
合計ジャッジ時間 16,065 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
66,836 KB
testcase_01 AC 66 ms
67,468 KB
testcase_02 AC 65 ms
68,856 KB
testcase_03 AC 68 ms
68,276 KB
testcase_04 AC 66 ms
67,940 KB
testcase_05 AC 67 ms
68,228 KB
testcase_06 AC 68 ms
67,708 KB
testcase_07 AC 69 ms
67,548 KB
testcase_08 AC 69 ms
67,780 KB
testcase_09 AC 69 ms
68,524 KB
testcase_10 AC 66 ms
67,804 KB
testcase_11 AC 75 ms
67,936 KB
testcase_12 AC 349 ms
110,936 KB
testcase_13 AC 267 ms
106,676 KB
testcase_14 AC 405 ms
136,320 KB
testcase_15 AC 302 ms
115,700 KB
testcase_16 AC 354 ms
125,328 KB
testcase_17 AC 298 ms
110,804 KB
testcase_18 AC 366 ms
128,128 KB
testcase_19 AC 246 ms
106,568 KB
testcase_20 AC 354 ms
127,684 KB
testcase_21 AC 304 ms
114,964 KB
testcase_22 AC 263 ms
143,280 KB
testcase_23 AC 257 ms
143,024 KB
testcase_24 AC 230 ms
130,044 KB
testcase_25 AC 220 ms
118,040 KB
testcase_26 AC 257 ms
129,952 KB
testcase_27 AC 413 ms
131,164 KB
testcase_28 AC 392 ms
132,336 KB
testcase_29 AC 399 ms
132,528 KB
testcase_30 AC 408 ms
134,092 KB
testcase_31 AC 418 ms
133,844 KB
権限があれば一括ダウンロードができます

ソースコード

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(i, u - 1)
    G.add_edge(i, v - 1)

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

0