結果

問題 No.3508 OR Mapping
コンテスト
ユーザー Ian
提出日時 2026-04-19 18:26:00
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
WA  
実行時間 -
コード長 2,555 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 485 ms
コンパイル使用メモリ 20,572 KB
実行使用メモリ 235,048 KB
最終ジャッジ日時 2026-04-19 18:26:58
合計ジャッジ時間 50,546 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 52 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
from collections import deque

def main():
    data = sys.stdin.buffer.read().split()
    p = 0
    N = int(data[p]); p += 1
    M = int(data[p]); p += 1
    K = int(data[p]); p += 1
    
    adj = [[] for _ in range(N+1)]
    radj = [[] for _ in range(N+1)]
    for _ in range(M):
        u = int(data[p]); p += 1
        v = int(data[p]); p += 1
        adj[u].append(v)
        radj[v].append(u)
    
    # Reachable from 1
    R = bytearray(N+1)
    R[1] = 1
    stk = [1]
    while stk:
        u = stk.pop()
        for v in adj[u]:
            if not R[v]:
                R[v] = 1
                stk.append(v)
    
    # Kosaraju forward DFS from 1 (visits exactly R)
    order = []
    visited = bytearray(N+1)
    visited[1] = 1
    snode = [1]
    siter = [0]
    while snode:
        u = snode[-1]
        au = adj[u]
        i = siter[-1]
        la = len(au)
        pushed = False
        while i < la:
            v = au[i]
            i += 1
            if not visited[v]:
                visited[v] = 1
                siter[-1] = i
                snode.append(v)
                siter.append(0)
                pushed = True
                break
        if not pushed:
            order.append(u)
            snode.pop()
            siter.pop()
    
    comp = [0]*(N+1)
    c = 0
    for u in reversed(order):
        if comp[u]: continue
        c += 1
        comp[u] = c
        st = [u]
        while st:
            x = st.pop()
            for y in radj[x]:
                if R[y] and not comp[y]:
                    comp[y] = c
                    st.append(y)
    
    num_scc = c
    
    # Chain check
    has_next = bytearray(num_scc + 2)
    for u in range(1, N+1):
        if not R[u]: continue
        cu = comp[u]
        if has_next[cu]: continue
        target = cu + 1
        for v in adj[u]:
            if comp[v] == target:
                has_next[cu] = 1
                break
    
    for i in range(1, num_scc):
        if not has_next[i]:
            print("No")
            return
    
    # Odd cycle in SCC(1) via parity BFS
    scc1 = comp[1]
    color = [-1]*(N+1)
    color[1] = 0
    q = deque([1])
    odd = False
    while q:
        u = q.popleft()
        cc = color[u]
        nc = 1 - cc
        for v in adj[u]:
            if comp[v] != scc1:
                continue
            cv = color[v]
            if cv == -1:
                color[v] = nc
                q.append(v)
            elif cv == cc:
                odd = True
    
    print("Yes" if odd else "No")

main()
0