結果
| 問題 |
No.899 γatheree
|
| コンテスト | |
| ユーザー |
vwxyz
|
| 提出日時 | 2021-11-03 00:28:00 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 31,907 bytes |
| コンパイル時間 | 158 ms |
| コンパイル使用メモリ | 82,600 KB |
| 実行使用メモリ | 172,832 KB |
| 最終ジャッジ日時 | 2024-10-12 07:54:24 |
| 合計ジャッジ時間 | 8,335 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 5 TLE * 18 |
ソースコード
from os import read
import sys
readline=sys.stdin.readline
import heapq
from collections import defaultdict,deque
class Graph:
def __init__(self,V,edges=False,graph=False,directed=False,weighted=False):
self.V=V
self.directed=directed
self.weighted=weighted
if not graph:
self.edges=edges
self.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=graph
self.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,bfs_tour=False,bipartite_graph=False,linked_components=False,parents=False,unweighted_dist=False,weighted_dist=False):
seen=[False]*self.V
seen[s]=True
if bfs_tour:
bt=[s]
if linked_components:
lc=[s]
if parents:
ps=[None]*self.V
ps[s]=s
if unweighted_dist or bipartite_graph:
uwd=[float('inf')]*self.V
uwd[s]=0
if weighted_dist:
wd=[float('inf')]*self.V
wd[s]=0
queue=deque([s])
while queue:
x=queue.popleft()
for y in self.graph[x]:
if self.weighted:
y,d=y
if not seen[y]:
seen[y]=True
queue.append(y)
if bfs_tour:
bt.append(y)
if linked_components:
lc.append(y)
if parents:
ps[y]=x
if unweighted_dist or bipartite_graph:
uwd[y]=uwd[x]+1
if weighted_dist:
wd[y]=wd[x]+d
if bipartite_graph:
bg=[[],[]]
for tpl in self.edges:
i,j=tpl[:2] if self.weighted else tpl
if type(uwd[i])==float or type(uwd[j])==float:
continue
if not uwd[i]%2^uwd[j]%2:
bg=False
break
else:
for x in range(self.V):
if type(uwd[x])==float:
continue
bg[uwd[x]%2].append(x)
tpl=()
if bfs_tour:
tpl+=(bt,)
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 tpl
def AP_BFS(self,bipartite_graph=False,linked_components=False,parents=False):
seen=[False]*self.V
if bipartite_graph:
bg=[None]*self.V
cnt=-1
if linked_components:
lc=[]
if parents:
ps=[None]*self.V
for s in range(self.V):
if seen[s]:
continue
seen[s]=True
if bipartite_graph:
cnt+=1
bg[s]=(cnt,0)
if linked_components:
lc.append([s])
if parents:
ps[s]=s
queue=deque([s])
while queue:
x=queue.popleft()
for y in self.graph[x]:
if self.weighted:
y,d=y
if not seen[y]:
seen[y]=True
queue.append(y)
if bipartite_graph:
bg[y]=(cnt,bg[x][1]^1)
if linked_components:
lc[-1].append(y)
if parents:
ps[y]=x
if bipartite_graph:
bg_=bg
bg=[[[],[]] for i in range(cnt+1)]
for tpl in self.edges:
i,j=tpl[:2] if self.weighted else tpl
if not bg_[i][1]^bg_[j][1]:
bg[bg_[i][0]]=False
for 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 tpl
def 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.V
finished=[False]*self.V
if directed_acyclic or cycle_detection or topological_sort:
dag=True
if euler_tour:
et=[]
if linked_components:
lc=[]
if parents or cycle_detection or subtree_size:
ps=[None]*self.V
ps[s]=s
if postorder or topological_sort:
post=[]
if preorder:
pre=[]
if subtree_size:
ss=[1]*self.V
if unweighted_dist or bipartite_graph:
uwd=[float('inf')]*self.V
uwd[s]=0
if weighted_dist:
wd=[float('inf')]*self.V
wd[s]=0
stack=[(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]=True
stack.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=y
if not seen[y]:
stack.append((y,d) if self.weighted else y)
if parents or cycle_detection or subtree_size:
ps[y]=x
if unweighted_dist or bipartite_graph:
uwd[y]=uwd[x]+1
if weighted_dist:
wd[y]=wd[x]+d
elif not finished[y]:
if (directed_acyclic or cycle_detection or topological_sort) and dag:
dag=False
if cycle_detection:
cd=(y,x)
elif not finished[x]:
finished[x]=True
if euler_tour:
et.append(~x)
if postorder or topological_sort:
post.append(x)
if subtree_size:
for y in self.graph[x]:
if self.weighted:
y,d=y
if y==ps[x]:
continue
ss[x]+=ss[y]
if bipartite_graph:
bg=[[],[]]
for tpl in self.edges:
i,j=tpl[:2] if self.weighted else tpl
if type(uwd[i])==float or type(uwd[j])==float:
continue
if not uwd[i]%2^uwd[j]%2:
bg=False
break
else:
for x in range(self.V):
if type(uwd[x])==float:
continue
bg[uwd[x]%2].append(x)
tpl=()
if bipartite_graph:
tpl+=(bg,)
if cycle_detection:
if dag:
cd=[]
else:
y,x=cd
cd=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 tpl
def 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.V
finished=[False]*self.V
if bipartite_graph:
bg=[None]*self.V
cnt=-1
if directed_acyclic or cycle_detection or topological_sort:
dag=True
if euler_tour:
et=[]
if linked_components:
lc=[]
if parents or cycle_detection:
ps=[None]*self.V
if postorder or topological_sort:
post=[]
if preorder:
pre=[]
for s in range(self.V):
if seen[s]:
continue
if bipartite_graph:
cnt+=1
bg[s]=(cnt,0)
if linked_components:
lc.append([])
if parents:
ps[s]=s
stack=[(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]=True
stack.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=y
if 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]=x
elif not finished[y]:
if directed_acyclic and dag:
dag=False
if cycle_detection:
cd=(y,x)
elif not finished[x]:
finished[x]=True
if euler_tour:
et.append(~x)
if postorder or topological_sort:
post.append(x)
if bipartite_graph:
bg_=bg
bg=[[[],[]] for i in range(cnt+1)]
for tpl in self.edges:
i,j=tpl[:2] if self.weighted else tpl
if not bg_[i][1]^bg_[j][1]:
bg[bg_[i][0]]=False
for 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=cd
cd=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 tpl
def 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=0
for i in range(self.V):
if dist[fp]<dist[i]:
fp=i
return fp,dist[fp]
u,d=Farthest_Point(0)
v,d=Farthest_Point(u)
return u,v,d
def SCC(self):
reverse_graph=[[] for i in range(self.V)]
for tpl in self.edges:
i,j=tpl[:2] if self.weighted else tpl
reverse_graph[j].append(i)
postorder=self.AP_DFS(postorder=True)
scc=[]
seen=[False]*self.V
for s in postorder[::-1]:
if seen[s]:
continue
queue=deque([s])
seen[s]=True
lst=[]
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]=True
queue.append(y)
scc.append(lst)
return scc
def Build_LCA(self,s):
self.lca_euler_tour,self.lca_parents,depth=self.SS_DFS(s,euler_tour=True,parents=True,unweighted_dist=True)
self.lca_dfs_in_index=[None]*self.V
self.lca_dfs_out_index=[None]*self.V
for i,x in enumerate(self.lca_euler_tour):
if x>=0:
self.lca_dfs_in_index[x]=i
else:
self.lca_dfs_out_index[~x]=i
self.ST=Segment_Tree(2*self.V,lambda x,y:min(x,y),float('inf'))
lst=[None]*2*self.V
for i in range(2*self.V):
if self.lca_euler_tour[i]>=0:
lst[i]=depth[self.lca_euler_tour[i]]
else:
lst[i]=depth[self.lca_parents[~self.lca_euler_tour[i]]]
self.ST.Build(lst)
def LCA(self,a,b):
m=min(self.lca_dfs_in_index[a],self.lca_dfs_in_index[b])
M=max(self.lca_dfs_in_index[a],self.lca_dfs_in_index[b])
x=self.lca_euler_tour[self.ST.Fold_Index(m,M+1)]
if x>=0:
return x
else:
return self.lca_parents[~x]
def Build_HLD(self,s):
size=self.SS_DFS(s,subtree_size=True)
seen=[False]*self.V
stack=[s]
self.hld_tour=[]
self.hld_parents=[None]*self.V
self.hld_depth=[None]*self.V
self.hld_path_parents=[None]*self.V
self.hld_path_parents[s]=s
self.hld_depth[s]=0
while stack:
x=stack.pop()
seen[x]=True
self.hld_tour.append(x)
max_size=0
max_size_y=None
for y in self.graph[x]:
if self.weighted:
y,d=y
if not seen[y] and max_size<size[y]:
max_size=size[y]
max_size_y=y
for y in self.graph[x]:
if self.weighted:
y,d=y
if not seen[y] and y!=max_size_y:
stack.append(y)
self.hld_parents[y]=x
self.hld_depth[y]=self.hld_depth[x]+1
self.hld_path_parents[y]=y
if max_size_y!=None:
stack.append(max_size_y)
self.hld_parents[max_size_y]=x
self.hld_depth[max_size_y]=self.hld_depth[x]+1
self.hld_path_parents[max_size_y]=self.hld_path_parents[x]
self.hld_tour_idx=[None]*self.V
for i in range(self.V):
self.hld_tour_idx[self.hld_tour[i]]=i
def HLD(self,a,b,edge=False):
L,R=[],[]
while self.hld_path_parents[a]!=self.hld_path_parents[b]:
if self.hld_depth[self.hld_path_parents[a]]<self.hld_depth[self.hld_path_parents[b]]:
R.append((self.hld_tour_idx[self.hld_path_parents[b]],self.hld_tour_idx[b]+1))
b=self.hld_parents[self.hld_path_parents[b]]
else:
L.append((self.hld_tour_idx[a]+1,self.hld_tour_idx[self.hld_path_parents[a]]))
a=self.hld_parents[self.hld_path_parents[a]]
if edge:
if self.hld_depth[a]!=self.hld_depth[b]:
retu=L+[(self.hld_tour_idx[a]+1,self.hld_tour_idx[b]+1)]+R[::-1]
else:
retu=L+R[::-1]
else:
if self.hld_depth[a]<self.hld_depth[b]:
retu=L+[(self.hld_tour_idx[a],self.hld_tour_idx[b]+1)]+R[::-1]
else:
retu=L+[(self.hld_tour_idx[a]+1,self.hld_tour_idx[b])]+R[::-1]
return retu
def Dijkstra(self,s,route_restoration=False):
dist=[float('inf')]*self.V
dist[s]=0
hq=[(0,s)]
if route_restoration:
parents=[None]*self.V
parents[s]=s
while hq:
dx,x=heapq.heappop(hq)
if dist[x]<dx:
continue
for y,dy in self.graph[x]:
if dist[y]>dx+dy:
dist[y]=dx+dy
if route_restoration:
parents[y]=x
heapq.heappush(hq,(dist[y],y))
if route_restoration:
return dist,parents
else:
return dist
def Bellman_Ford(self,s,route_restoration=False):
dist=[float('inf')]*self.V
dist[s]=0
if route_restoration:
parents=[s]*self.V
for _ in range(self.V-1):
for i,j,d in self.edges:
if dist[j]>dist[i]+d:
dist[j]=dist[i]+d
if route_restoration:
parents[j]=i
if not self.directed and dist[i]>dist[j]+d:
dist[i]=dist[j]+d
if route_restoration:
parents[i]=j
negative_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.V
for i in negative_cycle:
if is_negative_cycle[i]:
continue
else:
queue=deque([i])
is_negative_cycle[i]=True
while queue:
x=queue.popleft()
for y,d in self.graph[x]:
if not is_negative_cycle[y]:
queue.append(y)
is_negative_cycle[y]=True
if route_restoration:
parents[y]=x
for i in range(self.V):
if is_negative_cycle[i]:
dist[i]=-float('inf')
if route_restoration:
return dist,parents
else:
return dist
def 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]=0
if 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]=d
if route_restoration:
parents[i][j]=i
if not self.directed and dist[j][i]>d:
dist[j][i]=d
if route_restoration:
parents[j][i]=j
for 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,parents
else:
return dist
def 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 route
def 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_tree
def Ford_Fulkerson(self,s,t):
max_flow=0
residual_graph=[defaultdict(int) for i in range(self.V)]
if self.weighted:
for i,j,d in self.edges:
if not d:
continue
residual_graph[i][j]+=d
if not self.directed:
residual_graph[j][i]+=d
else:
for i,j in self.edges:
residual_graph[i][j]+=1
if not self.directed:
residual_graph[j][i]+=1
while True:
parents=[None]*self.V
parents[s]=s
seen=[False]*self.V
seen[s]=True
queue=deque([s])
while queue:
x=queue.popleft()
for y in residual_graph[x].keys():
if not seen[y]:
seen[y]=True
queue.append(y)
parents[y]=x
if y==t:
tt=t
while tt!=s:
residual_graph[parents[tt]][tt]-=1
residual_graph[tt][parents[tt]]+=1
if not residual_graph[parents[tt]][tt]:
residual_graph[parents[tt]].pop(tt)
tt=parents[tt]
max_flow+=1
break
else:
continue
break
else:
break
return max_flow
def BFS(self,s):
seen=[False]*self.V
seen[s]=True
queue=deque([s])
while queue:
x=queue.popleft()
for y in self.graph[x]:
if self.weighted:
y,d=y
if not seen[y]:
seen[y]=True
queue.append(y)
return
def DFS(self,s):
seen=[False]*self.V
finished=[False]*self.V
stack=[(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]=True
stack.append((x,d) if self.weighted else x)
for y in self.graph[x]:
if self.weighted:
y,d=y
if not seen[y]:
stack.append((y,d) if self.weighted else y)
elif not finished[x]:
finished[x]=True
return
class Lazy_Segment_Tree:
def __init__(self,N,f,e,f_act,e_act,operate):
self.N=N
self.f=f
self.e=e
self.f_act=f_act
self.e_act=e_act
self.operate=operate
self.segment_tree=[self.e]*(self.N+self.N)
self.segment_tree_act=[self.e_act]*(self.N+self.N)
def __getitem__(self,i):
if type(i) is int:
if -self.N<=i<0:
i+=self.N*2
elif 0<=i<self.N:
i+=self.N
else:
raise IndexError('list index out of range')
self.Propagate_Above(i)
self.Recalculate_Above(i)
return self.Operate_At(i)
else:
a,b,c=i.start,i.stop,i.step
if a==None or a<-self.N:
a=self.N
elif self.N<=a:
a=self.N*2
elif a<0:
a+=self.N*2
else:
a+=self.N
if b==None or self.N<=b:
b=self.N*2
elif b<-self.N:
b=self.N
elif b<0:
b+=self.N*2
else:
b+=self.N
return self.segment_tree[slice(a,b,c)]
def __setitem__(self,i,x):
if -self.N<=i<0:
i+=self.N*2
elif 0<=i<self.N:
i+=self.N
else:
raise IndexError('list index out of range')
self.Propagate_Above(i)
self.segment_tree[i]=x
self.segment_tree_act[i]=self.e_act
self.Recalculate_Above(i)
def Operate_At(self,i):
return self.operate(self.segment_tree[i],self.segment_tree_act[i])
def Propagate_At(self,i):
self.segment_tree[i]=self.Operate_At(i)
self.segment_tree_act[i<<1]=self.f_act(self.segment_tree_act[i<<1],self.segment_tree_act[i])
self.segment_tree_act[i<<1|1]=self.f_act(self.segment_tree_act[i<<1|1],self.segment_tree_act[i])
self.segment_tree_act[i]=self.e_act
def Propagate_Above(self,i):
H=i.bit_length()-1
for h in range(H,0,-1):
self.Propagate_At(i>>h)
def Recalculate_Above(self,i):
while i>1:
i>>=1
self.segment_tree[i]=self.f(self.Operate_At(i<<1),self.Operate_At(i<<1|1))
def Build(self,lst):
for i,x in enumerate(lst,self.N):
self.segment_tree[i]=x
for i in range(self.N-1,0,-1):
self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])
self.segment_tree_act=[self.e_act]*(self.N+self.N)
def Fold(self,L=None,R=None):
if L==None or L<-self.N:
L=self.N
elif self.N<=L:
L=self.N*2
elif L<0:
L+=self.N*2
else:
L+=self.N
if R==None or self.N<=R:
R=self.N*2
elif R<-self.N:
R=self.N
elif R<0:
R+=self.N*2
else:
R+=self.N
self.Propagate_Above(L//(L&-L))
self.Propagate_Above(R//(R&-R)-1)
vL=self.e
vR=self.e
while L<R:
if L&1:
vL=self.f(vL,self.Operate_At(L))
L+=1
if R&1:
R-=1
vR=self.f(self.Operate_At(R),vR)
L>>=1
R>>=1
return self.f(vL,vR)
def Fold_Index(self,L=None,R=None):
if L==None or L<-self.N:
L=self.N
elif self.N<=L:
L=self.N*2
elif L<0:
L+=self.N*2
else:
L+=self.N
if R==None or self.N<=R:
R=self.N*2
elif R<-self.N:
R=self.N
elif R<0:
R+=self.N*2
else:
R+=self.N
if L==R:
return None
x=self.Fold(L-self.N,R-self.N)
while L<R:
if L&1:
if self.segment_tree[L]==x:
i=L
break
L+=1
if R&1:
R-=1
if self.segment_tree[R]==x:
i=R
break
L>>=1
R>>=1
while i<self.N:
if self.segment_tree[i]==self.segment_tree[i<<1]:
i<<=1
else:
i<<=1
i|=1
i-=self.N
return i
def Operate_Range(self,a,L=None,R=None):
if L==None or L<-self.N:
L=self.N
elif self.N<=L:
L=self.N*2
elif L<0:
L+=self.N*2
else:
L+=self.N
if R==None or self.N<=R:
R=self.N*2
elif R<-self.N:
R=self.N
elif R<0:
R+=self.N*2
else:
R+=self.N
L0=L//(L&-L)
R0=R//(R&-R)-1
self.Propagate_Above(L0)
self.Propagate_Above(R0)
while L<R:
if L&1:
self.segment_tree_act[L]=self.f_act(self.segment_tree_act[L],a)
L+=1
if R&1:
R-=1
self.segment_tree_act[R]=self.f_act(self.segment_tree_act[R],a)
L>>=1
R>>=1
self.Recalculate_Above(L0)
self.Recalculate_Above(R0)
def Update(self):
for i in range(1,self.N):
self.Propagate_At(i)
for i in range(self.N,self.N*2):
self.segment_tree[i]=self.Operate_At(i)
self.segment_tree_act[i]=self.e_act
for i in range(self.N-1,0,-1):
self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])
def __str__(self):
self.Update()
return '['+', '.join(map(str,[self.operate(x,a) for x,a in zip(self.segment_tree[self.N:],self.segment_tree_act[self.N:])]))+']'
N=int(readline())
inf=1<<30
edges=[]
for i in range(N-1):
u,v=map(int,readline().split())
edges.append((u,v))
G=Graph(N,edges=edges)
tour,parents,depth=G.SS_BFS(0,bfs_tour=True,parents=True,unweighted_dist=True)
L0,R0=[None]*N,[None]*N
L1,R1,L2,R2=[inf]*N,[-inf]*N,[inf]*N,[-inf]*N
for i in range(N):
x=tour[i]
L0[x]=i
R0[x]=i+1
if depth[x]>=1:
p=parents[x]
L1[p]=min(L1[p],i)
R1[p]=max(R1[p],i+1)
if depth[x]>=2:
p=parents[parents[x]]
L2[p]=min(L2[p],i)
R2[p]=max(R2[p],i+1)
def f(tpl0,tpl1):
x0,c0=tpl0
x1,c1=tpl1
return (x0+x1,c0+c1)
e=(0,0)
def f_act(a,b):
if b==None:
return a
return b
e_act=None
def operate(tpl,a):
x,c=tpl
return (x if a==None else a*c,c)
LST=Lazy_Segment_Tree(N,f,e,f_act,e_act,operate)
A=list(map(int,readline().split()))
LST.Build([(A[x],1) for x in tour])
Q=int(readline())
for _ in range(Q):
x=int(readline())
ans=0
if depth[x]>=1:
p=parents[x]
c=LST.Fold(L0[p],R0[p])[0]
if c:
ans+=c
LST.Operate_Range(0,L0[p],R0[p])
c=LST.Fold(L1[p],R1[p])[0]
if c:
ans+=c
LST.Operate_Range(0,L1[p],R1[p])
else:
c=LST.Fold(L0[x],R0[x])[0]
if c:
ans+=c
LST.Operate_Range(0,L0[x],R0[x])
if depth[x]>=2:
p=parents[parents[x]]
c=LST.Fold(L0[p],R0[p])[0]
if c:
ans+=c
LST.Operate_Range(0,L0[p],R0[p])
if L1[x]!=inf:
c=LST.Fold(L1[x],R1[x])[0]
if c:
ans+=c
LST.Operate_Range(0,L1[x],R1[x])
if L2[x]!=inf:
c=LST.Fold(L2[x],R2[x])[0]
if c:
ans+=c
LST.Operate_Range(0,L2[x],R2[x])
print(ans)
LST.Operate_Range(ans,L0[x],R0[x])
vwxyz