正文
增加 24 行删除 24 行
200 行未改动全部展开201202203204205206207208209285 行未改动全部展开49549649749849950050150250339 行未改动5435445455465475485495505514 行未改动5565575585595605615625635645655665675685695705715725737 行未改动5815825835845855865875885899 行未改动5996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526534 行未改动658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688137 行未改动全部展开200 行未改动全部展开### 思路发现题目这样问你,相当于问你:若 $i$ 和 $j$ 有相同元素就连边,问是否存在唯一的拓扑序,也就是是否是一条链。直接隐式建图判链即可。如果存在 $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 行未改动全部展开200 行未改动全部展开201202203204205206207208209285 行未改动全部展开49549649749849950050150250339 行未改动5435445455465475485495505514 行未改动5565575585595605615625635645655665675685695705715725737 行未改动5815825835845855865875885899 行未改动5996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526534 行未改动658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688137 行未改动全部展开200 行未改动全部展开### 思路发现题目这样问你,相当于问你:若 $i$ 和 $j$ 有相同元素就连边,问是否存在唯一的拓扑序,也就是是否是一条链。直接隐式建图判链即可。如果存在 $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 行未改动全部展开