結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー mono_0812mono_0812
提出日時 2023-08-04 22:38:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 375 ms / 2,000 ms
コード長 3,350 bytes
コンパイル時間 389 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 142,080 KB
最終ジャッジ日時 2024-05-05 01:26:57
合計ジャッジ時間 9,923 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 145 ms
93,184 KB
testcase_01 AC 149 ms
93,184 KB
testcase_02 AC 144 ms
93,184 KB
testcase_03 AC 153 ms
93,056 KB
testcase_04 AC 292 ms
94,464 KB
testcase_05 AC 348 ms
94,336 KB
testcase_06 AC 370 ms
94,592 KB
testcase_07 AC 322 ms
94,336 KB
testcase_08 AC 333 ms
94,208 KB
testcase_09 AC 375 ms
94,464 KB
testcase_10 AC 361 ms
94,208 KB
testcase_11 AC 340 ms
94,464 KB
testcase_12 AC 369 ms
94,336 KB
testcase_13 AC 334 ms
94,336 KB
testcase_14 AC 153 ms
93,056 KB
testcase_15 AC 147 ms
92,928 KB
testcase_16 AC 149 ms
93,056 KB
testcase_17 AC 158 ms
92,928 KB
testcase_18 AC 153 ms
93,184 KB
testcase_19 AC 237 ms
141,968 KB
testcase_20 AC 183 ms
98,688 KB
testcase_21 AC 205 ms
118,400 KB
testcase_22 AC 237 ms
136,060 KB
testcase_23 AC 215 ms
132,776 KB
testcase_24 AC 222 ms
127,824 KB
testcase_25 AC 191 ms
125,568 KB
testcase_26 AC 156 ms
92,928 KB
testcase_27 AC 257 ms
141,568 KB
testcase_28 AC 247 ms
131,324 KB
testcase_29 AC 205 ms
98,560 KB
testcase_30 AC 201 ms
111,616 KB
testcase_31 AC 277 ms
142,080 KB
testcase_32 AC 200 ms
94,336 KB
testcase_33 AC 191 ms
106,232 KB
testcase_34 AC 206 ms
96,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

############################################################################################
import bisect,collections,copy,heapq,itertools,math,string,sys,queue,time,random
from decimal import Decimal
def I(): return input()
def IS(): return input().split()
def II(): return int(input())
def IIS(): return list(map(int,input().split()))
def LIIS(): return list(map(int,input().split()))

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

import math

def prime_numbers(n):
    prime = [True for i in range(n+1)]
    prime[0] = False
    prime[1] = False

    sqrt_n = math.ceil(math.sqrt(n))
    for i in range(2, sqrt_n):
        if prime[i]:
            for j in range(2*i, n+1, i):
                prime[j] = False
    numbers=[]
    for i in range(2,n+1):
        if prime[i]:
            numbers.append(i)

    return numbers

def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
 
    if temp!=1:
        arr.append([temp, 1])
 
    if arr==[]:
        arr.append([n, 1])
 
    return arr
 
INF=float("inf")
MOD=998244353
MOD2=10**9+7
#sys.setrecursionlimit(500005)
def bit_count(x):
    return bin(x).count("1")
def yesno(f):
    if f:print("Yes")
    else:print("No")
####################################################

from collections import defaultdict


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

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())
n,m=IIS()
li=[0 for i in range(n)]
uf=UnionFind(n)
dic=collections.defaultdict(int)
for _ in range(m):
    u,v=IIS()
    li[u-1]+=1
    li[v-1]-=1
    uf.union(u-1,v-1)
    dic[uf.find(u-1)]+=1
ans=0
cnt2=0
s=uf.all_group_members()
for i in s:
    cnt=0
    for j in s[i]:
        cnt+=abs(li[j])
    if dic[uf.find(i)]:
        cnt2+=1
        ans+=max(cnt//2-1,0)
print(ans+max(cnt2-1,0))
0