結果
問題 | No.1390 Get together |
ユーザー | Lignan |
提出日時 | 2021-06-18 07:42:10 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 1,699 bytes |
コンパイル時間 | 89 ms |
コンパイル使用メモリ | 10,764 KB |
実行使用メモリ | 36,604 KB |
最終ジャッジ日時 | 2023-09-03 17:49:43 |
合計ジャッジ時間 | 6,694 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 21 ms
13,212 KB |
testcase_01 | AC | 20 ms
8,648 KB |
testcase_02 | RE | - |
testcase_03 | AC | 42 ms
9,228 KB |
testcase_04 | AC | 35 ms
9,276 KB |
testcase_05 | AC | 38 ms
9,316 KB |
testcase_06 | AC | 35 ms
9,204 KB |
testcase_07 | AC | 34 ms
9,200 KB |
testcase_08 | AC | 35 ms
9,252 KB |
testcase_09 | AC | 40 ms
9,380 KB |
testcase_10 | AC | 20 ms
8,644 KB |
testcase_11 | AC | 20 ms
8,636 KB |
testcase_12 | AC | 20 ms
8,652 KB |
testcase_13 | AC | 20 ms
8,740 KB |
testcase_14 | AC | 20 ms
8,684 KB |
testcase_15 | AC | 20 ms
8,700 KB |
testcase_16 | TLE | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
ソースコード
from collections import defaultdict n,m=map(int,input().split()) lst=[[] for i in range(m)] for i in range(n): b,c=map(int,input().split()) b-=1 c-=1 lst[c].append(b) 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): rood=self.find(x) return [i for i in range(self.n) if self.find(i)==root] def roots(self,x): 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): groum_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()) slime=UnionFind(m) ans=0 for i in range(m): if len(lst[i])<2: continue for j in range(len(lst[i])): for k in range(j+1,len(lst[i])): if slime.same(lst[i][j],lst[i][k]): continue slime.union(lst[i][j],lst[i][k]) ans+=1 print(ans)