版本比较
ICPC 2023 区域赛南京站题解 + 重现赛游记
正文
增加 24 行删除 24 行
200 行未改动全部展开201202203204205205206207208209285 行未改动全部展开49549649749849949950050150250339 行未改动5435445455465475475485495505514 行未改动5565575585595605605615625635645655665675685695695705715725737 行未改动5815825835845855855865875885899 行未改动5996006016026036036046056056066076086096106106116126136146156156166176186196196206216226236236246256266266276286286296306316326336336346356366376386386396406416426426436446456466476486496496506516526534 行未改动658659660661662662663664664665666667668669670671671672673674675676676677678679680681682682683684684685686687688137 行未改动全部展开200 行未改动全部展开### 思路
发现题目这样问你,相当于问你:若 $i$ 和 $j$ 有相同元素就连边,问是否存在唯一的拓扑序,也就是是否是一条链。直接隐式建图判链即可。
如果存在 $i$ 和 $i+1$ 没有连边,就可以将 $i$ 和 $i+1$ 交换输出 如果存在 $i$ 和 $i+1$ 没有连边,就可以将 $i$ 和 $i+1$ 交换输出
### C++ 代码
```cpp285 行未改动全部展开```
## [D - 红黑树 / Red Black Tree](https://www.luogu.com.cn/problem/P13954)
### 题意 ### 题意
给定一颗有根树,每个节点是黑色或红色,你每次可以,对于每个节点,你需要求出让该节点的子树内每条从该节点到叶子的路径的长度都相等的最小代价。
### 思路39 行未改动struct SegmentTree { int tree[MAXN << 2]; int updateValue, queryResult; int updateLeft, updateRight;
// 区间更新 void updateRange(int l, int r, int idx) { if (updateLeft <= l && r <= updateRight) { tree[idx] = min(tree[idx], updateValue);4 行未改动 if (updateLeft <= mid) updateRange(l, mid, leftChild); if (updateRight > mid) updateRange(mid + 1, r, rightChild); } }
// 执行更新操作 inline void applyUpdate(int l, int r, int value) { if (l > r) return; updateLeft = l; updateRight = r; updateValue = value; updateRange(0, n, 1); }
// 清除线段树数据到临时数组 void clearData(int l, int r, int idx) { if (l == r) { tempArray[l] = tree[idx];7 行未改动 if (updateRight > mid) clearData(mid + 1, r, rightChild); } tree[idx] = INF; }
// 执行清除操作 inline void applyClear(int l, int r) { if (l > r) return; updateLeft = l;9 行未改动 for (int i = 0; i < children[node].size(); i++) { int child = children[node][i]; dfs(child); }
minDepth[node]--;
if (node == 1 || children[node].size() != 1) { dp[node].clear(); dp[node].resize(minDepth[node] + 1, 0); tempArray.resize(minDepth[node] + 1);
for (int i = 0; i < children[node].size(); i++) { int child = children[node][i]; int cnt1 = count1[child]; int cnt0 = count0[child];
for (int j = 0; j < dp[child].size(); j++) { int value = dp[child][j]; int position = j + cnt1;
segTree1.applyUpdate(position, min(position + cnt0, minDepth[node]), value - position); segTree2.applyUpdate(max(position - cnt1, 0), min(position, minDepth[node]), value + position); }
segTree1.applyClear(0, minDepth[node]); vector<int> temp1 = tempArray;
segTree2.applyClear(0, minDepth[node]);
for (int j = 0; j <= minDepth[node]; j++) { dp[node][j] += min(temp1[j] + j, tempArray[j] - j); } }
result[node] = INF; for (int j = 0; j <= minDepth[node]; j++) { result[node] = min(result[node], dp[node][j]); }
count1[node] = count0[node] = 0; if (s[node] == '1') count1[node]++; else count0[node]++;
} else { int child = children[node][0]; swap(dp[node], dp[child]); count1[node] = count1[child]; count0[node] = count0[child]; result[node] = result[child];
if (s[node] == '1') count1[node]++; else count0[node]++; }}4 行未改动 cin >> n >> s; tempArray.resize(n + 1); segTree1.applyClear(0, n); segTree2.applyClear(0, n);
s = " " + s;
for (int i = 1; i <= n; i++) { children[i].clear(); minDepth[i] = INF; count1[i] = count0[i] = 0; maxValues[i] = make_pair(0, 0); }
for (int i = 2; i <= n; i++) { cin >> parent[i]; children[parent[i]].push_back(i); }
// 计算每个节点到叶子的最小深度 for (int i = n; i > 1; i--) { if (children[i].size() == 0) minDepth[i] = 1; minDepth[parent[i]] = min(minDepth[parent[i]], minDepth[i] + 1); }
dfs(1);
for (int i = 1; i <= n; i++) { cout << result[i]; if (i != n) cout << " "; }137 行未改动全部展开