結果
問題 | No.2949 Product on Tree |
ユーザー | lif4635 |
提出日時 | 2024-10-25 21:35:50 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 752 ms / 2,000 ms |
コード長 | 3,306 bytes |
コンパイル時間 | 272 ms |
コンパイル使用メモリ | 82,036 KB |
実行使用メモリ | 187,416 KB |
最終ジャッジ日時 | 2024-10-25 21:36:25 |
合計ジャッジ時間 | 33,598 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 37 ms
54,240 KB |
testcase_01 | AC | 38 ms
53,292 KB |
testcase_02 | AC | 37 ms
52,872 KB |
testcase_03 | AC | 737 ms
144,544 KB |
testcase_04 | AC | 672 ms
143,120 KB |
testcase_05 | AC | 664 ms
144,916 KB |
testcase_06 | AC | 662 ms
144,840 KB |
testcase_07 | AC | 630 ms
143,504 KB |
testcase_08 | AC | 681 ms
144,992 KB |
testcase_09 | AC | 650 ms
145,060 KB |
testcase_10 | AC | 666 ms
144,808 KB |
testcase_11 | AC | 671 ms
146,472 KB |
testcase_12 | AC | 679 ms
148,080 KB |
testcase_13 | AC | 647 ms
150,948 KB |
testcase_14 | AC | 673 ms
152,000 KB |
testcase_15 | AC | 657 ms
150,956 KB |
testcase_16 | AC | 689 ms
155,504 KB |
testcase_17 | AC | 692 ms
153,492 KB |
testcase_18 | AC | 668 ms
153,564 KB |
testcase_19 | AC | 711 ms
155,344 KB |
testcase_20 | AC | 669 ms
155,140 KB |
testcase_21 | AC | 704 ms
156,108 KB |
testcase_22 | AC | 634 ms
152,560 KB |
testcase_23 | AC | 695 ms
145,884 KB |
testcase_24 | AC | 667 ms
146,128 KB |
testcase_25 | AC | 704 ms
146,012 KB |
testcase_26 | AC | 706 ms
146,272 KB |
testcase_27 | AC | 659 ms
145,880 KB |
testcase_28 | AC | 685 ms
146,132 KB |
testcase_29 | AC | 660 ms
146,164 KB |
testcase_30 | AC | 684 ms
146,276 KB |
testcase_31 | AC | 671 ms
146,792 KB |
testcase_32 | AC | 695 ms
149,852 KB |
testcase_33 | AC | 689 ms
154,840 KB |
testcase_34 | AC | 725 ms
157,256 KB |
testcase_35 | AC | 733 ms
158,480 KB |
testcase_36 | AC | 703 ms
159,440 KB |
testcase_37 | AC | 741 ms
159,972 KB |
testcase_38 | AC | 708 ms
159,832 KB |
testcase_39 | AC | 724 ms
159,976 KB |
testcase_40 | AC | 752 ms
160,208 KB |
testcase_41 | AC | 683 ms
161,992 KB |
testcase_42 | AC | 732 ms
159,196 KB |
testcase_43 | AC | 321 ms
150,840 KB |
testcase_44 | AC | 321 ms
153,892 KB |
testcase_45 | AC | 431 ms
187,416 KB |
testcase_46 | AC | 367 ms
166,348 KB |
testcase_47 | AC | 277 ms
137,828 KB |
testcase_48 | AC | 403 ms
165,424 KB |
ソースコード
#int-input def II() -> int : return int(input()) def MI() -> int : return map(int, input().split()) class fenwick_tree(): n=1 data=[0 for i in range(n)] def __init__(self,N): self.n=N self.data=[0 for i in range(N)] def add(self,p,x): assert 0<=p<self.n,"0<=p<n,p={0},n={1}".format(p,self.n) p+=1 while(p<=self.n): self.data[p-1]+=x p+=p& -p def sum(self,l,r): assert (0<=l and l<=r and r<=self.n),"0<=l<=r<=n,l={0},r={1},n={2}".format(l,r,self.n) return self.sum0(r)-self.sum0(l) def sum0(self,r): s=0 while(r>0): s+=self.data[r-1] r-=r&-r return s class dsu(): n=1 parent_or_size=[-1 for i in range(n)] def __init__(self,N): self.n=N self.parent_or_size=[-1 for i in range(N)] def merge(self,a,b): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n) x=self.leader(a) y=self.leader(b) if x==y: return x if (-self.parent_or_size[x]<-self.parent_or_size[y]): x,y=y,x self.parent_or_size[x]+=self.parent_or_size[y] self.parent_or_size[y]=x return x def same(self,a,b): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n) return self.leader(a)==self.leader(b) def leader(self,a): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) if (self.parent_or_size[a]<0): return a self.parent_or_size[a]=self.leader(self.parent_or_size[a]) return self.parent_or_size[a] def size(self,a): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) return -self.parent_or_size[self.leader(a)] def groups(self): leader_buf=[0 for i in range(self.n)] group_size=[0 for i in range(self.n)] for i in range(self.n): leader_buf[i]=self.leader(i) group_size[leader_buf[i]]+=1 result=[[] for i in range(self.n)] for i in range(self.n): result[leader_buf[i]].append(i) result2=[] for i in range(self.n): if len(result[i])>0: result2.append(result[i]) return result2 n = II() a = list(MI()) edge = [set() for i in range(n)] for i in range(n-1): u,v = MI() u -= 1 v -= 1 edge[u].add(v) edge[v].add(u) mod = 998244353 def DFS_par(edge): #親の情報をうまく持つDFS ans = 0 cnt = [0]*len(edge) #親を持ちながら非再帰DFS n = len(edge) st = [(0,None,0)] while st: # print(cnt) now,par,t = st.pop() if t == 0: st.append((now,par,1)) #行きがけ for chi in edge[now]: if chi != par: st.append((chi,now,0)) else: ans += cnt[now]*a[now]%mod if par != None: tmp = a[now]*(cnt[now]+1)%mod ans += cnt[par]*a[par]*tmp%mod cnt[par] += tmp pass #帰りがけ return ans print(DFS_par(edge)%mod)