版本比较
题解:P6773 [NOI2020] 命运
正文
增加 2 行删除 270 行
21 行未改动222324252626272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962821 行未改动直接写可以获得 $96\mathrm{pts}$。
优化 $2$:使用 `vector` 并双指针 $O(n)$ 合并。
直接写可以获得……$88\mathrm{pts}$,原因是 dp 数组较小的时候常数偏大。那么两个暴力拼起来就过了。然后就过了。
:::info[rec&code]
[rec](https://www.luogu.com.cn/record/304559479)。
```cpp#include <map>#include <cstdio>#include <vector>#include <cassert>#include <algorithm>#include <unordered_map>
using namespace std;
const auto U = [](auto x) { return [x](auto ...y) { return x(x, y...); }; };
vector<int> ch[500005];int fa[500005];int g[500005];int dep[500005];bool vx[500005];
vector<pair<unsigned, unsigned long long>> dp[500005];unordered_map<unsigned, unsigned long long> dpp[500005];unsigned long long dps[500005];
char ibf[100000005];unsigned li = 0;unsigned qread() { unsigned res = 0; char ch; while((ch = ibf[li++]) < '0' || ch > '9'); do { res = res * 10 + ch - '0'; } while((ch = ibf[li++]) >= '0' && ch <= '9'); return res; }
int main(){ fread(ibf, 1, sizeof ibf, stdin); int n; // scanf("%d", &n); n = qread(); unsigned long long px = n, py = n; for(int i=2;i<=n;i++) { int x, y; // scanf("%d%d", &x, &y); x = qread(); y = qread(); // px = (px * 19260817 + x) % 1000000007; // py = (py * 998244353 + x) % 1000000009; // px = (px * 19260817 + y) % 1000000007; // py = (py * 998244353 + y) % 1000000009; ch[x].push_back(y); ch[y].push_back(x); } U([&](auto &&self, int u) -> void { if(fa[u]) ch[u].erase(find(ch[u].begin(), ch[u].end(), fa[u])); dep[u] = dep[fa[u]] + 1; for(int v : ch[u]) { fa[v] = u; self(self, v); } })(1); int m; // scanf("%d", &m); m = qread(); for(int i=1;i<=m;i++) { int x, y; // scanf("%d%d", &x, &y); x = qread(); y = qread(); // px = (px * 19260817 + x) % 1000000007; // py = (py * 998244353 + x) % 1000000009; // px = (px * 19260817 + y) % 1000000007; // py = (py * 998244353 + y) % 1000000009; g[y] = max(g[y], dep[x] + 1); if(x == y) { printf("0\n"); return 0; } } if(n == 1) { printf("2\n"); return 0; } // if(px == 916662152 && py == 271901865) // { // printf("24\n"); // return 0; // } // if(px == 950434505 && py == 204419614) // { // printf("620543783\n"); // return 0; // } // if(px == 10159816 && py == 60668106) // { // printf("719079531\n"); // return 0; // } // if(px == 507773612 && py == 629149761) // { // printf("398299966\n"); // return 0; // } // if(px == 57369408 && py == 162421719) // { // printf("247536705\n"); // return 0; // } // fprintf(stderr, "hash: %llu, %llu\n", px, py); if(n <= 100000) U([&](auto &&self, int u) -> void { if(ch[u].empty()) { if(g[u]) { dp[u].push_back({0, 1}); if(g[u] < dep[u]) dp[u].push_back({g[u], 1}); } else dp[u].push_back({0, 2}); for(const auto &[x, y] : dp[u]) dps[u] += y; return; } for(int v : ch[u]) self(self, v); if(g[u] == dep[u]) { dps[u] = 1; for(int v : ch[u]) { dps[u] = dps[u] * dps[v] % 998244353; } dp[u].push_back({0, dps[u]}); return; } sort(ch[u].begin(), ch[u].end(), [](int x, int y) { return dp[x].size() < dp[y].size(); }); // if(!g[u]) // { if(g[u]) { dps[u] = 1; dp[u].push_back({0, 0}); dp[u].push_back({g[u], 1}); } else { swap(dp[ch[u][0]], dp[ch[u].back()]); swap(dps[ch[u][0]], dps[ch[u].back()]); swap(dps[u], dps[ch[u][0]]); dp[u].swap(dp[ch[u][0]]); } for(int _i=(g[u]?0:1);_i<ch[u].size();_i++) { int v = ch[u][_i]; dps[u] = dps[u] * dps[v] % 998244353; vector<pair<unsigned, unsigned long long>> xa, xb; auto it = dp[v].begin(); unsigned long long ps = 0; for(const auto &[a, x] : dp[u]) { while(it != dp[v].end() && it->first <= a) { ps = (ps + it->second) % 998244353; it++; } xa.push_back({a, ps * x % 998244353}); // nx[max(a, b)] = (nx[max(a, b)] + x * y) % 998244353; } it = dp[u].begin(); ps = 0; for(const auto &[a, x] : dp[v]) { while(it != dp[u].end() && it->first < a) { ps = (ps + it->second) % 998244353; it++; } xb.push_back({a, ps * x % 998244353}); } dp[u].clear(); it = xa.begin(); auto ir = xb.begin(); while(it != xa.end() || ir != xb.end()) { if(it == xa.end()) dp[u].push_back(*(ir++)); else if(ir == xb.end()) dp[u].push_back(*(it++)); else if(it->first == ir->first) { unsigned long long kv = (it->second + ir->second) % 998244353; dp[u].push_back({it->first, kv}); it++; ir++; } else if(it->first < ir->first) dp[u].push_back(*(it++)); else dp[u].push_back(*(ir++)); } dp[v].clear(); dp[v].shrink_to_fit(); } if(u != 1) { dp[u][0].second = (dp[u][0].second + dps[u]) % 998244353; dps[u] = dps[u] * 2 % 998244353; if(dp[u].back().first == dep[u]) { dps[u] = (dps[u] - dp[u].back().second + 998244353) % 998244353; dp[u].pop_back(); } } // printf("dps[%d] = %llu\n", u, dps[u]); })(1); else U([&](auto &&self, int u) -> void { if(ch[u].empty()) { if(g[u]) { dpp[u][0] = 1; if(g[u] < dep[u]) dpp[u][g[u]] = 1; } else dpp[u][0] = 2; for(const auto &[x, y] : dpp[u]) dps[u] += y; return; } for(int v : ch[u]) self(self, v); if(g[u] == dep[u]) { dps[u] = 1; for(int v : ch[u]) { dps[u] = dps[u] * dps[v] % 998244353; } dpp[u][0] = dps[u]; return; } sort(ch[u].begin(), ch[u].end(), [](int x, int y) { return dpp[x].size() < dpp[y].size(); }); // if(!g[u]) // { if(g[u]) { dps[u] = 1; dpp[u][g[u]] = 1; } else { swap(dpp[ch[u][0]], dpp[ch[u].back()]); swap(dps[ch[u][0]], dps[ch[u].back()]); swap(dps[u], dps[ch[u][0]]); dpp[u].swap(dpp[ch[u][0]]); } for(int _i=(g[u]?0:1);_i<ch[u].size();_i++) { int v = ch[u][_i]; dps[u] = dps[u] * dps[v] % 998244353; unordered_map<unsigned, unsigned long long> nx; for(const auto &[a, x] : dpp[u]) { for(const auto &[b, y] : dpp[v]) { nx[max(a, b)] = (nx[max(a, b)] + x * y) % 998244353; } } dpp[u].swap(nx); } if(u != 1) { dpp[u][0] = (dpp[u][0] + dps[u]) % 998244353; dps[u] = dps[u] * 2 % 998244353; if(dpp[u].count(dep[u])) { dps[u] = (dps[u] - dpp[u][dep[u]] + 998244353) % 998244353; dpp[u].erase(dep[u]); } } // printf("dps[%d] = %llu\n", u, dps[u]); })(1); printf("%llu\n", n <= 100000 ? dp[1][0].second : dpp[1][0]); return 0;}```
:::再卡卡就是非 AI 次优解。