結果

問題 No.3390 Public or Private
コンテスト
ユーザー LyricalMaestro
提出日時 2025-11-29 12:16:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 412 ms / 2,000 ms
コード長 1,398 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 178 ms
コンパイル使用メモリ 82,320 KB
実行使用メモリ 149,048 KB
最終ジャッジ日時 2025-11-29 12:16:51
合計ジャッジ時間 12,654 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

# https://yukicoder.me/problems/no/3390




def main():
    N, M = map(int, input().split())
    next_nodes = {}
    for _ in range(M):
        u, v = map(int, input().split())
        u -= 1
        v -= 1
        if u not in next_nodes:
            next_nodes[u] = set()
        next_nodes[u].add(v)
    
    Q = int(input())
    queries = []
    for _ in range(Q):
        values = tuple(map(int, input().split()))
        queries.append(values)

    private = set()
    for q, a, b in queries:
        if q == 2:
            a -= 1
            if a in private:
                private.remove(a)
            else:
                private.add(a)
        elif q == 1:
            a -= 1
            b -= 1
            if a in next_nodes and b in next_nodes[a]:
                next_nodes[a].remove(b)
            else:
                if a not in next_nodes:
                    next_nodes[a] = set()
                next_nodes[a].add(b)
            
        n = N - len(private)
        if a in next_nodes:
            lens = len(next_nodes[a])
            pubs = 0
            for b in next_nodes[a]:
                if b not in private:
                    pubs += 1
            lens -= pubs
        else:
            lens = 0

        if a not in private:
            ans = lens + n - 1
        else:
            ans = lens + n
        print(ans)









if __name__ == "__main__":
    main()
0