結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー cleanttedcleantted
提出日時 2023-08-04 22:25:31
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,686 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 137,812 KB
最終ジャッジ日時 2024-10-14 20:25:04
合計ジャッジ時間 14,113 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 173 ms
91,136 KB
testcase_01 WA -
testcase_02 AC 174 ms
90,752 KB
testcase_03 AC 174 ms
91,008 KB
testcase_04 AC 458 ms
111,312 KB
testcase_05 WA -
testcase_06 AC 861 ms
133,304 KB
testcase_07 AC 552 ms
116,684 KB
testcase_08 AC 645 ms
120,704 KB
testcase_09 AC 901 ms
136,448 KB
testcase_10 AC 829 ms
137,728 KB
testcase_11 AC 720 ms
123,904 KB
testcase_12 AC 888 ms
137,812 KB
testcase_13 AC 661 ms
120,448 KB
testcase_14 AC 178 ms
91,044 KB
testcase_15 AC 173 ms
90,880 KB
testcase_16 AC 174 ms
90,624 KB
testcase_17 AC 172 ms
91,020 KB
testcase_18 AC 178 ms
90,880 KB
testcase_19 AC 209 ms
105,936 KB
testcase_20 AC 180 ms
91,488 KB
testcase_21 AC 179 ms
92,416 KB
testcase_22 AC 204 ms
105,276 KB
testcase_23 AC 207 ms
104,888 KB
testcase_24 AC 208 ms
103,784 KB
testcase_25 AC 189 ms
93,696 KB
testcase_26 AC 177 ms
90,992 KB
testcase_27 AC 224 ms
106,240 KB
testcase_28 AC 223 ms
104,832 KB
testcase_29 AC 209 ms
96,384 KB
testcase_30 AC 186 ms
92,416 KB
testcase_31 AC 223 ms
106,368 KB
testcase_32 AC 202 ms
93,696 KB
testcase_33 AC 188 ms
92,032 KB
testcase_34 AC 211 ms
95,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# import bisect
import copy
import heapq
import itertools
import math
import operator
import random
import sys
from bisect import bisect, bisect_left, bisect_right, insort
from collections import Counter, deque
from fractions import Fraction
from functools import cmp_to_key, lru_cache, partial
from inspect import currentframe
from math import ceil, gcd, log10, pi, sqrt
from typing import Iterable, Iterator, List, Tuple, TypeVar, Union

# import pypyjit
# pypyjit.set_param('max_unroll_recursion=-1')
# import string
# import networkx as nx
input = sys.stdin.readline
sys.setrecursionlimit(10000000)
# mod = 10 ** 9 + 7
mod = 998244353
# mod = 1 << 128
# mod = 10 ** 30 + 1
INF = 1 << 61
DIFF = 10 ** -9
DX = [1, 0, -1, 0, 1, 1, -1, -1]
DY = [0, 1, 0, -1, 1, -1, 1, -1]

def read_values(): return tuple(map(int, input().split()))
def read_index(): return tuple(map(lambda x: int(x) - 1, input().split()))
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for _ in range(N)]
def dprint(*values): print(*values, file=sys.stderr)
def dprint2(*values):
    names = {id(v): k for k, v in currentframe().f_back.f_locals.items()}
    dprint(", ".join(f"{names.get(id(value), '???')}={repr(value)}" for value in values))



# https://tjkendev.github.io/procon-library/python/graph/scc.html
# 強連結成分分解(SCC): グラフGに対するSCCを行う
# 入力: <N>: 頂点サイズ, <G>: 順方向の有向グラフ, <RG>: 逆方向の有向グラフ
# 出力: (<ラベル数>, <各頂点のラベル番号>)
def scc(N, G, RG):
    order = []
    used = [0]*N
    group = [None]*N
    def dfs(s):
        used[s] = 1
        for t in G[s]:
            if not used[t]:
                dfs(t)
        order.append(s)
    def rdfs(s, col):
        group[s] = col
        used[s] = 1
        for t in RG[s]:
            if not used[t]:
                rdfs(t, col)
    for i in range(N):
        if not used[i]:
            dfs(i)
    used = [0]*N
    label = 0
    for s in reversed(order):
        if not used[s]:
            rdfs(s, label)
            label += 1
    return label, group

# 縮約後のグラフを構築
def construct(N, G, label, group):
    G0 = [set() for i in range(label)]
    GP = [[] for i in range(label)]
    for v in range(N):
        lbs = group[v]
        for w in G[v]:
            lbt = group[w]
            if lbs == lbt:
                continue
            G0[lbs].add(lbt)
        GP[lbs].append(v)
    return G0, GP


def main():
    N, M = read_values()
    L = []
    G = [set() for _ in range(N)]
    GR = [set() for _ in range(N)]

    for i in range(M):
        u, v = read_index()
        L.append((u, v))
        G[u].add((v, i))
        GR[v].add((u, i))

    T = [0] * N
    F = [False] * M
    for i in range(M):
        if F[i]:
            continue
        
        F[i] = True
        u0, v0 = L[i]
        G[u0].discard((v0, i))
        GR[v0].discard((u0, i))

        u = v0
        while True:
            for v, k in G[u]:
                if F[k]:
                    continue
                F[k] = True
                break
            else:
                break
            G[u].discard((v, k))
            GR[v].discard((u, k))
            u = v
        u1 = u
        
        v = u0
        while True:
            for u, k in GR[v]:
                if F[k]:
                    continue
                F[k] = True
                break
            else:
                break
            GR[v].discard((u, k))
            G[u].discard((v, k))
            v = u
        v1 = v

        T[u1] += 1
        T[v1] += 1


    print(sum(T) // 2 - 1)

    



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