結果
問題 | No.2115 Making Forest Easy |
ユーザー | 沙耶花 |
提出日時 | 2022-10-28 22:41:20 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 710 ms / 2,000 ms |
コード長 | 2,719 bytes |
コンパイル時間 | 4,919 ms |
コンパイル使用メモリ | 274,108 KB |
実行使用メモリ | 359,336 KB |
最終ジャッジ日時 | 2024-07-06 01:56:04 |
合計ジャッジ時間 | 25,910 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,812 KB |
testcase_02 | AC | 694 ms
358,804 KB |
testcase_03 | AC | 421 ms
102,164 KB |
testcase_04 | AC | 630 ms
279,332 KB |
testcase_05 | AC | 513 ms
208,856 KB |
testcase_06 | AC | 700 ms
358,872 KB |
testcase_07 | AC | 461 ms
50,756 KB |
testcase_08 | AC | 709 ms
358,804 KB |
testcase_09 | AC | 417 ms
43,964 KB |
testcase_10 | AC | 705 ms
359,336 KB |
testcase_11 | AC | 427 ms
103,504 KB |
testcase_12 | AC | 418 ms
43,984 KB |
testcase_13 | AC | 386 ms
91,720 KB |
testcase_14 | AC | 416 ms
44,028 KB |
testcase_15 | AC | 460 ms
49,072 KB |
testcase_16 | AC | 431 ms
95,256 KB |
testcase_17 | AC | 44 ms
12,544 KB |
testcase_18 | AC | 120 ms
62,500 KB |
testcase_19 | AC | 377 ms
187,512 KB |
testcase_20 | AC | 381 ms
42,944 KB |
testcase_21 | AC | 49 ms
23,168 KB |
testcase_22 | AC | 91 ms
24,268 KB |
testcase_23 | AC | 513 ms
243,304 KB |
testcase_24 | AC | 429 ms
94,184 KB |
testcase_25 | AC | 256 ms
99,924 KB |
testcase_26 | AC | 418 ms
43,996 KB |
testcase_27 | AC | 229 ms
51,652 KB |
testcase_28 | AC | 458 ms
50,784 KB |
testcase_29 | AC | 81 ms
42,112 KB |
testcase_30 | AC | 461 ms
49,100 KB |
testcase_31 | AC | 641 ms
276,100 KB |
testcase_32 | AC | 706 ms
359,228 KB |
testcase_33 | AC | 710 ms
358,604 KB |
testcase_34 | AC | 55 ms
15,104 KB |
testcase_35 | AC | 161 ms
83,020 KB |
testcase_36 | AC | 707 ms
358,648 KB |
testcase_37 | AC | 219 ms
48,732 KB |
testcase_38 | AC | 122 ms
30,060 KB |
testcase_39 | AC | 290 ms
147,420 KB |
testcase_40 | AC | 457 ms
48,696 KB |
testcase_41 | AC | 417 ms
43,848 KB |
testcase_42 | AC | 424 ms
103,248 KB |
testcase_43 | AC | 211 ms
28,152 KB |
testcase_44 | AC | 122 ms
30,896 KB |
testcase_45 | AC | 344 ms
38,624 KB |
testcase_46 | AC | 431 ms
102,300 KB |
testcase_47 | AC | 186 ms
24,260 KB |
testcase_48 | AC | 635 ms
266,392 KB |
testcase_49 | AC | 649 ms
287,224 KB |
testcase_50 | AC | 268 ms
64,732 KB |
testcase_51 | AC | 175 ms
40,460 KB |
ソースコード
#include <stdio.h> #include <atcoder/all> #include <bits/stdc++.h> using namespace std; using namespace atcoder; using mint = modint998244353; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf32 1000000001 #define Inf64 4000000000000000001 template <class Ss, Ss (*op0)(Ss, Ss), Ss (*e)(), class Sv, Ss (*op1)(Sv, Ss), class Se, Ss (*op2)(Se, Ss)> struct rerooting{ int n; vector<vector<int>> to; vector<vector<Se>> edge; vector<Ss> subtree,answer; vector<Sv> vertex; rerooting(int _n){ n = _n; to.resize(n); edge.resize(n); subtree.resize(n,e()); answer.resize(n,e()); vertex.resize(n); } rerooting(vector<Sv> v){ n = v.size(); to.resize(n); edge.resize(n); subtree.resize(n,e()); answer.resize(n,e()); vertex = v; } void add_directed_edge(int u,int v,Se x){ to[u].push_back(v); edge[u].push_back(x); } void add_edge(int u,int v,Se x){ add_directed_edge(u,v,x); add_directed_edge(v,u,x); } void solve(){ dfs(0,-1); redfs(0,-1,e()); } void dfs(int cur,int par){ Ss temp = e(); for(int i=0;i<to[cur].size();i++){ int nxt = to[cur][i]; if(nxt==par)continue; dfs(nxt,cur); temp = op0(temp, op2(edge[cur][i], subtree[nxt])); } subtree[cur] = op1(vertex[cur], temp); } void redfs(int cur, int par, Ss ps){ vector<Ss> P(to[cur].size()+1,e()); rep(i,to[cur].size()){ int nxt = to[cur][i]; if(nxt!=par)P[i+1] = op2(edge[cur][i], subtree[nxt]); else P[i+1] = op2(edge[cur][i], ps); } vector<Ss> S(P.rbegin(),P.rend()-1); S.insert(S.begin(),e()); rep(i,to[cur].size()){ P[i+1] = op0(P[i], P[i+1]); S[i+1] = op0(S[i+1], S[i]); } answer[cur] = op1(vertex[cur], P.back()); for(int i=0;i<to[cur].size();i++){ int nxt = to[cur][i]; if(nxt!=par){ redfs(nxt,cur,op1(vertex[cur], op0(P[i],S[to[cur].size()-1-i]))); } } } }; using A = array<mint,1005>; A e(){ A ret; rep(i,1005)ret[i] = 0; ret[0] = 1; return ret; } A op0(A a,A b){ rep(i,1004){ a[i+1] += a[i]; b[i+1] += b[i]; } A ret = e(); ret[0] = 0; rep(i,1005){ if(i==0)ret[i] = a[i]*b[i]; else{ ret[i] += (a[i]-a[i-1]) * (b[i]-b[i-1]); ret[i] += (a[i]-a[i-1]) * b[i-1]; ret[i] += a[i-1] * (b[i]-b[i-1]); } } return ret; } A op1(int a,A b){ rep(i,a){ b[a] += b[i]; b[i] = 0; } return b; } A op2(int a,A b){ mint s = 0; rep(i,1003)s += b[i]; b[0] += s; return b; } int main(){ int n; cin>>n; vector<int> a(n); rep(i,n)cin>>a[i]; rerooting<A,op0,e,int,op1,int,op2> R(a); rep(i,n-1){ int u,v; cin>>u>>v; R.add_edge(u-1,v-1,1); } R.solve(); mint ans = 0; rep(i,n){ rep(j,1003){ ans += R.answer[i][j] * j; } } cout<<ans.val()<<endl; return 0; }