結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 175 ms
90,880 KB
testcase_01 WA -
testcase_02 AC 175 ms
90,880 KB
testcase_03 AC 172 ms
90,880 KB
testcase_04 AC 458 ms
111,092 KB
testcase_05 WA -
testcase_06 AC 851 ms
133,504 KB
testcase_07 AC 548 ms
116,344 KB
testcase_08 AC 655 ms
120,600 KB
testcase_09 AC 907 ms
136,448 KB
testcase_10 AC 851 ms
137,276 KB
testcase_11 AC 702 ms
123,836 KB
testcase_12 AC 909 ms
137,728 KB
testcase_13 AC 639 ms
120,400 KB
testcase_14 AC 168 ms
90,732 KB
testcase_15 AC 164 ms
90,980 KB
testcase_16 AC 166 ms
90,880 KB
testcase_17 AC 167 ms
90,612 KB
testcase_18 AC 170 ms
90,856 KB
testcase_19 AC 199 ms
105,984 KB
testcase_20 AC 174 ms
91,292 KB
testcase_21 AC 179 ms
92,288 KB
testcase_22 AC 208 ms
104,940 KB
testcase_23 AC 216 ms
104,796 KB
testcase_24 AC 214 ms
103,628 KB
testcase_25 AC 185 ms
93,560 KB
testcase_26 AC 178 ms
90,880 KB
testcase_27 AC 252 ms
105,984 KB
testcase_28 AC 222 ms
104,448 KB
testcase_29 AC 206 ms
96,288 KB
testcase_30 AC 185 ms
92,396 KB
testcase_31 AC 219 ms
106,336 KB
testcase_32 AC 202 ms
93,696 KB
testcase_33 AC 186 ms
92,232 KB
testcase_34 AC 211 ms
95,552 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