版本比较
Kruskal 重构树
正文
增加 671 行删除 6 行
19 行未改动20212223242524252627142 行未改动全部展开170171172173176177174175176177203 行未改动全部展开381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668226 行未改动全部展开89589689789889990090190290390490590690790890991091191291391463591591691791863991992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813096 行未改动19 行未改动- 若原图联通,满足比原图新增了 $n-1$ 个结点,边权的结点编号 $x$ 满足 $x\in(n,2n)$;- 最小生成树重构得到大根堆,最大生成树反之[^1];- 重构树中从 $x$ 到 $y$ 的简单路径中经过的非叶子结点为生成图中 $x$ 到 $y$ 路径上经过的边权;
---
## 经典例题
- $\texttt{P1967 [NOIP 2013 提高组] 货车运输}$- $\texttt{P2245 星际导航}$142 行未改动全部展开}```::::
---
## 习题讲解
### P13548 [OOI 2022] Air Reform
203 行未改动全部展开在每一轮选出边的时候,同步构建一个新的 kruskal 重构树。最后 $m$ 次 $O(\log n)$ 询问即可。
总时间复杂度为 $O(n+m\log n+n\log^2 n+(n+m)\log^2 n)$。
:::info[$\texttt{code}$]```cpp#include <bits/stdc++.h>// #define int long long#define lint long long#define endl '\n'using namespace std;const int N = 2e5+5;
struct node{ int idx,x,y,z;
bool operator <(const node x) const { return this->z < x.z; }}a[N];
struct point{ int idx,dfs,col; bool operator <(const point x) const { return this->dfs < x.dfs; }};
int n,m,f[N<<1];int cntBlock;vector<int > e[N<<1];int cntNode,kurskalVal[N<<1];int newKurskalVal[N<<1];vector<node > edgeVec;vector<node > NewEdgeVec;vector<node > edgeAns;
int fa[N<<1],dep[N<<1];int sz[N<<1],wson[N<<1];int cntId,id[N<<1];int g[N<<1],top[N<<1];
vector<point > arr;int preMax[N],preSecMax[N];int sufMin[N],sufSecMin[N];bitset<1> tickNode[N<<1];
unordered_map<lint,bool > hashTable;
inline void init(int n);
inline lint checkHash(int x,int y){ return 1ll * x * 1000000 + y;}
inline bool cmp(node x,node y){ return x.idx < y.idx;}
inline int find(int x){ if(f[x] == x) return x; return f[x] = find(f[x]);}
inline void merge(int x,int y){ int fx = find(x); int fy = find(y); if(fx != fy) f[fx] = fy;}
inline void dfs1(int x,int father){ fa[x] = father; dep[x] = dep[father] + 1;
sz[x] = 1; for(auto y : e[x]){ if(y != father){ dfs1(y,x); sz[x] += sz[y]; if(sz[y] > sz[wson[x]]) wson[x] = y; } }}
inline void dfs2(int x,int TOP){ id[x] = ++cntId; g[cntId] = x; top[x] = TOP;
if(wson[x]) dfs2(wson[x],TOP); for(auto y : e[x]){ if(y != fa[x] && y != wson[x]) dfs2(y,y); }}
inline int lca(int x,int y){ while(top[x] != top[y]){ if(dep[top[x]] < dep[top[y]]) swap(x,y); x = fa[top[x]]; } return dep[x] < dep[y] ? x : y;}
signed main(){ ios::sync_with_stdio(0); cin.tie(0); // freopen("app.in","r",stdin); // freopen("app.out","w",stdout); int T,G; cin >> T >> G; while(T--){ cin >> n >> m;
init(n);
int x,y,z; for(int i = 1; i <= m; i++){ cin >> x >> y >> z; a[i] = (node){i,x,y,z}; hashTable[checkHash(x,y)] = true; hashTable[checkHash(y,x)] = true; }
sort(a+1,a+1+m);
for(int i = 1; i <= m; i++){ x = a[i].x,y = a[i].y,z = a[i].z; int fx = find(x),fy = find(y);
if(fx != fy){ kurskalVal[++cntNode] = z; merge(fx,cntNode),merge(fy,cntNode); e[fx].push_back(cntNode),e[cntNode].push_back(fx); e[fy].push_back(cntNode),e[cntNode].push_back(fy); } }
dfs1(cntNode,0); dfs2(cntNode,cntNode);
cntId = 0,cntNode = n; for(int i = 1; i <= (n<<1); i++){ f[i] = i,wson[i] = 0; e[i].clear(); newKurskalVal[i] = -1; }
edgeAns.clear(); while(cntBlock > 1){ arr.clear(),edgeVec.clear(); NewEdgeVec.clear();
for(int i = 1; i <= n; i++){ preMax[i] = preSecMax[i] = 0; sufMin[i] = sufSecMin[i] = 0; f[i] = find(i); tickNode[i].reset(); arr.push_back((point){i,id[i],f[i]}); }
sort(arr.begin(),arr.end()); for(int i = 1; i <= n; i++){ int idx = i - 1; preMax[i] = i; if(i == 1){ preSecMax[i] = 0; continue; } if(arr[idx].col != arr[preMax[i-1]-1].col) preSecMax[i] = preMax[i-1]; else preSecMax[i] = preSecMax[i-1]; }
for(int i = n; i >= 1; i--){ int idx = i - 1; sufMin[i] = i; if(i == n){ sufSecMin[i] = n+1; continue; } if(arr[idx].col != arr[sufMin[i+1]-1].col) sufSecMin[i] = sufMin[i+1]; else sufSecMin[i] = sufSecMin[i+1]; }
for(int i = 1; i <= n; i++){ int idx = i-1,l = i-1,r = i+1;
while(l >= 1){ if(arr[idx].col == arr[l-1].col){ l = preSecMax[l]; continue; } if(hashTable.find(checkHash(arr[idx].idx,arr[l-1].idx)) != hashTable.end()) l--; else break; } while(r <= n){ if(arr[idx].col == arr[r-1].col){ r = sufSecMin[r]; continue; } if(hashTable.find(checkHash(arr[idx].idx,arr[r-1].idx)) != hashTable.end()) r++; else break; }
int lca1 = -1,lca2 = -1,lIdx = -1,rIdx = -1,Idx = arr[idx].idx; if(1 <= l && l <= n) lIdx = arr[l-1].idx,lca1 = lca(lIdx,Idx); else l = -1; if(1 <= r && r <= n) rIdx = arr[r-1].idx,lca2 = lca(rIdx,Idx); else r = -1; if(l == -1 && r == -1) continue; if(l != -1 && r != -1){ if(kurskalVal[lca1] < kurskalVal[lca2]) edgeVec.push_back((node){f[i],Idx,lIdx,kurskalVal[lca1]}); else edgeVec.push_back((node){f[i],Idx,rIdx,kurskalVal[lca2]}); }else if(l != -1) edgeVec.push_back((node){f[i],Idx,lIdx,kurskalVal[lca1]}); else if(r != -1) edgeVec.push_back((node){f[i],Idx,rIdx,kurskalVal[lca2]}); } sort(edgeVec.begin(),edgeVec.end()); for(auto val : edgeVec){ x = val.x,y = val.y,z = val.z; int fx = find(x),fy = find(y); if(fx != fy && tickNode[fx] == false){ cntBlock--,merge(fx,fy); edgeAns.push_back(val); } tickNode[fx].set(); } }
for(int i = 1; i <= (n<<1); i++) f[i] = i;
sort(edgeAns.begin(),edgeAns.end());
for(auto val : edgeAns){ x = val.x,y = val.y,z = val.z; int fx = find(x),fy = find(y); newKurskalVal[++cntNode] = z; merge(fx,cntNode),merge(fy,cntNode); e[fx].push_back(cntNode),e[cntNode].push_back(fx); e[fy].push_back(cntNode),e[cntNode].push_back(fy); }
dfs1(cntNode,0); dfs2(cntNode,cntNode);
sort(a+1,a+1+m,cmp);
for(int i = 1; i <= m; i++) cout << newKurskalVal[lca(a[i].x,a[i].y)] << " "; cout << endl; } return 0;}
inline void init(int n){ for(int i = 1; i <= (n<<1); i++){ f[i] = i; wson[i] = 0; e[i].clear(); kurskalVal[i] = -1; }
hashTable.clear(); cntId = 0; cntBlock = cntNode = n;}```:::
::::
---
### P4768 [NOI2018] 归程
简化题意,给出一个 $n$ 个结点 $m$ 条边的无向连通图,每条边有两个边权 $l_i,h_i$。每次询问给出一个二元组 $(s,d)$,边权 $h_i\le d$ 的边我们称为“不好的边”。
226 行未改动全部展开}```::::
---
### P9638 「yyOI R1」youyou 的军训
一些 tips 放在了这里 <https://www.luogu.com.cn/article/0as8n2fv>。
考虑是否适用于 kruskal 重构树,不难发现修改的限制条件“不改变相对顺序”就是防止重构树的形状发生改变设计的,只用单纯修改重构树上点权即可。
简单来说,这题就是瓶颈路问题的板子。
首先考虑跑一遍最大生成树。因为对于一个连接两个联通块的边 $x\leftrightarrow y$,若其权值 $z$ 小于另一条同样连接这个联通块的 $x'\leftrightarrow y'$ 的权值 $z'$,当 $x'\leftrightarrow y'$ 因为小于敏感度断掉时,$x\leftrightarrow y$ 一定也断掉。因为没有边长这一说,所以这样选一定是不劣的。
进行操作一时,不妨先用一个变量 $limit$ 记录。初始设置为 $-\infty$,这样不会有边断掉。
此时考虑在 $limit$ 的限制条件下进行操作 $2$。本质上就是寻找最大瓶颈,即找到 $x$ 的最浅祖先结点满足该结点的权值 $\ge limit$。此时该子树内所有结点都在一个联通块里,叶子结点数量即为答案。
对于操作三,我们找到对应原边后修改这两个点的 $\text{lca}$ 的权值即可。对于操作三,我们找到对应原边后修改这两个点的 $\text{lca}$ 的权值即可。但是需要注意的是,因为这条边就算变大如果已经断掉了也不会恢复,那么我们需要把这个修改(如果有效)先存起来,进行新的操作一后更新。
排序建 kruskal 重构树是 $O(m\log m+m)$,修改找原边用映射 $O(n)$,求 $\text{lca}$ 用树剖 $O(\log n)$,倍增找瓶颈 $O(\log n)$,跑 dfs 挂权值和树剖均是 $O(n)$。最劣的时间复杂度为 $O(m\log m+m+n+q\log n)$。
需要注意的是跑树剖不能只对 $2n-1$ 跑,因为不保证图强连通,最终可能是一个森林。::::info[$\texttt{code}$]```cpp#include <bits/stdc++.h>// #define int long long#define lint long long#define ldouble long double#define endl '\n'using namespace std;const int N = 4e5+5;
struct edge{ int idx,x,y,z; bitset<1> is;
edge(int a = 0,int b = 0,int c = 0,int d = 0): idx(a),x(b),y(c),z(d) {}
bool operator <(const edge &other) const { return this->z < other.z; } bool operator >(const edge &other) const { return this->z > other.z; }}e[N];int table[N];
struct node{ int x,y,z;}history[N];int cnt;
int n,m,q;int f[N<<1];int limit;
vector<int > g[N<<1];int cntId,id[N<<1];int fa[N<<1][30],dep[N<<1];int sz[N<<1],wson[N<<1];int top[N<<1],val[N<<1];int cntLeaves[N<<1];
inline int find(int x){ if(f[x] == x) return x; return f[x] = find(f[x]);}
inline void dfs1(int x,int father){ fa[x][0] = father,sz[x] = 1; dep[x] = dep[father] + 1; for(int y : g[x]){ if(y == father) continue; dfs1(y,x); sz[x] += sz[y]; if(sz[y] > sz[wson[x]]) wson[x] = y; }}
inline void dfs2(int x,int TOP){ id[x] = ++cntId; top[x] = TOP; if(wson[x]){ dfs2(wson[x],TOP); cntLeaves[x] += cntLeaves[wson[x]]; }else cntLeaves[x] = 1; for(int y : g[x]){ if(y != fa[x][0] && y != wson[x]){ dfs2(y,y); cntLeaves[x] += cntLeaves[y]; } }}
inline int lca(int x,int y){ if(find(x) != find(y)) return 0;
while(top[x] != top[y]){ if(dep[top[x]] < dep[top[y]]) swap(x,y); x = fa[top[x]][0]; } return dep[x] < dep[y] ? x : y;}
inline int query(int x){ int idx = 0; while(fa[x][idx] && val[fa[x][idx]] >= limit) idx++; if(idx == 0) return 1;
idx--; while(idx >= 0){ if(val[fa[x][idx]] >= limit) x = fa[x][idx]; idx--; }
return cntLeaves[x];}
signed main(){ ios::sync_with_stdio(0); cin.tie(0); // freopen("app.in","r",stdin); // freopen("app.out","w",stdout); cin >> n >> m >> q;
int x,y,k; for(int i = 1; i <= m; i++){ cin >> x >> y >> k; e[i] = edge(i,x,y,k); e[i].is.reset(); }
sort(e+1,e+1+m,greater<edge>());
for(int i = 1; i <= (n<<1); i++) f[i] = i;
for(int i = 1; i <= m; i++) table[e[i].idx] = i;
int cntNode = n; for(int i = 1; i <= m; i++){ int x = e[i].x,y = e[i].y; int fx = find(x),fy = find(y); if(fx != fy){ e[i].is.set(); val[++cntNode] = e[i].z; f[fx] = cntNode,f[fy] = cntNode; g[fx].push_back(cntNode),g[cntNode].push_back(fx); g[fy].push_back(cntNode),g[cntNode].push_back(fy); } if(cntNode == (n<<1)-1) break; }
val[0] = -1; for(int idx = cntNode; idx >= 1; idx--){ if(!id[idx]){ dfs1(idx,0); dfs2(idx,idx); } }
for(int j = 1; (1<<j) <= (n<<1); j++) for(int i = 1; i <= (n<<1); i++) fa[i][j] = fa[fa[i][j-1]][j-1]; int op; while(q--){ cin >> op; if(op == 1){ cin >> x; limit = x;
for(int i = 1; i <= cnt; i++) val[lca(history[i].x,history[i].y)] = history[i].z; cnt = 0; }else if(op == 2){ cin >> x; cout << query(x) << endl; }else if(op == 3){ cin >> x >> y; edge p = e[table[x]]; if(p.is == true) history[++cnt] = (node){p.x,p.y,y}; } } return 0;}```::::
---
### P3684 [CERC2016] 机棚障碍 Hangar Hurdles
题意就是给一个 01 矩阵,可以从每个点展开一个长度为奇数的正方形。要求询问两个点之间,可以通过的最大正方形。
我们给每一个点预处理赋予权值 $v_{x,y}$,代表位于 $(x,y)$ 最大可展开的正方形的长度。
发现时间复杂度最多是平方带一个 $\log$ 的,不妨思考枚举每个坐标然后二分一下。
此时二分的检验应该是 $O(1)$ 的。
预处理是好文明。我们再 $O(n^2)$ 预处理出 $d_{x,y}$ 表示从 $(x,y)$ 往下最长空白长度。再 $O(n^2)$ 预处理出 $f_{x,y}$ 表示以 $(x,y)$ 为左上角向右下可以延伸的最大正方形。
第一个显然是好做的。第二个显然也是好做的。
我们不妨模拟一下,推导 $(x,y)$ 时,我们往后寻找到 $(x+k,y)$ 时,需要满足 $\min\limits_{i\in[x,x+k]} d_{i,y}\ge k+1$。一个显然的是,$f_{x+1,y}\ge f_{x,y}-1$,不妨继承了之后再从已经有的右边界向右延伸。
对于求解 $\min$ 的问题,不妨用单调队列维护。
做完后我们就可以 $O(n^2\log n)$ 求 $v_{x,y}$ 了,然后再 $O(n^2)$ 建一下图,相邻点的边权为权值更小值。
然后跑个重构树就做完了。
为了辨别并查集数组把 $f$ 用 $g$ 代替。
::::info[$\texttt{code}$]```cpp#include <bits/stdc++.h>// #define int long long#define lint long long#define endl '\n'using namespace std;const int N = 1e3+5;
struct node{ int x,y,z;
node(int a = 0,int b = 0,int c = 0): x(a),y(b),z(c) {}
inline bool operator <(const node& other) const { return this->z < other.z; } inline bool operator >(const node& other) const { return this->z > other.z; }}arr[(N*N)<<1];
int n,m,cntNode;bool a[N][N];int f[N*N*2],v[N][N];int d[N][N],g[N][N];deque<int > que;vector<int> e[N*N*2];int val[N*N*2];
int fa[N*N*2];int dep[N*N*2];int lb[N*N*2];
inline int getId(int x,int y){ return n*(x-1) + y;}
inline bool check(int x,int y,int k){ int lx = x-k+1,ly = y-k+1,rx = x+k-1,ry = y+k-1; if(!(1 <= lx && lx <= n && 1 <= ly && ly <= n)) return false; if(!(1 <= rx && rx <= n && 1 <= ry && ry <= n)) return false; return g[lx][ly] >= k*2-1;}
inline int find(int x){ if(f[x] == x) return x; return f[x] = find(f[x]);}
inline void dfs(int x,int father){ fa[x] = father; dep[x] = dep[father] + 1;
int p = lb[father],q = lb[p]; lb[x] = (dep[father]-dep[p] == dep[p]-dep[q] ? q : father);
for(int y : e[x]){ if(y != father) dfs(y,x); }}
inline int lca(int x,int y){ if(dep[x] < dep[y]) swap(x,y); while(dep[x] > dep[y]){ if(dep[lb[x]] >= dep[y]) x = lb[x]; else x = fa[x]; } while(x != y){ if(lb[x] != lb[y]) x = lb[x],y = lb[y]; else x = fa[x],y = fa[y]; } return x;}
signed main(){ ios::sync_with_stdio(0); cin.tie(0); // freopen("app.in","r",stdin); // freopen("app.out","w",stdout); cin >> n;
char character; for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ cin >> character; a[i][j] = (character == '.'); } }
for(int i = n; i >= 1; i--){ for(int j = 1; j <= n; j++){ if(!a[i][j]) continue; d[i][j] = d[i+1][j] + 1; } }
for(int i = 1; i <= n; i++){ int end = 1;
que.clear(); for(int j = 1; j <= n; j++){ if(!a[i][j]) continue; end = max(end,j+1); g[i][j] = max(1,g[i][j-1]-1);
while(!que.empty() && que.front() < j) que.pop_front(); if(que.empty()) que.push_back(j);
while(a[i][end]){ while(!que.empty() && d[i][que.back()] >= d[i][end]) que.pop_back(); que.push_back(end); if(g[i][j]+1 > d[i][que.front()]) break; else g[i][j]++;
end++; } } }
for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ int l = 1,r = n; while(l <= r){ int mid = l + r >> 1; if(check(i,j,mid)){ v[i][j] = (mid<<1) - 1; l = mid + 1; }else r = mid - 1; } } }
for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ if(i > 1) arr[++m] = node(getId(i-1,j),getId(i,j),min(v[i-1][j],v[i][j])); if(j > 1) arr[++m] = node(getId(i,j-1),getId(i,j),min(v[i][j-1],v[i][j])); } }
for(int i = 1; i <= n*n*2; i++) f[i] = i;
sort(arr+1,arr+1+m,greater<node>());
cntNode = n*n; for(int i = 1; i <= m; i++){ int x = arr[i].x,y = arr[i].y,z = arr[i].z; int fx = find(x),fy = find(y); if(fx != fy){ val[++cntNode] = z,f[fx] = f[fy] = cntNode;; e[fx].push_back(cntNode),e[cntNode].push_back(fx); e[fy].push_back(cntNode),e[cntNode].push_back(fy); } }
dfs(cntNode,0);
int q; cin >> q; int Ax,Ay,Bx,By; while(q--){ cin >> Ax >> Ay >> Bx >> By; cout << val[lca(getId(Ax,Ay),getId(Bx,By))] << endl; } return 0;}```::::
---
### P4197 [ONTAK2010] Peaks
很典的两个 trick 结合。
6 行未改动