結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー ニックネームニックネーム
提出日時 2023-08-04 22:07:59
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 658 bytes
コンパイル時間 187 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 32,108 KB
最終ジャッジ日時 2024-04-22 20:25:16
合計ジャッジ時間 8,811 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
10,752 KB
testcase_01 AC 27 ms
10,752 KB
testcase_02 AC 28 ms
10,624 KB
testcase_03 AC 27 ms
10,752 KB
testcase_04 AC 337 ms
11,008 KB
testcase_05 AC 605 ms
11,264 KB
testcase_06 AC 643 ms
11,392 KB
testcase_07 AC 399 ms
11,008 KB
testcase_08 AC 471 ms
11,136 KB
testcase_09 AC 693 ms
11,392 KB
testcase_10 AC 676 ms
11,008 KB
testcase_11 AC 508 ms
11,264 KB
testcase_12 AC 697 ms
11,264 KB
testcase_13 AC 475 ms
11,264 KB
testcase_14 AC 27 ms
10,752 KB
testcase_15 AC 27 ms
10,752 KB
testcase_16 AC 25 ms
10,752 KB
testcase_17 AC 26 ms
10,752 KB
testcase_18 AC 26 ms
10,752 KB
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 #

class UF:
    def __init__(self,n):
        self.p = [-1]*n
    def f(self,x):
        if self.p[x]<0: return x
        else: self.p[x] = self.f(self.p[x]); return self.p[x]
    def u(self,x,y):
        x = self.f(x); y = self.f(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
n,m = map(int,input().split())
a = [0]*n; b = [0]*n; c = []; uf = UF(n)
for _ in range(m):
    u,v = map(int,input().split())
    a[u-1] += 1; b[v-1] += 1; uf.u(u-1,v-1)
ans = len(set(uf.f(i) for i in range(n)))-1
for u,v in zip(a,b):
    if u>v: c.append(u-v)
    if u==v==0: ans -= 1
print(ans+max(sum(c)-1,0))
0