版本比较
题解:CF1211H Road Repair in Treeland
正文
增加 44 行删除 15 行
11234567891011121314151617181920212223242523 行未改动49505152535455565719 行未改动7778798061818283848 行未改动939495969798991001011021031041051061079 行未改动11711811912010012112212312412512612712812911013013113213313413 行未改动14814915015115215315415515615715813815916016116214214316316416516614514614716716816917017117217317417515317617717817918018118218318418518618718818916519019116719219319419519619719819917120020120220320420520620720818020921021121221353 行未改动UPD:经提醒,改了一些错别字,代码锅了,感谢 Ryanhao 指出,已经修复,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 fa if (now + chs <= MX) { cmin(f[now + chs], dp[u][now]); } { //color it with diff color with fa
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];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 fa if (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); state[v][now + chs] = make_pair(las, make_pair(now, chs)); edgecol[v][now + chs] = 1; } } //color it with diff color with fa if (dp[u][now] + chs <= MX && dp[u][now] + chs < f[now]) { //color it with diff color from fa if (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; 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]; state[v][0] = make_pair(las, 0); 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] = col[FA[u]][edgecol[u][j]]; Ans[u] = fcol; if (u != 1) Go(state[u][j].first, state[u][j].second, fcol); { 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, j, Ans[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, 0}, edgecol[i][j] = 0; 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 行未改动