結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー mkawa2mkawa2
提出日時 2023-08-04 22:31:45
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,981 bytes
コンパイル時間 259 ms
コンパイル使用メモリ 82,584 KB
実行使用メモリ 92,864 KB
最終ジャッジ日時 2024-04-22 20:48:40
合計ジャッジ時間 4,667 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,556 KB
testcase_01 AC 37 ms
52,872 KB
testcase_02 AC 37 ms
54,112 KB
testcase_03 AC 36 ms
52,992 KB
testcase_04 AC 146 ms
77,760 KB
testcase_05 AC 211 ms
78,576 KB
testcase_06 AC 224 ms
79,232 KB
testcase_07 AC 169 ms
77,696 KB
testcase_08 AC 187 ms
78,592 KB
testcase_09 AC 227 ms
79,456 KB
testcase_10 AC 216 ms
78,732 KB
testcase_11 AC 192 ms
78,528 KB
testcase_12 AC 220 ms
78,456 KB
testcase_13 AC 192 ms
78,956 KB
testcase_14 AC 36 ms
52,736 KB
testcase_15 AC 37 ms
52,352 KB
testcase_16 WA -
testcase_17 AC 38 ms
52,608 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(1000005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = -1-(-1 << 63)
# md = 10**9+7
md = 998244353

class UnionFind:
    def __init__(self, n):
        self.table = [-1]*n
        self.mem = [[u] for u in range(n)]
        self.cnt = n

    def root(self, u):
        stack = []
        while self.table[u] >= 0:
            stack.append(u)
            u = self.table[u]
        for v in stack: self.table[v] = u
        return u

    def same(self, u, v):
        return self.root(u) == self.root(v)

    def merge(self, u, v):
        u = self.root(u)
        v = self.root(v)
        if u == v: return False
        su = -self.table[u]
        sv = -self.table[v]
        if su < sv: u, v = v, u
        self.table[u] = -su-sv
        self.table[v] = u
        self.mem[u] += self.mem[v]
        self.mem[v].clear()
        self.cnt -= 1
        return True

    def member(self,u):
        return self.mem[self.root(u)]

    # グループの要素数
    def size(self, u):
        return -self.table[self.root(u)]

n,m=LI()
deg=[0]*n
uf=UnionFind(n)
for _ in range(m):
    u,v=LI1()
    deg[u]-=1
    deg[v]+=1
    uf.merge(u,v)
# print(deg)

ans=0
for uu in uf.mem:
    if len(uu)==1:continue
    cur=0
    for u in uu:
        if deg[u]>0:cur+=deg[u]
    if cur:cur-=1
    ans+=cur
    # print(uu,ans)

print(max(ans,uf.cnt-1))
0