結果
問題 | No.2949 Product on Tree |
ユーザー | GOTKAKO |
提出日時 | 2024-10-25 22:08:34 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,335 ms / 2,000 ms |
コード長 | 3,953 bytes |
コンパイル時間 | 2,661 ms |
コンパイル使用メモリ | 218,324 KB |
実行使用メモリ | 30,308 KB |
最終ジャッジ日時 | 2024-10-25 22:09:40 |
合計ジャッジ時間 | 49,982 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,820 KB |
testcase_03 | AC | 780 ms
15,820 KB |
testcase_04 | AC | 821 ms
15,324 KB |
testcase_05 | AC | 917 ms
15,816 KB |
testcase_06 | AC | 849 ms
15,800 KB |
testcase_07 | AC | 868 ms
15,452 KB |
testcase_08 | AC | 833 ms
16,080 KB |
testcase_09 | AC | 1,010 ms
16,196 KB |
testcase_10 | AC | 978 ms
16,832 KB |
testcase_11 | AC | 1,057 ms
17,908 KB |
testcase_12 | AC | 1,047 ms
19,864 KB |
testcase_13 | AC | 1,147 ms
20,568 KB |
testcase_14 | AC | 1,107 ms
21,984 KB |
testcase_15 | AC | 1,141 ms
24,204 KB |
testcase_16 | AC | 1,096 ms
22,444 KB |
testcase_17 | AC | 1,248 ms
23,196 KB |
testcase_18 | AC | 1,218 ms
23,000 KB |
testcase_19 | AC | 1,254 ms
25,124 KB |
testcase_20 | AC | 1,300 ms
24,612 KB |
testcase_21 | AC | 1,309 ms
24,860 KB |
testcase_22 | AC | 1,240 ms
26,016 KB |
testcase_23 | AC | 821 ms
16,028 KB |
testcase_24 | AC | 884 ms
16,020 KB |
testcase_25 | AC | 911 ms
15,920 KB |
testcase_26 | AC | 927 ms
16,012 KB |
testcase_27 | AC | 964 ms
16,196 KB |
testcase_28 | AC | 943 ms
16,232 KB |
testcase_29 | AC | 1,001 ms
16,640 KB |
testcase_30 | AC | 1,076 ms
17,048 KB |
testcase_31 | AC | 1,108 ms
18,040 KB |
testcase_32 | AC | 1,146 ms
19,404 KB |
testcase_33 | AC | 1,211 ms
21,936 KB |
testcase_34 | AC | 1,284 ms
26,288 KB |
testcase_35 | AC | 1,229 ms
23,648 KB |
testcase_36 | AC | 1,280 ms
28,024 KB |
testcase_37 | AC | 1,330 ms
28,404 KB |
testcase_38 | AC | 1,295 ms
25,328 KB |
testcase_39 | AC | 1,335 ms
25,876 KB |
testcase_40 | AC | 1,317 ms
29,872 KB |
testcase_41 | AC | 1,276 ms
27,344 KB |
testcase_42 | AC | 1,274 ms
30,308 KB |
testcase_43 | AC | 93 ms
15,008 KB |
testcase_44 | AC | 93 ms
15,220 KB |
testcase_45 | AC | 120 ms
18,368 KB |
testcase_46 | AC | 107 ms
16,832 KB |
testcase_47 | AC | 80 ms
13,188 KB |
testcase_48 | AC | 111 ms
17,264 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; long long mod = 998244353; //入力が必ず-mod<a<modの時. struct mint{ long long v = 0; mint(){} mint(int a){v = a<0?a+mod:a;} mint(long long a){v = a<0?a+mod:a;} mint(unsigned long long a){v = a;} long long val(){return v;} void modu(){v %= mod;} mint repeat2mint(const long long a,long long b){ mint ret = 1,p = a; while(b){ if(b&1) ret *= p; p *= p; b >>= 1; } return ret; }; mint &operator=(const mint &b) = default; mint operator-() const {return mint(0)-(*this);} mint operator+(const mint b){return mint(v)+=b;} mint operator-(const mint b){return mint(v)-=b;} mint operator*(const mint b){return mint(v)*=b;} mint operator/(const mint b){return mint(v)/=b;} mint operator+=(const mint b){ v += b.v; if(v >= mod) v -= mod; return *this; } mint operator-=(const mint b){ v -= b.v; if(v < 0) v += mod; return *this; } mint operator*=(const mint b){v = v*b.v%mod; return *this;} mint operator/=(mint b){ if(b == 0) assert(false); int left = mod-2; while(left){if(left&1) *this *= b; b *= b; left >>= 1;} return *this; } mint operator++(int){*this += 1; return *this;} mint operator--(int){*this -= 1; return *this;} bool operator==(const mint b){return v == b.v;} bool operator!=(const mint b){return v != b.v;} bool operator>(const mint b){return v > b.v;} bool operator>=(const mint b){return v >= b.v;} bool operator<(const mint b){return v < b.v;} bool operator<=(const mint b){return v <= b.v;} mint pow(const long long x){return repeat2mint(v,x);} mint inv(){return mint(1)/v;} }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<mint> A(N); for(auto &a : A) cin >> a.v; vector<vector<int>> Graph(N); for(int i=0; i<N-1; i++){ int u,v; cin >> u >> v; u--; v--; Graph.at(u).push_back(v); Graph.at(v).push_back(u); } vector<bool> stop(N); auto findsiz = [&](auto&& self,int pos,int back) -> int { if(stop.at(pos)) return 0; int ret = 1; for(auto to : Graph.at(pos)) if(to != back) ret += self(self,to,pos); return ret; }; auto findcent = [&](int start) -> int { int siz = findsiz(findsiz,start,-1); int cent = -1; auto dfs = [&](auto dfs,int pos,int back) -> int { if(stop.at(pos)) return 0; int ret = 0; bool iscent = true; for(auto to : Graph.at(pos)) if(to != back){ int ko = dfs(dfs,to,pos); if(ko > siz/2) iscent = false; ret += ko; } ret++; if(siz-ret > siz/2) iscent = false; if(iscent) cent = pos; return ret; }; dfs(dfs,start,-1); return cent; }; mint answer = 0; queue<int> Q; Q.push(0); mint div2 = mint(1)/2; while(Q.size()){ int start = Q.front(); Q.pop(); int cent = findcent(start); int n = Graph.at(cent).size(); vector<mint> sum(n); auto dfs = [&](auto dfs,int pos,int back,mint now,int i) -> void { if(stop.at(pos)) return; now *= A.at(pos); sum.at(i) += now; for(auto to : Graph.at(pos)) if(to != back) dfs(dfs,to,pos,now,i); }; for(int i=0; i<n; i++) dfs(dfs,Graph.at(cent).at(i),cent,1,i); mint all = 0; for(auto v : sum) all += v,answer += A.at(cent)*v; for(auto v : sum) answer += A.at(cent)*(all*v-v*v)*div2; stop.at(cent) = true; for(int i=0; i<n; i++){ int to = Graph.at(cent).at(i); if(!stop.at(to)) Q.push(to); } } cout << answer.v << endl; }