版本比较
题解:CF1211H Road Repair in Treeland
正文
增加 44 行删除 15 行
12345623 行未改动303132333435363719 行未改动5758596061626364658 行未改动747576777879808182838485869 行未改动9697989910010110210310410510610710810911011111211311413 行未改动12812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318453 行未改动UPD:经提醒,改了一些错别字,代码锅了,感谢 Ryanhao 指出,已经修复,这道题评黑的原因是不是因为不会 Kotlin 语言导致的啊……看到这种“最小的最大”/“最大的最小”,很容易想到二分答案。23 行未改动代码(并没有使用 Kotlin 语言实现,用的是 C++17,加入了格式化):```cpp#include <bits/stdc++.h>using namespace std;const int N = 3000 + 7, INF = 1e8;bool __st;19 行未改动void dfs(int u, int fa){dp[u][(u != 1)] = 0;sz[u] = 1;for (int v : G[u]){if (v == fa)continue;8 行未改动continue;if (now > MX)continue;if (now + chs <= MX){cmin(f[now + chs], dp[u][now]);}{if (dp[u][now] + chs <= MX)cmin(f[now], dp[u][now] + chs);}9 行未改动MX = x;dfs(1, 1);for (int j = 0; j <= (x); ++j){if (dp[1][j] <= x)return 1;}return 0;}int col[N][2];int tot = 0;bool edgecol[N][N];int laskid[N], FA[N];pair<int, int> state[N][N];void dfs2(int u, int fa){dp[u][(u != 1)] = 0;sz[u] = 1;13 行未改动if (dp[v][chs] > MX)continue;if (now > MX)continue;//We color it with the same to faif (now + chs <= MX){if (dp[u][now] < f[now + chs]){cmin(f[now + chs], dp[u][now]);state[v][now + chs] = make_pair(las, now);edgecol[v][now + chs] = 1;}}//color it with diff color with faif (dp[u][now] + chs <= MX && dp[u][now] + chs < f[now]){cmin(f[now], dp[u][now] + chs);state[v][now] = make_pair(las, now);edgecol[v][now] = 0;}}sz[u] += sz[v];for (int i = 0; i <= (sz[u]); ++i)dp[u][i] = f[i];state[v][0] = make_pair(las, 0);las = v;}laskid[u] = las;}int Ans[N];void Go(int u, int j, int fcol){if (!u)return;col[u][1] = fcol;col[u][0] = ++tot;Ans[u] = col[FA[u]][edgecol[u][j]];if (u != 1)Go(state[u][j].first, state[u][j].second, fcol);if (!laskid[u])return;int vv = laskid[u];Go(vv, j, Ans[u]);}void work(int x){init();MX = x;tot = 0;for (int i = 0; i <= (n); ++i)for (int j = 0; j <= (n); ++j)state[i][j] = {0, 0}, edgecol[i][j] = 0;dfs2(1, 1);for (int j = 0; j <= (x); ++j){if (dp[1][j] > x)53 行未改动1234567891011121314151617181920212223242523 行未改动49505152535455565719 行未改动77787980818283848 行未改动939495969798991001011021031041051061079 行未改动11711811912012112212312412512612712812913013113213313413 行未改动14814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221353 行未改动UPD:代码被 Ryanhao hack 了,已经修复。没有调过去的同学可以看看这组 hack。hack 数据:```191 21 32 42 52 63 73 83 9```答案是 $2$。---这道题评黑的原因是不是因为不会 Kotlin 语言导致的啊……看到这种“最小的最大”/“最大的最小”,很容易想到二分答案。23 行未改动代码(并没有使用 Kotlin 语言实现,用的是 C++17,加入了格式化):```cpp#include <bits/stdc++.h>using namespace std;const int N = 3000 + 7, INF = 1e8;bool __st;19 行未改动void dfs(int u, int fa){dp[u][(u != 1)] = 0;sz[u] = 1;for (int v : G[u]){if (v == fa)continue;8 行未改动continue;if (now > MX)continue;//We color it with the same to faif (now + chs <= MX){cmin(f[now + chs], dp[u][now]);}{//color it with diff color with faif (dp[u][now] + chs <= MX)cmin(f[now], dp[u][now] + chs);}9 行未改动MX = x;dfs(1, 1);for (int j = 0; j <= (x); ++j){if (dp[1][j] <= x)return 1;}return 0;}int col[N][2];int tot = 0;bool edgecol[N][N];int laskid[N], FA[N];pair<int, pair<int, int>> state[N][N];void dfs2(int u, int fa){dp[u][(u != 1)] = 0;sz[u] = 1;13 行未改动if (dp[v][chs] > MX)continue;if (now > MX)continue;//We color it with the same to faif (now + chs <= MX){if (dp[u][now] < f[now + chs]){cmin(f[now + chs], dp[u][now]);state[v][now + chs] = make_pair(las, make_pair(now, chs));edgecol[v][now + chs] = 1;}}{//color it with diff color from faif (dp[u][now] + chs <= MX && dp[u][now] + chs < f[now]){cmin(f[now], dp[u][now] + chs);state[v][now] = make_pair(las, make_pair(now, chs));edgecol[v][now] = 0;}}}sz[u] += sz[v];for (int i = 0; i <= (sz[u]); ++i)dp[u][i] = f[i];las = v;}laskid[u] = las;}int Ans[N];void Go(int u, int j, int fcol){/*Go(u,j,fcol)是我到了节点u,fa->我时fa的状态是j,fa->我的颜色为fcol*/if (!u)return;col[u][1] = fcol;col[u][0] = ++tot;Ans[u] = fcol;if (u != 1){int nxt = state[u][j].first, nxtj = state[u][j].second.first;Go(nxt, nxtj,col[FA[u]][edgecol[nxt][nxtj]]);}if (!laskid[u])return;int vv = laskid[u];Go(vv, (u == 1 ? j : state[u][j].second.second), col[u][edgecol[vv][(u == 1 ? j : state[u][j].second.second)]]);}void work(int x){init();MX = x;tot = 0;for (int i = 0; i <= (n); ++i)for (int j = 0; j <= (n); ++j)state[i][j] = {0, make_pair(0, 0)}, edgecol[i][j] = 0;dfs2(1, 1);for (int j = 0; j <= (x); ++j){if (dp[1][j] > x)53 行未改动