結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー flygonflygon
提出日時 2023-08-04 22:30:55
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,449 bytes
コンパイル時間 283 ms
コンパイル使用メモリ 82,392 KB
実行使用メモリ 113,664 KB
最終ジャッジ日時 2024-04-22 20:47:51
合計ジャッジ時間 5,772 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,040 KB
testcase_01 AC 45 ms
55,040 KB
testcase_02 AC 41 ms
54,784 KB
testcase_03 AC 43 ms
54,912 KB
testcase_04 AC 163 ms
79,044 KB
testcase_05 AC 242 ms
81,664 KB
testcase_06 AC 276 ms
83,456 KB
testcase_07 AC 175 ms
80,128 KB
testcase_08 AC 233 ms
81,236 KB
testcase_09 AC 271 ms
82,944 KB
testcase_10 AC 234 ms
82,140 KB
testcase_11 AC 219 ms
81,536 KB
testcase_12 AC 252 ms
82,572 KB
testcase_13 AC 238 ms
81,168 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 112 ms
113,664 KB
testcase_20 AC 61 ms
73,728 KB
testcase_21 AC 96 ms
102,784 KB
testcase_22 AC 110 ms
109,168 KB
testcase_23 AC 105 ms
106,496 KB
testcase_24 AC 108 ms
102,608 KB
testcase_25 AC 102 ms
106,492 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 123 ms
104,024 KB
testcase_29 WA -
testcase_30 AC 98 ms
97,024 KB
testcase_31 AC 128 ms
110,296 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(5*10**5)
input = sys.stdin.readline
from collections import defaultdict, deque, Counter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right
from math import gcd

from collections import defaultdict


class UnionFind:
    def __init__(self, n):
        self.n = n
        self.p = [-1] * (n+1)

    def find(self, x):
        if self.p[x] < 0:
            return x
        else:
            self.p[x] = self.find(self.p[x])
            return self.p[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.p[x] > self.p[y]:
            x, y = y, x
        self.p[x] += self.p[y]
        self.p[y] = x

    def same(self, a, b):
        return self.find(a) == self.find(b)

    def group(self):
        d = defaultdict(list)
        for i in range(1, self.n+1):
            par = self.find(i)
            d[par].append(i)
        return d


n,m = map(int,input().split())
graph = [[] for i in range(n+1)]
uf = UnionFind(n)
indeg = [0]*(n+1)
outdeg = [0]*(n+1)
for i in range(m):
    a,b = map(int,input().split())
    graph[a].append(b)
    uf.union(a,b)
    indeg[b] += 1
    outdeg[a] += 1

g = uf.group()

ans = 0
gg = 0
for k,v in g.items():
    cnt = 0
    for i in v:
        if indeg[i] >= outdeg[i]:
            cnt += indeg[i] - outdeg[i]
    ans += max(cnt-1, 0)
    gg += cnt >= 1

print(ans + gg-1)
0