結果
問題 | No.899 γatheree |
ユーザー | vwxyz |
提出日時 | 2024-09-14 04:06:46 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,866 ms / 2,000 ms |
コード長 | 11,791 bytes |
コンパイル時間 | 252 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 145,116 KB |
最終ジャッジ日時 | 2024-09-14 04:07:23 |
合計ジャッジ時間 | 34,783 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 58 ms
58,240 KB |
testcase_01 | AC | 106 ms
76,800 KB |
testcase_02 | AC | 102 ms
77,056 KB |
testcase_03 | AC | 104 ms
76,672 KB |
testcase_04 | AC | 104 ms
76,928 KB |
testcase_05 | AC | 104 ms
77,056 KB |
testcase_06 | AC | 1,835 ms
143,308 KB |
testcase_07 | AC | 1,861 ms
143,548 KB |
testcase_08 | AC | 1,866 ms
143,244 KB |
testcase_09 | AC | 1,821 ms
143,348 KB |
testcase_10 | AC | 1,780 ms
143,268 KB |
testcase_11 | AC | 1,749 ms
143,068 KB |
testcase_12 | AC | 1,757 ms
143,332 KB |
testcase_13 | AC | 1,823 ms
143,172 KB |
testcase_14 | AC | 1,820 ms
143,368 KB |
testcase_15 | AC | 1,794 ms
143,364 KB |
testcase_16 | AC | 1,800 ms
143,324 KB |
testcase_17 | AC | 1,777 ms
143,784 KB |
testcase_18 | AC | 1,758 ms
143,332 KB |
testcase_19 | AC | 1,738 ms
143,100 KB |
testcase_20 | AC | 1,802 ms
143,496 KB |
testcase_21 | AC | 1,638 ms
144,060 KB |
testcase_22 | AC | 1,655 ms
145,116 KB |
testcase_23 | AC | 1,696 ms
144,992 KB |
ソースコード
import heapq import random from collections import defaultdict,deque class Graph: def __init__(self,V,edges=None,graph=None,directed=False,weighted=False,inf=float("inf")): self.V=V self.directed=directed self.weighted=weighted self.inf=inf if graph!=None: 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)) """ else: 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) def SIV_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 if unweighted_dist or bipartite_graph: uwd=[self.inf]*self.V uwd[s]=0 if weighted_dist: wd=[self.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 uwd[i]==self.inf or uwd[j]==self.inf: continue if not uwd[i]%2^uwd[j]%2: bg=False break else: for x in range(self.V): if uwd[x]==self.inf: continue bg[uwd[x]%2].append(x) retu=() if bfs_tour: retu+=(bt,) if bipartite_graph: retu+=(bg,) if linked_components: retu+=(lc,) if parents: retu+=(ps,) if unweighted_dist: retu+=(uwd,) if weighted_dist: retu+=(wd,) if len(retu)==1: retu=retu[0] return retu def Build_BFSD(self,D): self.bfsd_D=D self.bfsd_L=[[self.V]*(self.bfsd_D+1) for x in range(self.V)] self.bfsd_R=[[0]*(self.bfsd_D+1) for x in range(self.V)] self.bfsd_tour,self.bfsd_parents=self.SIV_BFS(0,bfs_tour=True,parents=True) self.bfsd_idx=[None]*self.V for i,x in enumerate(self.bfsd_tour): self.bfsd_idx[x]=i for i in range(self.V-1,-1,-1): x=self.bfsd_tour[i] self.bfsd_L[x][0]=i self.bfsd_R[x][0]=i+1 for y in self.graph[x]: if y==self.bfsd_parents[x]: continue for d in range(1,self.bfsd_D+1): self.bfsd_L[x][d]=min(self.bfsd_L[x][d],self.bfsd_L[y][d-1]) self.bfsd_R[x][d]=max(self.bfsd_R[x][d],self.bfsd_R[y][d-1]) def BFSD(self,x,d,annular=False): assert d<=self.bfsd_D bfsd=[] def push_LR(l,r): if l<r: bfsd.append((l,r)) if annular: push_LR(self.bfsd_L[x][d],self.bfsd_R[x][d]) while d: d-=1 if self.bfsd_parents[x]==None: break p=self.bfsd_parents[x] l,r=self.bfsd_L[p][d],self.bfsd_R[p][d] if l<r: if d: ll,rr=self.bfsd_L[x][d-1],self.bfsd_R[x][d-1] if ll<rr: push_LR(l,ll) push_LR(rr,r) else: push_LR(l,r) else: push_LR(l,r) x=p else: push_LR(self.bfsd_L[x][d],self.bfsd_R[x][d]) d1=0 for d0 in range(d-1,-1,-1): if d0-d1<0: break push_LR(self.bfsd_L[x][d0-d1],self.bfsd_R[x][d0-d1]) if self.bfsd_parents[x]==None: d1+=1 if d0-d1<0: break else: x=self.bfsd_parents[x] push_LR(self.bfsd_L[x][d0-d1],self.bfsd_R[x][d0-d1]) return bfsd class Segment_Tree: def __init__(self,N,f,e,lst=None,dynamic=False,bisect_search=True): self.f=f self.e=e self.N=N self.bisect_search=bisect_search if self.bisect_search: self.le=1 while self.le<self.N: self.le*=2 else: self.le=self.N if dynamic: self.segment_tree=defaultdict(lambda:self.e) else: if lst==None: self.segment_tree=[self.e]*2*self.le else: assert len(lst)<=self.N self.segment_tree=[self.e]*self.le+[x for x in lst]+[self.e]*(self.le-len(lst)) for i in range(self.le-1,0,-1): self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) def __getitem__(self,i): if type(i)==int: if -self.le<=i<0: return self.segment_tree[i+self.le*2] elif 0<=i<self.le: return self.segment_tree[i+self.le] else: raise IndexError("list index out of range") else: a,b,c=i.start,i.stop,i.step if a==None: a=self.le else: a+=self.le if b==None: b=self.le*2 else: b+=self.le return self.segment_tree[slice(a,b,c)] def __setitem__(self,i,x): if -self.le<=i<0: i+=self.le*2 elif 0<=i<self.le: i+=self.le else: raise IndexError("list index out of range") self.segment_tree[i]=x while i>1: i>>= 1 self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) def Build(self,lst): for i,x in enumerate(lst,self.le): self.segment_tree[i]=x for i in range(self.le-1,0,-1): self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) def Fold(self,L=None,R=None): if L==None: L=self.le else: assert 0<=L<=self.N L+=self.le if R==None: R=self.le*2 else: assert 0<=R<=self.N R+=self.le vL=self.e vR=self.e while L<R: if L&1: vL=self.f(vL,self.segment_tree[L]) L+=1 if R&1: R-=1 vR=self.f(self.segment_tree[R],vR) L>>=1 R>>=1 return self.f(vL,vR) def Fold_Index(self,L=None,R=None): if L==None: L=self.le else: assert 0<=L<=self.N L+=self.le if R==None: R=self.le*2 else: assert 0<=R<=self.N R+=self.le if L==R: return None x=self.Fold(L-self.le,R-self.le) 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.le: if self.segment_tree[i]==self.segment_tree[i<<1]: i<<=1 else: i<<=1 i|=1 i-=self.le return i def Bisect_Right(self,L=None,f=None): assert self.bisect_search if L==self.le: return self.le if L==None: L=0 assert 0<=L<=self.N L+=self.le vl=self.e vr=self.e l,r=L,self.le*2 while l<r: if l&1: vl=self.f(vl,self.segment_tree[l]) l+=1 if r&1: r-=1 vr=self.f(self.segment_tree[r],vr) l>>=1 r>>=1 if f(self.f(vl,vr)): return self.N v=self.e while True: while L%2==0: L>>=1 vv=self.f(v,self.segment_tree[L]) if f(vv): v=vv L+=1 else: while L<self.le: L<<=1 vv=self.f(v,self.segment_tree[L]) if f(vv): v=vv L+=1 return L-self.le def Bisect_Left(self,R=None,f=None): assert self.bisect_search if R==0: return 0 if R==None: R=self.le assert 0<=R<=self.N R+=self.le vl=self.e vr=self.e l,r=self.le,R while l<r: if l&1: vl=self.f(vl,self.segment_tree[l]) l+=1 if r&1: r-=1 vr=self.f(self.segment_tree[r],vr) l>>=1 r>>=1 if f(self.f(vl,vr)): return 0 v=self.e while True: R-=1 while R>1 and R%2: R>>=1 vv=self.f(self.segment_tree[R],v) if f(vv): v=vv else: while R<self.le: R=2*R+1 vv=self.f(self.segment_tree[R],v) if f(vv): v=vv R-=1 return R+1-self.le def __str__(self): return "["+", ".join(map(str,[self.segment_tree[i] for i in range(self.le,self.le+self.N)]))+"]" N=int(input()) edges=[] for i in range(N-1): u,v=map(int,input().split()) edges.append((u,v)) G=Graph(N,edges=edges) G.Build_BFSD(2) A=list(map(int,input().split())) A=Segment_Tree(N,lambda x,y:x+y,0,[A[G.bfsd_tour[i]] for i in range(N)]) Q=int(input()) for q in range(Q): x=int(input()) ans=0 for d in range(3): for l,r in G.BFSD(x,d): while l<r: l=A.Bisect_Right(l,lambda s:s==0) if l<r: ans+=A[l] A[l]=0 ans+=A[G.bfsd_idx[x]] print(ans) A[G.bfsd_idx[x]]=ans