版本比较
『算法笔记 1』四倍五等分的 Tarjan
正文
增加 153 行删除 12 行
27 行未改动2829303132323334353627 行未改动6465666768686970707172737442 行未改动11711811912012112112212312412522 行未改动1481491501511521521531541541551561561571581591601601611621631641651651661671681694 行未改动1741751761771781781791801811823 行未改动186187188189190191190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246199 行未改动全部展开44644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354452 行未改动27 行未改动 } if(sl == 1) cout << "YES" << endl;//显然合并 n-1 次 else cout << ans << endl; return 0;} }```::::时间复杂度 $O(n+m \alpha(n))$。\不用管 $\alpha(n)$,可以近似认为是 $O(n+m))$。27 行未改动 num = 0,sou(i); ans = max(ans,num); } if(ans == n) cout << "YES" << endl; else cout << ans << endl; else cout << ans << endl; return 0;} }```::::时间复杂度 $O(n+m)$。\(经测试,在 Luogu 及本题数据范围下,DFS 递归的常数导致其平均耗时是并查集的 $1.39$ 倍)。42 行未改动这条边被称之为树边。\然后 $low_x = \min(low_x,low_y)$。\因为 $low$ 的定义如下: > $low_x$ 表示节点及其子节点能访问到的最早($dfn$ 最小)的祖先编号。
$low_y$ 表示 $y$ 在其子节点搜到的最早祖先,$y$ 是 $x$ 的子节点,也要算在 $x$ 搜索到的一部分里。\ 这里还能分出两种情况:- $dfn_x \le low_y$。\这种情况表明 $y$ 不可能找到比 $x$ 更~~远古~~早的子节点。\22 行未改动#define min(a,b) (a<b?a:b)#define endl "\n"int n,m,s,head[514514],next1[514514],to[514514],cnt,uu,vv;int dfn[514514],low[514514],num,ans;bool vis[514514];//是否为割点 bool vis[514514];//是否为割点#define add(a,b) (++cnt,to[cnt] = b,next1[cnt] = head[a],head[a] = cnt)void Tarjan(int x,int father,bool isroot){//isroot 表示是否为根节点 void Tarjan(int x,int father,bool isroot){//isroot 表示是否为根节点 int sum = 0; dfn[x] = low[x] = ++num;//访问顺序 dfn[x] = low[x] = ++num;//访问顺序 for(int i = head[x];i;i = next1[i]){ int y = to[i]; if(y == father) continue; if(dfn[y] == 0){//情况 1 if(dfn[y] == 0){//情况 1 Tarjan(y,x,0); low[x] = min(low[x],low[y]); if(low[y] >= dfn[x]) ++sum; } else low[x] = min(low[x],dfn[y]);//情况 2 else low[x] = min(low[x],dfn[y]);//情况 2 } if(isroot == 1 && sum >= 2) vis[x] = 1,++ans; if(isroot == 0 && sum >= 1) vis[x] = 1,++ans;}4 行未改动 for(int i = 1;i <= m;++i){ cin >> uu >> vv; add(vv,uu),add(uu,vv); } for(int i = 1;i <= n;++i) if(dfn[i] == 0) Tarjan(i,-1,1);//图不保证联通 for(int i = 1;i <= n;++i) if(dfn[i] == 0) Tarjan(i,-1,1);//图不保证联通 cout << ans << endl; for(int i = 1;i <= n;++i) if(vis[i]) cout << i << " "; return 0;}3 行未改动时间复杂度 $O(n+m)$。
### For Example[P5058 嗅探器](https://www.luogu.com.cn/problem/P5058)。\这道题需要保证所有的数据包都能被捕获,也就是说找到一个点 $x$,使得任意两点之间的全部路径都经过 $x$。\这等价于把点 $x$ 删去后整个图就不连通。\这道题需要保证所有的数据包都能被捕获,也就是说找到一个点 $x$,使得两点之间的全部路径都经过 $x$。\如果 $x$ 是割点,且将 $a,b$ 两点割在两边,则该点满足要求。\其实就是割点的板子。::::info[代码]```cpp#include <bits/stdc++.h>using namespace std;int n,u,v,aa,bb,sum,low[214514],dfn[214514],cnt,flag;int answer[214514],head[214514],to[1014514],next1[1014514],cnt1;#define add(a,b) (++cnt1,to[cnt1] = b,next1[cnt1] = head[a],head[a] = cnt1)void tarjan(int x){ low[x] = dfn[x] = ++cnt; for(int i = head[x];i;i = next1[i]){ int y = to[i]; if(dfn[y] == 0){ tarjan(y),low[x] = min(low[x],low[y]); if(low[y] >= dfn[x]){//如果在子树内部出不去,则是割点 if(dfn[bb] >= dfn[y] && aa != x){//如果 bb 在 y 之后被搜到 //因为 y 是 aa 的子孙(从 aa 开始 Tarjan),所以 aa 一定比 y 先搜到 //因此 x 把 aa,bb 分割 //不能把 aa 去掉所以需要特判 answer[x] = 1; flag = 1; } } } else low[x] = min(low[x],dfn[y]); }}int main(){ cin.tie(0),cout.tie(0); cin >> n; while(1){ cin >> u >> v; if(u == 0 && v == 0) break; add(u,v),add(v,u); } cin >> aa >> bb; tarjan(aa); if(flag == 0){ cout << "No solution"; return 0; } for(int i = 1;i <= n;i++){ if(answer[i] == 0) continue; cout << i; return 0; } return 0;}```
::::# 割边暴力方法:删除每条边,然后求连通性,如果连通分量变多,那么就是割边。\显然同样不优。## 实现199 行未改动全部展开::::注意到这两种方法中 $x$ 是否入栈是不一样的。\第一种方法里因为判断的是有没有 $x$ 在的边双(这就是不用判孤立点的原因)。\第二种方法判断的是有没有 $y$ 在的边双(这就是要建立虚拟节点的原因)。
### For Example[This problem](https://www.luogu.com.cn/problem/U643940)。\手搓的,不保证数据正确。\首先把这个东西跑边双,新建超级节点代表边双联通分量,然后就是一片森林。
1. 路径上割边的数量模 $2$ 是一个不变量。\即使有多条最短路径,不会改变经过的割边数量。
2. $dist = 2$ 可以转化为在树上黑白染色。\这样距离是偶数的一定是同一颜色。\记一个**联通分量**中有 $sum_1$ 个黑色起始点,$sum_2$ 个黑色结束点,$sum_3$ 个白色起始点,$sum_4$ 个白色结束点,该**联通分量**的贡献为 $sum_1 \times sum_2 + sum_3 \times sum_4$。\由于图不联通,所以需要枚举累加。::::info[代码]```cpp#include <bits/stdc++.h>using namespace std;#define int long long#define max(a,b) (a>b?a:b)#define min(a,b) (a<b?a:b)#define endl "\n"int n,m,s,head[514514],next1[514514],to[514514],cnt,uu,vv,p[514514];int dfn[514514],low[514514],num,ans,sta[514514],top,c[514514],answer,sum1,sum2,sum3,sum4;bool vis[514514],isbr[514514];int head2[514514],next2[514514],to2[514514],cnt2,S[2][514514];vector<int>g[514514];#define add(a,b) (++cnt,to[cnt] = b,next1[cnt] = head[a],head[a] = cnt)#define add2(a,b) (++cnt2,to2[cnt2] = b,next2[cnt2] = head2[a],head2[a] = cnt2)void sou(int x,int father,bool col){ vis[x] = 1; if(col == 1) sum1 += S[0][x],sum2 += S[1][x]; else sum3 += S[0][x],sum4 += S[1][x]; for(int i = head2[x];i;i = next2[i]){ int y = to2[i]; if(y == father) continue; sou(y,x,!col); }}void Tarjan(int x,int father){ bool f2 = 0;//f2:重边 low[x] = dfn[x] = ++num; sta[++top] = x; for(int i = head[x];i;i = next1[i]){ int y = to[i]; if(dfn[y] == 0){ Tarjan(y,x),low[x] = min(low[x],low[y]); if(dfn[x] < low[y]) isbr[i] = 1; } else if((y == father && f2) || y != father) low[x] = min(low[x],dfn[y]); if(y == father && f2 == 0) f2 = 1; } if(dfn[x] == low[x]){ ++ans; g[ans].push_back(x),p[x] = ans; while(sta[top] != x){ p[sta[top]] = ans; g[ans].push_back(sta[top]); --top; } --top; }}signed main(){ ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); cin >> n >> m; for(int i = 1;i <= n;++i) cin >> c[i]; for(int i = 1;i <= m;++i){ cin >> uu >> vv; add(vv,uu),add(uu,vv); } for(int i = 1;i <= n;++i) if(dfn[i] == 0) Tarjan(i,-1); for(int i = 1;i <= n;++i){ for(int j = head[i];j;j = next1[j]) if(isbr[j]) add2(p[i],p[to[j]]),add2(p[to[j]],p[i]); } for(int i = 1;i <= ans;++i){ for(int j = 0;j < g[i].size();++j) ++S[c[g[i][j]]][i]; } for(int i = 1;i <= ans;++i){ if(vis[i] != 0) continue; sum1 = 0,sum2 = 0,sum3 = 0,sum4 = 0; sou(i,-1,0); answer += sum1 * sum2 + sum3 * sum4; } cout << answer; return 0;}```
::::
# Tip- 要注意 Tarjan 是否取等。- 注意一下 Tarjan 是判定 $x$ 的还是判定 $y$ 的。- 至于差分算法为什么不讲,因为我不会。52 行未改动