版本比较
题解:P6773 [NOI2020] 命运
正文
增加 2 行删除 270 行
21 行未改动22232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529621 行未改动直接写可以获得 $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;}```:::21 行未改动2223242526272821 行未改动直接写可以获得 $96\mathrm{pts}$。优化 $2$:使用 `vector` 并双指针 $O(n)$ 合并。然后就过了。再卡卡就是非 AI 次优解。