結果
問題 | No.1451 集団登校 |
ユーザー | vwxyz |
提出日時 | 2023-05-21 15:58:17 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 1,559 ms / 2,000 ms |
コード長 | 3,333 bytes |
コンパイル時間 | 234 ms |
コンパイル使用メモリ | 13,184 KB |
実行使用メモリ | 87,060 KB |
最終ジャッジ日時 | 2024-06-01 12:37:50 |
合計ジャッジ時間 | 13,891 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 31 ms
11,008 KB |
testcase_01 | AC | 31 ms
11,136 KB |
testcase_02 | AC | 31 ms
11,264 KB |
testcase_03 | AC | 33 ms
11,264 KB |
testcase_04 | AC | 32 ms
11,136 KB |
testcase_05 | AC | 31 ms
11,136 KB |
testcase_06 | AC | 32 ms
11,264 KB |
testcase_07 | AC | 33 ms
11,136 KB |
testcase_08 | AC | 1,559 ms
87,060 KB |
testcase_09 | AC | 32 ms
11,136 KB |
testcase_10 | AC | 463 ms
50,120 KB |
testcase_11 | AC | 272 ms
50,296 KB |
testcase_12 | AC | 431 ms
46,196 KB |
testcase_13 | AC | 725 ms
50,888 KB |
testcase_14 | AC | 966 ms
41,692 KB |
testcase_15 | AC | 416 ms
32,284 KB |
testcase_16 | AC | 635 ms
44,284 KB |
testcase_17 | AC | 830 ms
12,800 KB |
testcase_18 | AC | 265 ms
25,556 KB |
testcase_19 | AC | 170 ms
34,164 KB |
testcase_20 | AC | 1,025 ms
44,652 KB |
testcase_21 | AC | 564 ms
11,904 KB |
testcase_22 | AC | 258 ms
16,000 KB |
testcase_23 | AC | 160 ms
11,264 KB |
testcase_24 | AC | 420 ms
49,144 KB |
testcase_25 | AC | 415 ms
30,392 KB |
testcase_26 | AC | 625 ms
44,804 KB |
testcase_27 | AC | 835 ms
27,428 KB |
testcase_28 | AC | 268 ms
18,688 KB |
testcase_29 | AC | 472 ms
29,384 KB |
ソースコード
import sys readline=sys.stdin.readline from collections import defaultdict class UnionFind: def __init__(self,N,label=None,f=None,weighted=False): self.N=N self.parents=[None]*self.N self.size=[1]*self.N self.roots={i for i in range(self.N)} self.label=label if self.label!=None: self.label=[x for x in label] self.f=f self.weighted=weighted if self.weighted: self.weight=[0]*self.N def Find(self,x): stack=[] while self.parents[x]!=None: stack.append(x) x=self.parents[x] if self.weighted: w=0 for y in stack[::-1]: self.parents[y]=x w+=self.weight[y] self.weight[y]=w else: for y in stack[::-1]: self.parents[y]=x return x def Union(self,x,y,w=None): root_x=self.Find(x) root_y=self.Find(y) if root_x==root_y: if self.weighted: if self.weight[y]-self.weight[x]==w: return True else: return False else: if self.size[root_x]<self.size[root_y]: x,y=y,x root_x,root_y=root_y,root_x if self.weighted: w=-w self.parents[root_y]=root_x self.size[root_x]+=self.size[root_y] self.roots.remove(root_y) if self.label!=None: self.label[root_x]=self.f(self.label[root_x],self.label[root_y]) if self.weighted: self.weight[root_y]=w+self.weight[x]-self.weight[y] def Size(self,x): return self.size[self.Find(x)] def Same(self,x,y): return self.Find(x)==self.Find(y) def Label(self,x): return self.label[self.Find(x)] def Weight(self,x,y): root_x=self.Find(x) root_y=self.Find(y) if root_x!=root_y: return None return self.weight[y]-self.weight[x] def Roots(self): return list(self.roots) def Linked_Components_Count(self): return len(self.roots) def Linked_Components(self): linked_components=defaultdict(list) for x in range(self.N): linked_components[self.Find(x)].append(x) return linked_components def __str__(self): linked_components=defaultdict(list) for x in range(self.N): linked_components[self.Find(x)].append(x) return "\n".join(f"{r}: {m}" for r,m in linked_components.items()) N,M=map(int,readline().split()) mod=10**9+7 inve2=(mod+1)//2 dp=[defaultdict(int) for i in range(N)] for i in range(N): dp[i][i]=1 UF=UnionFind(N) for m in range(M): a,b=map(int,readline().split()) a=UF.Find(a-1);b=UF.Find(b-1) size_a,size_b=UF.Size(a),UF.Size(b) UF.Union(a,b) r=UF.Find(a) if size_a==size_b: dp_a,dp_b=dp[a],dp[b] dp[r]=defaultdict(int) for x,p in dp_a.items(): dp[r][x]+=p*inve2%mod dp[r][x]%=mod for x,p in dp_b.items(): dp[r][x]+=p*inve2%mod dp[r][x]%=mod ans_lst=[0]*N for r in UF.Roots(): for x,p in dp[r].items(): ans_lst[x]+=p print(*ans_lst,sep="\n")