結果
問題 |
No.1451 集団登校
|
ユーザー |
![]() |
提出日時 | 2025-04-10 01:55:18 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 652 ms / 2,000 ms |
コード長 | 1,984 bytes |
コンパイル時間 | 478 ms |
コンパイル使用メモリ | 12,544 KB |
実行使用メモリ | 40,288 KB |
最終ジャッジ日時 | 2025-04-10 01:55:30 |
合計ジャッジ時間 | 9,612 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
import sys input = sys.stdin.readline mod=10**9+7 n,m=map(int,input().split()) class Union_find(): def __init__(self, N): # N要素のUnion_find. A.Union_find(N)で作る. self.Group = [i for i in range(N)] self.Nodes = [1]*(N) def find(self,x): # find(a)=find(b)のとき同じグループ while self.Group[x] != x: x = self.Group[x] return x def Union(self,x,y): if self.find(x) != self.find(y): if self.Nodes[self.find(x)] < self.Nodes[self.find(y)]: self.Nodes[self.find(y)] += self.Nodes[self.find(x)] self.Nodes[self.find(x)] = 0 self.Group[self.find(x)] = self.find(y) else: self.Nodes[self.find(x)] += self.Nodes[self.find(y)] self.Nodes[self.find(y)] = 0 self.Group[self.find(y)] = self.find(x) uf1=Union_find(n) uf2=Union_find(n) TO=[i for i in range(n)] TO2=[i for i in range(n)] Parent=[-1]*(2*n+1) for i in range(m): x,y=map(int,input().split()) x-=1 y-=1 k=uf1.find(x) l=uf1.find(y) to_k=TO[k] to_l=TO[l] if k==l: continue if uf1.Nodes[k]<uf1.Nodes[l]: uf1.Union(k,l) TO[uf1.find(x)]=to_l elif uf1.Nodes[l]<uf1.Nodes[k]: uf1.Union(k,l) TO[uf1.find(x)]=to_k else: uf2.Union(to_k,to_l) Parent[TO2[to_k]]=len(Parent) Parent[TO2[to_l]]=len(Parent) Parent.append(-1) uf1.Union(k,l) TO[uf1.find(x)]=uf2.find(to_k) TO2[uf2.find(to_k)]=len(Parent)-1 ANS=[0]*n from functools import lru_cache @lru_cache(maxsize=None) def depth(x): if Parent[x]==-1: return 0 else: return depth(Parent[x])+1 INV=pow(2,mod-2,mod) for i in range(n): x=uf1.find(i) to=TO[x] if uf2.find(i)==to: print(pow(INV,depth(i),mod)) else: print(0)