結果
問題 | No.1293 2種類の道路 |
ユーザー |
👑 ![]() |
提出日時 | 2020-11-20 21:58:14 |
言語 | PyPy3 (7.9.16) |
結果 |
AC
|
実行時間 | 624 ms / 2,000 ms |
コード長 | 2,467 bytes |
コンパイル時間 | 274 ms |
実行使用メモリ | 113,916 KB |
最終ジャッジ日時 | 2023-02-23 18:18:39 |
合計ジャッジ時間 | 10,033 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge15 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 84 ms
75,752 KB |
testcase_01 | AC | 84 ms
75,720 KB |
testcase_02 | AC | 86 ms
75,528 KB |
testcase_03 | AC | 84 ms
75,668 KB |
testcase_04 | AC | 86 ms
75,656 KB |
testcase_05 | AC | 85 ms
75,768 KB |
testcase_06 | AC | 84 ms
75,740 KB |
testcase_07 | AC | 85 ms
75,772 KB |
testcase_08 | AC | 88 ms
75,756 KB |
testcase_09 | AC | 618 ms
93,620 KB |
testcase_10 | AC | 607 ms
93,980 KB |
testcase_11 | AC | 617 ms
93,016 KB |
testcase_12 | AC | 619 ms
93,284 KB |
testcase_13 | AC | 624 ms
94,184 KB |
testcase_14 | AC | 385 ms
102,136 KB |
testcase_15 | AC | 379 ms
102,156 KB |
testcase_16 | AC | 520 ms
95,512 KB |
testcase_17 | AC | 514 ms
97,396 KB |
testcase_18 | AC | 395 ms
101,452 KB |
testcase_19 | AC | 401 ms
113,916 KB |
testcase_20 | AC | 402 ms
113,016 KB |
testcase_21 | AC | 422 ms
83,452 KB |
testcase_22 | AC | 412 ms
82,828 KB |
testcase_23 | AC | 403 ms
83,720 KB |
ソースコード
class Union_Find(): def __init__(self,N): """0,1,...,n-1を要素として初期化する. N:要素数 """ self.n=N self.parents=[-1]*N self.rank=[0]*N def find(self, x): """要素xの属している族を調べる. x:要素 """ V=[] while self.parents[x]>=0: V.append(x) x=self.parents[x] for v in V: self.parents[v]=x return x def union(self, x, y): """要素x,yを同一視する. x,y:要素 """ x=self.find(x) y=self.find(y) if x==y: return if self.rank[x]<self.rank[y]: x,y=y,x self.parents[x]+=self.parents[y] self.parents[y]=x if self.rank[x]==self.rank[y]: self.rank[x]+=1 def size(self, x): """要素xの属している要素の数. x:要素 """ return -self.parents[self.find(x)] def same(self, x, y): """要素x,yは同一視されているか? x,y:要素 """ return self.find(x) == self.find(y) def members(self, x): """要素xが属している族の要素. ※族の要素の個数が欲しいときはsizeを使うこと!! 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): """全ての族の出力 """ X={r:[] for r in self.roots()} for k in range(self.n): X[self.find(k)].append(k) return X def __str__(self): return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots()) #=====--========================================= N,D,W=map(int,input().split()) U=Union_Find(N+1) for _ in range(D): a,b=map(int,input().split()) U.union(a,b) V=Union_Find(N+1) for j in range(W): c,d=map(int,input().split()) V.union(c,d) G=U.all_group_members() K=0 for t in G: if t==0:continue M=set() X=0 for a in G[t]: f=V.find(a) if f in M: continue M.add(f) X+=V.size(a) K+=X*len(G[t]) print(K-N)