結果
問題 | No.2949 Product on Tree |
ユーザー | liveworldlike |
提出日時 | 2024-10-25 23:02:15 |
言語 | C++23(gcc13) (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,474 ms / 2,000 ms |
コード長 | 2,094 bytes |
コンパイル時間 | 1,477 ms |
コンパイル使用メモリ | 89,604 KB |
実行使用メモリ | 107,264 KB |
最終ジャッジ日時 | 2024-10-25 23:03:28 |
合計ジャッジ時間 | 55,911 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,820 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,820 KB |
testcase_03 | AC | 1,156 ms
52,736 KB |
testcase_04 | AC | 1,122 ms
51,712 KB |
testcase_05 | AC | 1,143 ms
53,120 KB |
testcase_06 | AC | 1,221 ms
53,248 KB |
testcase_07 | AC | 1,140 ms
52,224 KB |
testcase_08 | AC | 1,213 ms
54,144 KB |
testcase_09 | AC | 1,174 ms
55,168 KB |
testcase_10 | AC | 1,148 ms
56,704 KB |
testcase_11 | AC | 1,195 ms
59,392 KB |
testcase_12 | AC | 1,186 ms
67,968 KB |
testcase_13 | AC | 1,151 ms
69,632 KB |
testcase_14 | AC | 1,186 ms
75,904 KB |
testcase_15 | AC | 1,179 ms
84,480 KB |
testcase_16 | AC | 1,251 ms
77,824 KB |
testcase_17 | AC | 1,234 ms
80,384 KB |
testcase_18 | AC | 1,255 ms
79,744 KB |
testcase_19 | AC | 1,259 ms
87,808 KB |
testcase_20 | AC | 1,255 ms
85,888 KB |
testcase_21 | AC | 1,244 ms
86,784 KB |
testcase_22 | AC | 1,210 ms
90,624 KB |
testcase_23 | AC | 1,169 ms
53,760 KB |
testcase_24 | AC | 1,195 ms
53,888 KB |
testcase_25 | AC | 1,218 ms
54,144 KB |
testcase_26 | AC | 1,231 ms
54,016 KB |
testcase_27 | AC | 1,237 ms
54,528 KB |
testcase_28 | AC | 1,282 ms
54,784 KB |
testcase_29 | AC | 1,168 ms
55,552 KB |
testcase_30 | AC | 1,196 ms
57,728 KB |
testcase_31 | AC | 1,176 ms
61,952 KB |
testcase_32 | AC | 1,274 ms
63,360 KB |
testcase_33 | AC | 1,447 ms
76,672 KB |
testcase_34 | AC | 1,348 ms
92,672 KB |
testcase_35 | AC | 1,474 ms
81,920 KB |
testcase_36 | AC | 1,390 ms
98,688 KB |
testcase_37 | AC | 1,353 ms
99,968 KB |
testcase_38 | AC | 1,287 ms
89,216 KB |
testcase_39 | AC | 1,350 ms
90,752 KB |
testcase_40 | AC | 1,370 ms
105,856 KB |
testcase_41 | AC | 1,325 ms
96,000 KB |
testcase_42 | AC | 1,335 ms
107,264 KB |
testcase_43 | AC | 228 ms
39,504 KB |
testcase_44 | AC | 247 ms
40,116 KB |
testcase_45 | AC | 305 ms
50,696 KB |
testcase_46 | AC | 275 ms
45,656 KB |
testcase_47 | AC | 200 ms
34,300 KB |
testcase_48 | AC | 279 ms
47,204 KB |
ソースコード
#pragma GCC optimize("O3") #include <map> #include <stdio.h> #include <stdlib.h> #include <vector> using namespace std; typedef int64_t ll; const ll MOD = 1000000007; const ll MOD2 = 998244353; const ll mod = MOD2; constexpr ll fastpow(ll x, ll p) { ll res = 1; while (p) { if (p & 1) { res *= x; res %= mod; } x *= x; x %= mod; p >>= 1; } return res; } const ll inv2 = fastpow(2, mod - 2); void solve() { ll n; scanf("%ld", &n); ll *a = (ll *)malloc(n * sizeof(ll)); for (ll i = 0; i < n; i++) { scanf("%ld", &a[i]); } ll *u = (ll *)malloc(n * sizeof(ll)); ll *v = (ll *)malloc(n * sizeof(ll)); for (ll i = 0; i < n - 1; i++) { scanf("%ld %ld", &u[i], &v[i]); u[i]--; v[i]--; } map<ll, vector<ll>> N; for (ll i = 0; i < n - 1; i++) { N[u[i]].push_back(v[i]); N[v[i]].push_back(u[i]); } map<ll, vector<ll>> C; auto child = [&](ll p, ll u, auto self) -> void { C[p].push_back(u); for (ll v : N[u]) { if (v == p) continue; self(u, v, self); } }; child(-1, 0, child); vector<ll> F(n, -1); auto f = [&](ll u, auto self) -> ll { if (F[u] == -1) { F[u] = a[u]; F[u] %= mod; for (ll v : C[u]) { F[u] += a[u] * self(v, self); F[u] %= mod; } } return F[u]; }; vector<ll> G(n, -1); auto g = [&](ll u) -> ll { if (G[u] == -1) { ll S = 0, s = 0; for (ll v : C[u]) { ll value = f(v, f); S += value; S %= mod; s += value * value; s %= mod; } S *= S; S %= mod; G[u] = S; G[u] += mod - s; G[u] %= mod; G[u] *= a[u]; G[u] %= mod; G[u] *= inv2; G[u] %= mod; } return G[u]; }; ll res = 0; auto h = [&](ll u, auto self) -> void { res += g(u); res %= mod; res += f(u, f); res %= mod; for (ll v : C[u]) { self(v, self); } }; h(0, h); for (ll i = 0; i < n; i++) { res += mod - a[i]; res %= mod; } printf("%ld\n", res); } int main() { solve(); }