双连通 缩点 求桥模版:
- #include <stdio.h>
- #include <iostream>
- #include <algorithm>
- #include <string.h>
- #include <queue>
- #include <vector>
- using namespace std;
- #define N 100005
- #define M 200300
- #define inf 10000000
- struct Edge{
- int from,to,next;
- bool cut;
- }edge[2*M];
- int head[N],edgenum;
- int Low[N],DFN[N],Stack[N];//Belong数组的值是1~block
- int Index,top;
- int Belong[N],block;//新图的连通块标号(1~block)
- bool Instack[N];
- int bridge; //割桥数量
- void addedge(int u,int v){
- Edge E={u,v,head[u],0}; edge[edgenum]=E; head[u] = edgenum++;
- Edge E2={v,u,head[v],0};edge[edgenum]=E2;head[v] = edgenum++;
- }
- void Tarjan(int u,int pre){
- int v;
- Low[u] = DFN[u] = ++Index;
- Stack[top++] = u;
- Instack[u] = true;
- for(int i = head[u]; ~i ;i = edge[i].next){
- v = edge[i].to;
- // 如果重边有效的话下面这句改成: if(v == pre && pre_num == 0){pre_num++;continue;} pre_num在for上面定义 int pre_num=0;
- if( v == pre )continue;
- if( !DFN[v] ){
- Tarjan(v,u);
- if(Low[u] > Low[v])Low[u] = Low[v];
- if(Low[v] > Low[u]){
- bridge++;
- edge[i].cut = true;
- edge[i^1].cut = true;
- }
- }
- else if(Instack[v] && Low[u] > DFN[v])Low[u] = DFN[v];
- }
- if(Low[u] == DFN[u]){
- block++;
- do{
- v = Stack[--top];
- Instack[v] = false;
- Belong[v] = block;
- }while( v != u );
- }
- }
- void work(int l, int r){
- memset(DFN,0,sizeof(DFN));
- memset(Instack,false,sizeof(Instack));
- Index = top = block = bridge = 0;
- for(int i = l; i <= r; i++)if(!DFN[i])Tarjan(i,i);
- }
- vector<int>G[N];//点标从1-block
- void suodian(){
- for(int i = 1; i <= block; i++)G[i].clear();
- for(int i = 0; i < edgenum; i+=2){
- int u = Belong[edge[i].from], v = Belong[edge[i].to];
- if(u==v)continue;
- G[u].push_back(v), G[v].push_back(u);
- }
- }
- void init(){edgenum = 0; memset(head,-1,sizeof(head));}
求割点和桥 (binshen模版)
- #include <stdio.h>
- #include <string.h>
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <queue>
- #include <set>
- #include <map>
- #include <string>
- #include <math.h>
- #include <stdlib.h>
- #include <time.h>
- using namespace std;
- const int INF = 0x3f3f3f3f;
- /*
- * 求 无向图的割点和桥
- * 可以找出割点和桥,求删掉每个点后增加的连通块。
- * 需要注意重边的处理,可以先用矩阵存,再转邻接表,或者进行判重
- */
- #define N 10005
- #define M 200010
- struct Edge{
- int to,next,w;
- bool cut;//是否为桥的标记
- }edge[M*2];
- int head[N],edgenum;
- int Low[N],DFN[N],Stack[N],Index,top;
- bool Instack[N];
- bool cut[N]; //是否为割点
- int add_block[N];//删除i点后增加的连通块数量为add_block[i]
- int bridge; //桥数量
- void addedge(int u,int v,int w)
- {
- edge[edgenum].to = v;edge[edgenum].next = head[u];edge[edgenum].cut = false;
- edge[edgenum].w = w;
- head[u] = edgenum++;
- edge[edgenum].to = u;edge[edgenum].next = head[v];edge[edgenum].cut = false;
- edge[edgenum].w = w;
- head[v] = edgenum++;
- }
- void Tarjan(int u,int pre){
- int v;
- Low[u] = DFN[u] = ++Index;
- Stack[top++] = u;
- Instack[u] = true;
- int son = 0;
- int pre_num = 0;
- for(int i = head[u];i != -1;i = edge[i].next)
- {
- v = edge[i].to;
- //若重边无效则把下面这句换成 if(v == pre)continue;
- if(v == pre && pre_num == 0){pre_num++;continue;}
- if( !DFN[v] )
- {
- son++;
- Tarjan(v,u);
- if(Low[u] > Low[v])Low[u] = Low[v];
- //桥
- //一条无向边(u,v)是桥,当且仅当(u,v)为树枝边,且满足DFS(u)<Low(v)。
- if(Low[v] > DFN[u])
- {
- bridge++;
- edge[i].cut = true;
- edge[i^1].cut = true;
- }
- //割点
- //一个顶点u是割点,当且仅当满足(1)或(2) (1) u为树根,且u有多于一个子树。
- //(2) u不为树根,且满足存在(u,v)为树枝边(或称父子边,
- //即u为v在搜索树中的父亲),使得DFS(u)<=Low(v)
- if(u != pre && Low[v] >= DFN[u])//不是树根
- {
- cut[u] = true;
- add_block[u]++;
- }
- }
- else if( Low[u] > DFN[v])
- Low[u] = DFN[v];
- }
- //树根,分支数大于1
- if(u == pre && son > 1)cut[u] = true;
- if(u == pre)add_block[u] = son - 1;
- Instack[u] = false;
- top--;
- }
- void solve(int l, int r){ //点标区间为[l,r]
- memset(DFN,0,sizeof(DFN));
- memset(Instack,false,sizeof(Instack));
- memset(add_block,0,sizeof(add_block));
- memset(cut,false,sizeof(cut));
- Index = top = bridge = 0;
- for(int i = l;i <= r;i++)if(!DFN[i])Tarjan(i,i);
- }
- void init(){ edgenum = 0; memset(head,-1,sizeof(head));}
模版1:重边有效 白书模版:
- #include<stdio.h>
- #include<iostream>
- #include<string.h>
- #include<algorithm>
- #include<vector>
- #include<math.h>
- #include<queue>
- #include<set>
- using namespace std;
- #define N 10050
- #define M 200005
- int n,m;//n个点 m条边 点标从1开始
- struct Edge{
- int from,to,val,nex;
- bool cut;//记录这条边是否为割边
- }edge[2*M];//双向边则 edge[i]与edge[i^1]是2条反向边
- int head[N],edgenum;//在一开始就要 memset(head,-1,sizeof(head)); edgenum=0;
- int low[N],dfn[N],tarjin_time;
- void add(int u,int v,int w){
- Edge E={u,v,w,head[u],0};
- edge[edgenum]=E;
- head[u]=edgenum++;
- Edge E2={v,u,w,head[v],0};
- edge[edgenum]=E2;
- head[v]=edgenum++;
- }
- void tarjin(int u,int pre)
- {
- low[u]=dfn[u]= ++tarjin_time;
- int flag=1;//flag是阻止双向边的反向边 i和i^1
- for(int i=head[u];i!=-1;i=edge[i].nex)
- {
- int v=edge[i].to;
- if(flag&&v==pre)
- {
- flag=0;
- continue;
- }
- if(!dfn[v])
- {
- tarjin(v,u);
- if(low[u]>low[v])low[u]=low[v];
- if(low[v]>dfn[u])//是桥low[v]表示v能走到的最早祖先 有重边且u是v的最早祖先 则low[v]==dfn[u],所以不会当作桥
- edge[i].cut=edge[i^1].cut=true;
- }
- else if(low[u]>dfn[v])low[u]=dfn[v];
- }
- }
- void find_edgecut()
- {
- memset(dfn,0,sizeof(dfn));
- tarjin_time=0;
- for(int i=1;i<=n;i++)if(!dfn[i])tarjin(i,i);
- }
- void init(){memset(head, -1, sizeof head); edgenum = 0;}
重边算1条:
- #include <stdio.h>
- #include <algorithm>
- #include <iostream>
- #include <string.h>
- #include <vector>
- using namespace std;
- /*
- * 求 无向图的割点和桥
- * 可以找出割点和桥,求删掉每个点后增加的连通块。
- * 这个模版是重边算1条
- */
- const int MAXN = 10010;
- const int MAXM = 100010;
- struct Edge{
- int to,next;
- bool cut;//是否为桥的标记
- }edge[MAXM*2];
- int head[MAXN],edgenum;
- int Low[MAXN],DFN[MAXN],Stack[MAXN];
- int Index,top;
- bool Instack[MAXN];
- bool cut[MAXN]; //点是否为割点
- int add_block[MAXN];//删除一个点后增加的连通块
- int bridge;
- void add(int u,int v){
- edge[edgenum].to = v;edge[edgenum].next = head[u];edge[edgenum].cut = false;
- head[u] = edgenum++;
- edge[edgenum].to = u;edge[edgenum].next = head[v];edge[edgenum].cut = false;
- head[v] = edgenum++;
- }
- void Tarjan(int u,int pre){
- int v;
- Low[u] = DFN[u] = ++Index;
- Stack[top++] = u;
- Instack[u] = true;
- int son = 0;
- for(int i = head[u];i != -1;i = edge[i].next)
- {
- v = edge[i].to;
- if(v == pre)continue;
- if( !DFN[v] )
- {
- son++;
- Tarjan(v,u);
- if(Low[u] > Low[v])Low[u] = Low[v];
- //桥
- //一条无向边(u,v)是桥,当且仅当(u,v)为树枝边,且满足DFS(u)<Low(v)。
- if(Low[v] > DFN[u])
- {
- bridge++;
- edge[i].cut = true;
- edge[i^1].cut = true;
- }
- //割点
- //一个顶点u是割点,当且仅当满足(1)或(2) (1) u为树根,且u有多于一个子树。
- //(2) u不为树根,且满足存在(u,v)为树枝边(或称父子边,
- //即u为v在搜索树中的父亲),使得DFS(u)<=Low(v)
- if(u != pre && Low[v] >= DFN[u])//不是树根
- {
- cut[u] = true;
- add_block[u]++;
- }
- }
- else if( Low[u] > DFN[v])
- Low[u] = DFN[v];
- }
- //树根,分支数大于1
- if(u == pre && son > 1)cut[u] = true;
- if(u == pre)add_block[u] = son - 1;
- Instack[u] = false;
- top--;
- }
- void solve(int n){
- memset(DFN,0,sizeof(DFN));
- memset(Instack,false,sizeof(Instack));
- memset(add_block,0,sizeof(add_block));
- memset(cut,false,sizeof(cut));
- Index = top = 0;
- bridge = 0;
- for(int i = 1;i <= n;i++)if(!DFN[i]) Tarjan(i,i);
- }
- void init(){edgenum = 0; memset(head,-1,sizeof(head));}