結果
問題 | No.2115 Making Forest Easy |
ユーザー | kwm_t |
提出日時 | 2022-10-29 17:43:25 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 584 ms / 2,000 ms |
コード長 | 3,972 bytes |
コンパイル時間 | 4,450 ms |
コンパイル使用メモリ | 280,396 KB |
実行使用メモリ | 242,176 KB |
最終ジャッジ日時 | 2024-07-06 15:24:46 |
合計ジャッジ時間 | 25,390 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,820 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 567 ms
241,920 KB |
testcase_03 | AC | 480 ms
102,272 KB |
testcase_04 | AC | 547 ms
199,936 KB |
testcase_05 | AC | 463 ms
153,984 KB |
testcase_06 | AC | 581 ms
241,920 KB |
testcase_07 | AC | 458 ms
48,640 KB |
testcase_08 | AC | 578 ms
241,920 KB |
testcase_09 | AC | 429 ms
43,904 KB |
testcase_10 | AC | 573 ms
242,048 KB |
testcase_11 | AC | 482 ms
102,144 KB |
testcase_12 | AC | 434 ms
43,776 KB |
testcase_13 | AC | 435 ms
91,776 KB |
testcase_14 | AC | 445 ms
43,776 KB |
testcase_15 | AC | 449 ms
47,232 KB |
testcase_16 | AC | 492 ms
102,400 KB |
testcase_17 | AC | 45 ms
12,416 KB |
testcase_18 | AC | 98 ms
43,008 KB |
testcase_19 | AC | 312 ms
128,256 KB |
testcase_20 | AC | 375 ms
41,088 KB |
testcase_21 | AC | 42 ms
17,664 KB |
testcase_22 | AC | 101 ms
23,552 KB |
testcase_23 | AC | 428 ms
168,448 KB |
testcase_24 | AC | 489 ms
102,272 KB |
testcase_25 | AC | 223 ms
74,752 KB |
testcase_26 | AC | 438 ms
43,648 KB |
testcase_27 | AC | 257 ms
55,680 KB |
testcase_28 | AC | 450 ms
48,512 KB |
testcase_29 | AC | 66 ms
29,440 KB |
testcase_30 | AC | 452 ms
47,456 KB |
testcase_31 | AC | 548 ms
198,016 KB |
testcase_32 | AC | 578 ms
242,176 KB |
testcase_33 | AC | 584 ms
241,792 KB |
testcase_34 | AC | 58 ms
14,720 KB |
testcase_35 | AC | 130 ms
56,960 KB |
testcase_36 | AC | 582 ms
241,664 KB |
testcase_37 | AC | 243 ms
52,608 KB |
testcase_38 | AC | 132 ms
29,952 KB |
testcase_39 | AC | 236 ms
100,096 KB |
testcase_40 | AC | 455 ms
47,232 KB |
testcase_41 | AC | 437 ms
43,776 KB |
testcase_42 | AC | 477 ms
102,144 KB |
testcase_43 | AC | 205 ms
26,368 KB |
testcase_44 | AC | 131 ms
29,824 KB |
testcase_45 | AC | 343 ms
37,120 KB |
testcase_46 | AC | 479 ms
102,016 KB |
testcase_47 | AC | 182 ms
22,912 KB |
testcase_48 | AC | 552 ms
193,024 KB |
testcase_49 | AC | 557 ms
204,160 KB |
testcase_50 | AC | 301 ms
64,640 KB |
testcase_51 | AC | 191 ms
42,496 KB |
ソースコード
#include <bits/stdc++.h> #include <atcoder/all> using namespace std; using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; using mint = modint998244353; const int mod = 998244353; //const int INF = 1e9; //const long long LINF = 1e18; //const bool debug = false; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" #define P pair<long long,long long> template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } struct ReRooting { struct Edge { int to; Edge(int _to) :to(_to) {} }; vector<int>a; vector<vector<Edge>>g; vector<vector<mint>>dp, ans; int sz; ReRooting(int _sz) :sz(_sz) { a.resize(sz); g.resize(sz); dp = vector<vector<mint>>(sz, vector<mint>(1001)); ans = vector<vector<mint>>(sz, vector<mint>(1001)); } void ReadGraph() { rep(i, sz - 1) { int a, b; cin >> a >> b; a--; b--; g[a].push_back({ b }); g[b].push_back({ a }); } } /* chi*chi chi*par */ // childs*child vector<mint> mergeChi(const vector<mint> &lh, const vector<mint> &rh) {//★★ vector<mint> ret(1001); vector<mint> lsub = lh, rsub = rh; rep2(i, 1, 1001)rsub[0] += rsub[i]; rep2(i, 1, lsub.size())lsub[i] = lsub[i - 1] + lh[i]; rep2(i, 1, rsub.size())rsub[i] = rsub[i - 1] + rh[i]; rep(i, ret.size()) ret[i] = lsub[i] * rsub[i]; rrep2(i, 1, ret.size())ret[i] -= ret[i - 1]; return ret; } // par*childs vector<mint> mergePar(const vector<mint> &lh, const vector<mint> &rh) {//★★ vector<mint> ret(1001); vector<mint> lsub = lh, rsub = rh; rep2(i, 1, lsub.size())lsub[i] = lsub[i - 1] + lh[i]; rep2(i, 1, rsub.size())rsub[i] = rsub[i - 1] + rh[i]; rep(i, ret.size()) ret[i] = lsub[i] * rsub[i]; rrep2(i, 1, ret.size())ret[i] -= ret[i - 1]; return ret; } // par(部分木)*childs vector<mint> mergePar2(const vector<mint> &lh, const vector<mint> &rh) {//★★ vector<mint> ret(1001); vector<mint> lsub = lh, rsub = rh; rep2(i, 1, 1001)rsub[0] += rsub[i]; rep2(i, 1, lsub.size())lsub[i] = lsub[i - 1] + lh[i]; rep2(i, 1, rsub.size())rsub[i] = rsub[i - 1] + rh[i]; rep(i, ret.size()) ret[i] = lsub[i] * rsub[i]; rrep2(i, 1, ret.size())ret[i] -= ret[i - 1]; return ret; } void dfs(int v, int p = -1) { vector<mint>sub(1001); sub[0] = 1; for (auto e : g[v]) { if (p == e.to)continue; dfs(e.to, v); sub = mergeChi(sub, dp[e.to]); } dp[v][a[v]] = 1; dp[v] = mergePar(dp[v], sub); } void dfsR(int v, int p = -1) { ans[v] = dp[v]; if (-1 != p) ans[v] = mergePar2(ans[v], dp[p]); vector<vector<mint>>vdata; for (auto e : g[v]) vdata.push_back(dp[e.to]); int sz = vdata.size(); vector<vector<mint>>sumL(sz + 1, vector<mint>(1001)), sumR(sz + 1, vector<mint>(1001)); sumL[0][0] = 1, sumR[0][0] = 1; rep(i, sz)sumL[i + 1] = mergeChi(sumL[i], vdata[i]); rep(i, sz)sumR[i + 1] = mergeChi(sumR[i], vdata[sz - i - 1]); for (int i = 0; i < g[v].size(); ++i) { auto e = g[v][i]; if (p == e.to)continue; auto chi = mergePar(sumL[i], sumR[sz - (i + 1)]); vector<mint>sub(1001); sub[a[v]] = 1; dp[v] = mergePar(sub, chi); dfsR(e.to, v); } } void debug() { rep(i, sz) { rep(j, 10) cout << dp[i][j].val() << " "; cout << endl; } cout << endl; } void output() { mint ret = 0; rep(i, sz) rep(j, 1001) ret += ans[i][j] * j; cout << ret.val() << endl; } void solve() { rep(i, sz)cin >> a[i]; ReadGraph(); dfs(0); dfsR(0); output(); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; ReRooting reroot(n); reroot.solve(); return 0; }