結果
問題 | No.1227 I hate ThREE |
ユーザー |
![]() |
提出日時 | 2021-09-15 02:45:44 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 239 ms / 2,000 ms |
コード長 | 25,286 bytes |
コンパイル時間 | 492 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 113,408 KB |
最終ジャッジ日時 | 2024-06-27 22:37:10 |
合計ジャッジ時間 | 9,055 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
import bisectimport copyimport decimalimport fractionsimport functoolsimport heapqimport itertoolsimport mathimport randomimport sysfrom collections import Counter,deque,defaultdictfrom functools import lru_cache,reducefrom heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_maxdef _heappush_max(heap,item):heap.append(item)heapq._siftdown_max(heap, 0, len(heap)-1)def _heappushpop_max(heap, item):if heap and item < heap[0]:item, heap[0] = heap[0], itemheapq._siftup_max(heap, 0)return itemfrom math import degrees, gcd as GCDread=sys.stdin.readreadline=sys.stdin.readlinereadlines=sys.stdin.readlinesdef Extended_Euclid(n,m):stack=[]while m:stack.append((n,m))n,m=m,n%mif n>=0:x,y=1,0else:x,y=-1,0for i in range(len(stack)-1,-1,-1):n,m=stack[i]x,y=y,x-(n//m)*yreturn x,yclass MOD:def __init__(self,p,e=1):self.p=pself.e=eself.mod=self.p**self.edef Pow(self,a,n):a%=self.modif n>=0:return pow(a,n,self.mod)else:assert math.gcd(a,self.mod)==1x=Extended_Euclid(a,self.mod)[0]return pow(x,-n,self.mod)def Build_Fact(self,N):assert N>=0self.factorial=[1]self.cnt=[0]*(N+1)for i in range(1,N+1):ii=iself.cnt[i]=self.cnt[i-1]while ii%self.p==0:ii//=self.pself.cnt[i]+=1self.factorial.append((self.factorial[-1]*ii)%self.mod)self.factorial_inve=[None]*(N+1)self.factorial_inve[-1]=self.Pow(self.factorial[-1],-1)for i in range(N-1,-1,-1):ii=i+1while ii%self.p==0:ii//=self.pself.factorial_inve[i]=(self.factorial_inve[i+1]*ii)%self.moddef Fact(self,N):return self.factorial[N]*pow(self.p,self.cnt[N],self.mod)%self.moddef Fact_Inve(self,N):if self.cnt[N]:return Nonereturn self.factorial_inve[N]def Comb(self,N,K,divisible_count=False):if K<0 or K>N:return 0retu=self.factorial[N]*self.factorial_inve[K]*self.factorial_inve[N-K]%self.modcnt=self.cnt[N]-self.cnt[N-K]-self.cnt[K]if divisible_count:return retu,cntelse:retu*=pow(self.p,cnt,self.mod)retu%=self.modreturn retuclass Graph:def __init__(self,V,edges=False,graph=False,directed=False,weighted=False):self.V=Vself.directed=directedself.weighted=weightedif not graph:self.edges=edgesself.graph=[[] for i in range(self.V)]if weighted:for i,j,d in self.edges:self.graph[i].append((j,d))if not self.directed:self.graph[j].append((i,d))else:for i,j in self.edges:self.graph[i].append(j)if not self.directed:self.graph[j].append(i)else:self.graph=graphself.edges=[]for i in range(self.V):if self.weighted:for j,d in self.graph[i]:if self.directed or not self.directed and i<=j:self.edges.append((i,j,d))else:for j in self.graph[i]:if self.directed or not self.directed and i<=j:self.edges.append((i,j))def SS_BFS(self,s,bipartite_graph=False,linked_components=False,parents=False,unweighted_dist=False,weighted_dist=False):seen=[False]*self.Vseen[s]=Trueif linked_components:lc=[s]if parents:ps=[None]*self.Vps[s]=sif unweighted_dist or bipartite_graph:uwd=[float('inf')]*self.Vuwd[s]=0if weighted_dist:wd=[float('inf')]*self.Vwd[s]=0queue=deque([s])while queue:x=queue.popleft()for y in self.graph[x]:if self.weighted:y,d=yif not seen[y]:seen[y]=Truequeue.append(y)if linked_components:lc.append(y)if parents:ps[y]=xif unweighted_dist or bipartite_graph:uwd[y]=uwd[x]+1if weighted_dist:wd[y]=wd[x]+dif bipartite_graph:bg=[[],[]]for tpl in self.edges:i,j=tpl[:2] if self.weighted else tplif type(uwd[i])==float or type(uwd[j])==float:continueif not uwd[i]%2^uwd[j]%2:bg=Falsebreakelse:for x in range(self.V):if type(uwd[x])==float:continuebg[uwd[x]%2].append(x)tpl=()if bipartite_graph:tpl+=(bg,)if linked_components:tpl+=(lc,)if parents:tpl+=(ps,)if unweighted_dist:tpl+=(uwd,)if weighted_dist:tpl+=(wd,)if len(tpl)==1:tpl=tpl[0]return tpldef AP_BFS(self,bipartite_graph=False,linked_components=False,parents=False):seen=[False]*self.Vif bipartite_graph:bg=[None]*self.Vcnt=-1if linked_components:lc=[]if parents:ps=[None]*self.Vfor s in range(self.V):if seen[s]:continueseen[s]=Trueif bipartite_graph:cnt+=1bg[s]=(cnt,0)if linked_components:lc.append([s])if parents:ps[s]=squeue=deque([s])while queue:x=queue.popleft()for y in self.graph[x]:if self.weighted:y,d=yif not seen[y]:seen[y]=Truequeue.append(y)if bipartite_graph:bg[y]=(cnt,bg[x][1]^1)if linked_components:lc[-1].append(y)if parents:ps[y]=xif bipartite_graph:bg_=bgbg=[[[],[]] for i in range(cnt+1)]for tpl in self.edges:i,j=tpl[:2] if self.weighted else tplif not bg_[i][1]^bg_[j][1]:bg[bg_[i][0]]=Falsefor x in range(self.V):if bg[bg_[x][0]]:bg[bg_[x][0]][bg_[x][1]].append(x)tpl=()if bipartite_graph:tpl+=(bg,)if linked_components:tpl+=(lc,)if parents:tpl+=(ps,)if len(tpl)==1:tpl=tpl[0]return tpldef SS_DFS(self,s,bipartite_graph=False,cycle_detection=False,directed_acyclic=False,euler_tour=False,linked_components=False,parents=False,postorder=False,preorder=False,subtree_size=False,topological_sort=False,unweighted_dist=False,weighted_dist=False):seen=[False]*self.Vfinished=[False]*self.Vif directed_acyclic or cycle_detection or topological_sort:dag=Trueif euler_tour:et=[]if linked_components:lc=[]if parents or cycle_detection or subtree_size:ps=[None]*self.Vps[s]=sif postorder or topological_sort:post=[]if preorder:pre=[]if subtree_size:ss=[1]*self.Vif unweighted_dist or bipartite_graph:uwd=[float('inf')]*self.Vuwd[s]=0if weighted_dist:wd=[float('inf')]*self.Vwd[s]=0stack=[(s,0)] if self.weighted else [s]while stack:if self.weighted:x,d=stack.pop()else:x=stack.pop()if not seen[x]:seen[x]=Truestack.append((x,d) if self.weighted else x)if euler_tour:et.append(x)if linked_components:lc.append(x)if preorder:pre.append(x)for y in self.graph[x]:if self.weighted:y,d=yif not seen[y]:stack.append((y,d) if self.weighted else y)if parents or cycle_detection or subtree_size:ps[y]=xif unweighted_dist or bipartite_graph:uwd[y]=uwd[x]+1if weighted_dist:wd[y]=wd[x]+delif not finished[y]:if (directed_acyclic or cycle_detection or topological_sort) and dag:dag=Falseif cycle_detection:cd=(y,x)elif not finished[x]:finished[x]=Trueif euler_tour:et.append(~x)if postorder or topological_sort:post.append(x)if subtree_size:for y in self.graph[x]:if y==ps[x]:continuess[x]+=ss[y]if bipartite_graph:bg=[[],[]]for tpl in self.edges:i,j=tpl[:2] if self.weighted else tplif type(uwd[i])==float or type(uwd[j])==float:continueif not uwd[i]%2^uwd[j]%2:bg=Falsebreakelse:for x in range(self.V):if type(uwd[x])==float:continuebg[uwd[x]%2].append(x)tpl=()if bipartite_graph:tpl+=(bg,)if cycle_detection:if dag:cd=[]else:y,x=cdcd=self.Route_Restoration(y,x,ps)tpl+=(cd,)if directed_acyclic:tpl+=(dag,)if euler_tour:tpl+=(et,)if linked_components:tpl+=(lc,)if parents:tpl+=(ps,)if postorder:tpl+=(post,)if preorder:tpl+=(pre,)if subtree_size:tpl+=(ss,)if topological_sort:if dag:tp_sort=post[::-1]else:tp_sort=[]tpl+=(tp_sort,)if unweighted_dist:tpl+=(uwd,)if weighted_dist:tpl+=(wd,)if len(tpl)==1:tpl=tpl[0]return tpldef AP_DFS(self,bipartite_graph=False,cycle_detection=False,directed_acyclic=False,euler_tour=False,linked_components=False,parents=False,postorder=False,preorder=False,topological_sort=False):seen=[False]*self.Vfinished=[False]*self.Vif bipartite_graph:bg=[None]*self.Vcnt=-1if directed_acyclic or cycle_detection or topological_sort:dag=Trueif euler_tour:et=[]if linked_components:lc=[]if parents or cycle_detection:ps=[None]*self.Vif postorder or topological_sort:post=[]if preorder:pre=[]for s in range(self.V):if seen[s]:continueif bipartite_graph:cnt+=1bg[s]=(cnt,0)if linked_components:lc.append([])if parents:ps[s]=sstack=[(s,0)] if self.weighted else [s]while stack:if self.weighted:x,d=stack.pop()else:x=stack.pop()if not seen[x]:seen[x]=Truestack.append((x,d) if self.weighted else x)if euler_tour:et.append(x)if linked_components:lc[-1].append(x)if preorder:pre.append(x)for y in self.graph[x]:if self.weighted:y,d=yif not seen[y]:stack.append((y,d) if self.weighted else y)if bipartite_graph:bg[y]=(cnt,bg[x][1]^1)if parents or cycle_detection:ps[y]=xelif not finished[y]:if directed_acyclic and dag:dag=Falseif cycle_detection:cd=(y,x)elif not finished[x]:finished[x]=Trueif euler_tour:et.append(~x)if postorder or topological_sort:post.append(x)if bipartite_graph:bg_=bgbg=[[[],[]] for i in range(cnt+1)]for tpl in self.edges:i,j=tpl[:2] if self.weighted else tplif not bg_[i][1]^bg_[j][1]:bg[bg_[i][0]]=Falsefor x in range(self.V):if bg[bg_[x][0]]:bg[bg_[x][0]][bg_[x][1]].append(x)tpl=()if bipartite_graph:tpl+=(bg,)if cycle_detection:if dag:cd=[]else:y,x=cdcd=self.Route_Restoration(y,x,ps)tpl+=(cd,)if directed_acyclic:tpl+=(dag,)if euler_tour:tpl+=(et,)if linked_components:tpl+=(lc,)if parents:tpl+=(ps,)if postorder:tpl+=(post,)if preorder:tpl+=(pre,)if topological_sort:if dag:tp_sort=post[::-1]else:tp_sort=[]tpl+=(tp_sort,)if len(tpl)==1:tpl=tpl[0]return tpldef Tree_Diameter(self,weighted=False):def Farthest_Point(u):dist=self.SS_BFS(u,weighted_dist=True) if weighted else self.SS_BFS(u,unweighted_dist=True)fp=0for i in range(self.V):if dist[fp]<dist[i]:fp=ireturn fp,dist[fp]u,d=Farthest_Point(0)v,d=Farthest_Point(u)return u,v,ddef SCC(self):reverse_graph=[[] for i in range(self.V)]for tpl in self.edges:i,j=tpl[:2] if self.weighted else tplreverse_graph[j].append(i)postorder=self.AP_DFS(postorder=True)scc=[]seen=[False]*self.Vfor s in postorder[::-1]:if seen[s]:continuequeue=deque([s])seen[s]=Truelst=[]while queue:x=queue.popleft()lst.append(x)for y in reverse_graph[x]:if self.weighted:y=y[0]if not seen[y]:seen[y]=Truequeue.append(y)scc.append(lst)return sccdef Build_LCA(self,s):self.euler_tour,self.parents,depth=self.SS_DFS(s,euler_tour=True,parents=True,unweighted_dist=True)self.dfs_in_index=[None]*self.Vself.dfs_out_index=[None]*self.Vfor i,x in enumerate(self.euler_tour):if x>=0:self.dfs_in_index[x]=ielse:self.dfs_out_index[~x]=iself.ST=Segment_Tree(2*self.V,lambda x,y:min(x,y),float('inf'))lst=[None]*2*self.Vfor i in range(2*self.V):if self.euler_tour[i]>=0:lst[i]=depth[self.euler_tour[i]]else:lst[i]=depth[self.parents[~self.euler_tour[i]]]self.ST.Build(lst)def LCA(self,a,b):m=min(self.dfs_in_index[a],self.dfs_in_index[b])M=max(self.dfs_in_index[a],self.dfs_in_index[b])x=self.euler_tour[self.ST.Fold_Index(m,M+1)]if x>=0:return xelse:return self.parents[~x]def Dijkstra(self,s,route_restoration=False):dist=[float('inf')]*self.Vdist[s]=0hq=[(0,s)]if route_restoration:parents=[None]*self.Vparents[s]=swhile hq:dx,x=heapq.heappop(hq)if dist[x]<dx:continuefor y,dy in self.graph[x]:if dist[y]>dx+dy:dist[y]=dx+dyif route_restoration:parents[y]=xheapq.heappush(hq,(dist[y],y))if route_restoration:return dist,parentselse:return distdef Bellman_Ford(self,s,route_restoration=False):dist=[float('inf')]*self.Vdist[s]=0if route_restoration:parents=[s]*self.Vfor _ in range(self.V-1):for i,j,d in self.edges:if dist[j]>dist[i]+d:dist[j]=dist[i]+dif route_restoration:parents[j]=iif not self.directed and dist[i]>dist[j]+d:dist[i]=dist[j]+dif route_restoration:parents[i]=jnegative_cycle=[]for i,j,d in self.edges:if dist[j]>dist[i]+d:negative_cycle.append(j)if not self.directed and dist[i]>dist[j]+d:negative_cycle.append(i)if negative_cycle:is_negative_cycle=[False]*self.Vfor i in negative_cycle:if is_negative_cycle[i]:continueelse:queue=deque([i])is_negative_cycle[i]=Truewhile queue:x=queue.popleft()for y,d in self.graph[x]:if not is_negative_cycle[y]:queue.append(y)is_negative_cycle[y]=Trueif route_restoration:parents[y]=xfor i in range(self.V):if is_negative_cycle[i]:dist[i]=-float('inf')if route_restoration:return dist,parentselse:return distdef Warshall_Floyd(self,route_restoration=False):dist=[[float('inf')]*self.V for i in range(self.V)]for i in range(self.V):dist[i][i]=0if route_restoration:parents=[[j for j in range(self.V)] for i in range(self.V)]for i,j,d in self.edges:if dist[i][j]>d:dist[i][j]=dif route_restoration:parents[i][j]=iif not self.directed and dist[j][i]>d:dist[j][i]=dif route_restoration:parents[j][i]=jfor k in range(self.V):for i in range(self.V):for j in range(self.V):if dist[i][j]>dist[i][k]+dist[k][j]:dist[i][j]=dist[i][k]+dist[k][j]if route_restoration:parents[i][j]=parents[k][j]for i in range(self.V):if dist[i][i]<0:for j in range(self.V):if dist[i][j]!=float('inf'):dist[i][j]=-float('inf')if route_restoration:return dist,parentselse:return distdef Route_Restoration(self,s,g,parents):route=[g]while s!=g and parents[g]!=g:g=parents[g]route.append(g)route=route[::-1]return routedef Kruskal(self):UF=UnionFind(self.V)sorted_edges=sorted(self.edges,key=lambda x:x[2])minimum_spnning_tree=[]for i,j,d in sorted_edges:if not UF.Same(i,j):UF.Union(i,j)minimum_spnning_tree.append((i,j,d))return minimum_spnning_treedef Ford_Fulkerson(self,s,t):max_flow=0residual_graph=[defaultdict(int) for i in range(self.V)]if self.weighted:for i,j,d in self.edges:if not d:continueresidual_graph[i][j]+=dif not self.directed:residual_graph[j][i]+=delse:for i,j in self.edges:residual_graph[i][j]+=1if not self.directed:residual_graph[j][i]+=1while True:parents=[None]*self.Vparents[s]=sseen=[False]*self.Vseen[s]=Truequeue=deque([s])while queue:x=queue.popleft()for y in residual_graph[x].keys():if not seen[y]:seen[y]=Truequeue.append(y)parents[y]=xif y==t:tt=twhile tt!=s:residual_graph[parents[tt]][tt]-=1residual_graph[tt][parents[tt]]+=1if not residual_graph[parents[tt]][tt]:residual_graph[parents[tt]].pop(tt)tt=parents[tt]max_flow+=1breakelse:continuebreakelse:breakreturn max_flowdef BFS(self,s):seen=[False]*self.Vseen[s]=Truequeue=deque([s])while queue:x=queue.popleft()for y in self.graph[x]:if self.weighted:y,d=yif not seen[y]:seen[y]=Truequeue.append(y)returndef DFS(self,s):seen=[False]*self.Vfinished=[False]*self.Vstack=[(s,0)] if self.weighted else [s]while stack:if self.weighted:x,d=stack.pop()else:x=stack.pop()if not seen[x]:seen[x]=Truestack.append((x,d) if self.weighted else x)for y in self.graph[x]:if self.weighted:y,d=yif not seen[y]:stack.append((y,d) if self.weighted else y)elif not finished[x]:finished[x]=TruereturnN,C=map(int,readline().split())mod=10**9+7edges=[]for _ in range(N-1):a,b=map(int,readline().split())a-=1;b-=1edges.append((a,b))G=Graph(N,edges=edges)parents,tour=G.SS_DFS(0,parents=True,postorder=True)if C<=10**4:dp=[[1]*C for x in range(N)]for x in tour:if x==0 or len(G.graph[x])>1:for y in G.graph[x]:if y==parents[x]:continuefor i in range(C):s=0if i>=3:s+=dp[y][i-3]if i<=C-4:s+=dp[y][i+3]dp[x][i]*=sdp[x][i]%=modans=sum(dp[0])%modelse:dp=[[1]*N*3 for x in range(N)]for x in tour:if x==0 or len(G.graph[x])>1:for y in G.graph[x]:if y==parents[x]:continuefor i in range(N*3):s=0if i>=3:s+=dp[y][i-3]if i<=N*3-4:s+=dp[y][i+3]else:s+=dp[y][N*3-1]dp[x][i]*=sdp[x][i]%=modans=sum(dp[0])*2+(C-len(dp[0])*2)*dp[0][N*3-1]ans%=modprint(ans)