結果
問題 | No.2403 "Eight" Bridges of Königsberg |
ユーザー | 👑 獅子座じゃない人 |
提出日時 | 2023-07-13 21:51:32 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 489 ms / 2,000 ms |
コード長 | 2,219 bytes |
コンパイル時間 | 202 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 173,100 KB |
最終ジャッジ日時 | 2024-05-05 01:22:22 |
合計ジャッジ時間 | 7,302 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 37 ms
52,608 KB |
testcase_01 | AC | 38 ms
52,992 KB |
testcase_02 | AC | 40 ms
52,352 KB |
testcase_03 | AC | 40 ms
52,864 KB |
testcase_04 | AC | 284 ms
120,516 KB |
testcase_05 | AC | 442 ms
158,232 KB |
testcase_06 | AC | 470 ms
162,832 KB |
testcase_07 | AC | 341 ms
132,100 KB |
testcase_08 | AC | 366 ms
139,296 KB |
testcase_09 | AC | 489 ms
170,384 KB |
testcase_10 | AC | 449 ms
170,312 KB |
testcase_11 | AC | 390 ms
145,692 KB |
testcase_12 | AC | 471 ms
173,100 KB |
testcase_13 | AC | 369 ms
139,408 KB |
testcase_14 | AC | 37 ms
52,736 KB |
testcase_15 | AC | 37 ms
52,864 KB |
testcase_16 | AC | 37 ms
52,352 KB |
testcase_17 | AC | 37 ms
52,352 KB |
testcase_18 | AC | 37 ms
52,864 KB |
testcase_19 | AC | 107 ms
110,720 KB |
testcase_20 | AC | 61 ms
70,656 KB |
testcase_21 | AC | 70 ms
82,048 KB |
testcase_22 | AC | 103 ms
107,904 KB |
testcase_23 | AC | 100 ms
106,240 KB |
testcase_24 | AC | 104 ms
100,552 KB |
testcase_25 | AC | 95 ms
99,712 KB |
testcase_26 | AC | 42 ms
54,912 KB |
testcase_27 | AC | 147 ms
110,472 KB |
testcase_28 | AC | 118 ms
90,368 KB |
testcase_29 | AC | 102 ms
78,708 KB |
testcase_30 | AC | 93 ms
89,404 KB |
testcase_31 | AC | 118 ms
94,612 KB |
testcase_32 | AC | 103 ms
76,888 KB |
testcase_33 | AC | 92 ms
85,888 KB |
testcase_34 | AC | 107 ms
78,452 KB |
ソースコード
# https://github.com/shakayami/ACL-for-python/blob/master/dsu.py class dsu(): n=1 parent_or_size=[-1 for i in range(n)] def __init__(self,N): self.n=N self.parent_or_size=[-1 for i in range(N)] def merge(self,a,b): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n) x=self.leader(a) y=self.leader(b) if x==y: return x if (-self.parent_or_size[x]<-self.parent_or_size[y]): x,y=y,x self.parent_or_size[x]+=self.parent_or_size[y] self.parent_or_size[y]=x return x def same(self,a,b): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n) return self.leader(a)==self.leader(b) def leader(self,a): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) if (self.parent_or_size[a]<0): return a self.parent_or_size[a]=self.leader(self.parent_or_size[a]) return self.parent_or_size[a] def size(self,a): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) return -self.parent_or_size[self.leader(a)] def groups(self): leader_buf=[0 for i in range(self.n)] group_size=[0 for i in range(self.n)] for i in range(self.n): leader_buf[i]=self.leader(i) group_size[leader_buf[i]]+=1 result=[[] for i in range(self.n)] for i in range(self.n): result[leader_buf[i]].append(i) result2=[] for i in range(self.n): if len(result[i])>0: result2.append(result[i]) return result2 n,m=map(int,input().split()) u,v=[list(e) for e in zip(*[map(lambda s: int(s)-1, input().split()) for i in range(m)])] g=dsu(n) outdeg=[0 for i in range(n)] indeg=[0 for i in range(n)] for i in range(m): g.merge(u[i],v[i]) outdeg[u[i]]+=1 indeg[v[i]]+=1 ccs=g.groups() ans=-1 for cc in ccs: tmp=0 ok=False for p in cc: if indeg[p]>0: ok=True tmp+=abs(outdeg[p]-indeg[p]) tmp//=2 ans+=max(tmp-1,0)+ok print(ans)