版本比较
题解:B4339 [中山市赛 2023] 树的改造
正文
增加 88 行删除 131 行
60 行未改动61626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721860 行未改动代码可能有点长,已经弄懂的朋友可以自己写,以下只做参考### **上代码**```cpp#include<bits/stdc++.h>using namespace std;const int N = 1e6+5;struct Edge {int to, next;};Edge edge[N*2];int head[N], cnt;int cha[N]; //差分数组void init() {for(int i = 0; i < N; i++) head[i] = -1;cnt = 0;}void addedge(int u, int v) {edge[cnt].to = v;edge[cnt].next = head[u];head[u] = cnt++;}int fa[N][20], deep[N];// ----------预处理深度和倍增祖先 ----------void dfs(int x, int fat) {deep[x] = deep[fat] + 1;fa[x][0] = fat;for(int i = 1; (1 << i) <= deep[x]; i++) {fa[x][i] = fa[fa[x][i-1]][i-1];}for(int i = head[x]; i != -1; i = edge[i].next) {int v = edge[i].to;if(v != fat) dfs(v, x);}}int LCA(int x, int y){if(deep[x] < deep[y]) swap(x, y);int diff = deep[x] - deep[y];for(int i = 19; i >= 0; i--) {if(diff & (1 << i)) x = fa[x][i];}if(x == y) return x;for(int i = 19; i >= 0; i--) {if(fa[x][i] != fa[y][i]) {x = fa[x][i];y = fa[y][i];}}return fa[x][0];}int n;// ---------- 并查集 ----------struct DSU {int parent[N], sz[N];DSU(int n) {for(int i = 0; i <= n; i++) parent[i] = i, sz[i] = 1;}int find(int x) {return parent[x] == x ? x : parent[x] = find(parent[x]);}void unite(int a, int b) {a = find(a); b = find(b);if(a == b) return;if(sz[a] < sz[b]) swap(a, b);parent[b] = a;sz[a] += sz[b];}};// ---------- 树上差分 DFS(收集子树和,得到每条边的覆盖次数) ----------// 注意:我们用cha[child] 表示 A 中边 (child, parent[child]) 被覆盖的次数void dfscha(int u, int fa){for(int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].to;if(v == fa) continue;dfscha(v, u);cha[u] += cha[v]; // 子树差分值累加到父节点}}int main() {ios::sync_with_stdio(false);cin.tie(0);init();cin >> n;vector<pair<int,int>> a, b;a.reserve(n - 1);b.reserve(n - 1);for(int i = 1; i < n; i++) {int u, v;cin >> u >> v;a.push_back({min(u,v), max(u,v)});}for(int i = 1; i < n; i++) {int u, v;cin >> u >> v;b.push_back({min(u,v), max(u,v)});}sort(a.begin(), a.end());sort(b.begin(), b.end());//默认按字典序排序,找公共边vector<pair<int,int>> c;int i = 0, j = 0; //双指针while(i < a.size() && j < b.size()) {if(a[i] == b[j]){c.push_back(a[i]);i++; j++;}else if(a[i] < b[j]) i++;else j++;}// ---------- 并查集合并成块 ----------DSU dsu(n);for(auto &e : c) {dsu.unite(e.first, e.second);}// 清空 head,重新建立A树的邻接表(用于LCA和差分DFS)init();for(auto &e : a) {addedge(e.first, e.second);addedge(e.second, e.first);}dfs(1, 0);// 在 A 树上对 B 的所有边做路径差分for(auto &e : b) {int u = e.first, v = e.second;int l = LCA(u, v);cha[u]++;cha[v]++;cha[l] -= 2;}// 执行差分累加(链式前向星版本)dfscha(1, 0);int ans = 0;for(int i = 2; i <= n; i++){if(cha[i] > 0) ans++; // 统计被覆盖的边数}cout << ans << '\n';return 0;}```return 0;}```### **管理大大辛苦了orz**### **蒟蒻不才,求审核通过**60 行未改动61626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417560 行未改动代码可能有点长,已经弄懂的朋友可以自己写,以下只做参考### **上代码**```cpp#include <bits/stdc++.h>using namespace std;const int MAXN = 1000005;int n;int head[MAXN], to[2*MAXN], nxt[2*MAXN], ecnt=0;int parent[MAXN], depth[MAXN], sz[MAXN], heavy[MAXN], top[MAXN];int order[MAXN], ord_cnt=0;int diff[MAXN], degB[MAXN];int bu[MAXN], bv[MAXN];void addEdge(int u, int v) {to[++ecnt]=v; nxt[ecnt]=head[u]; head[u]=ecnt;}// 快速读入inline int read() {int x=0, f=1; char c=getchar();while(c<'0'||c>'9') { if(c=='-') f=-1; c=getchar(); }while(c>='0'&&c<='9') { x=x*10+c-'0'; c=getchar(); }return x*f;}int lca(int u, int v) {while(top[u] != top[v]) {if(depth[top[u]] < depth[top[v]]) swap(u,v);u = parent[top[u]];}return depth[u] < depth[v] ? u : v;}int main() {n = read();for(int i=1; i<n; ++i) {int u=read(), v=read();addEdge(u,v); addEdge(v,u);}for(int i=1; i<n; ++i) {int u=read(), v=read();bu[i]=u; bv[i]=v;degB[u]++; degB[v]++;}// 迭代 DFS 求 parent, depth, orderint stack[MAXN], stp=0;stack[++stp]=1;parent[1]=0; depth[1]=0;while(stp) {int u=stack[stp--];order[++ord_cnt]=u;for(int e=head[u]; e; e=nxt[e]) {int v=to[e];if(v==parent[u]) continue;parent[v]=u;depth[v]=depth[u]+1;stack[++stp]=v;}}// 逆序求 size 和 heavyfor(int i=n; i>=1; --i) {int u=order[i];sz[u]=1;heavy[u]=0;int maxsz=0;for(int e=head[u]; e; e=nxt[e]) {int v=to[e];if(parent[v]==u) {sz[u]+=sz[v];if(sz[v]>maxsz) {maxsz=sz[v];heavy[u]=v;}}}}// 求 toptop[1]=1;for(int i=1; i<=n; ++i) {int u=order[i];for(int e=head[u]; e; e=nxt[e]) {int v=to[e];if(parent[v]==u) {if(v==heavy[u]) top[v]=top[u];else top[v]=v;}}}// 处理 B 边for(int i=1; i<n; ++i) {int u=bu[i], v=bv[i];int l = lca(u,v);diff[u]++;diff[v]++;diff[l]--;if(parent[l]) diff[parent[l]]--;}// 后序累加for(int i=n; i>=1; --i) {int u=order[i];if(parent[u]) diff[parent[u]] += diff[u];}int ans=0;for(int i=1; i<=n; ++i) {if(diff[i] - degB[i] > 0) ans++;}printf("%d\n", ans);return 0;}```### **管理大大辛苦了orz**### **蒟蒻不才,求审核通过**