题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1588
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; /* * 求 无向图的割点和桥 * 可以找出割点和桥,求删掉每个点后增加的连通块。 * 需要注意重边的处理,可以先用矩阵存,再转邻接表,或者进行判重 */ const int MAXN = 10010; const int MAXM = 2000010; struct Edge { int to,next; int w; bool cut;//是否为桥的标记 }edge[MAXM]; int head[MAXN],tot; int Low[MAXN],DFN[MAXN],Stack[MAXN]; int Index,top; bool Instack[MAXN]; bool cut[MAXN]; int add_block[MAXN];//删除一个点后增加的连通块 int bridge; void addedge(int u,int v,int w) { edge[tot].to = v;edge[tot].next = head[u];edge[tot].cut = false; edge[tot].w = w; head[u] = tot++; edge[tot].to = u;edge[tot].next = head[v];edge[tot].cut = false; edge[tot].w = w; head[v] = tot++; } 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 && 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 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(){ tot = 0; memset(head,-1,sizeof(head));} vector<int>G; int n, m; int main(){ int u, v, i, j, T; scanf("%d",&T); while(T--){ scanf("%d %d",&n,&m); init(); while(m--){ scanf("%d %d",&u,&v); addedge(u,v,1); } solve(n); G.clear(); for(i=0;i<tot;i+=2)if(edge[i].cut)G.push_back(i/2); printf("%d\n",G.size()); for(i=0;i<G.size();i++){ printf("%d",G[i]+1); i==G.size()-1?puts(""):printf(" "); } if(T)puts(""); } return 0; }
#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<vector> using namespace std; #define N 10050 #define M 200005 int n,m;//n个点 m条边 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;} vector<int>G; int main(){ int u, v, i, j, T; scanf("%d",&T); while(T--){ scanf("%d %d",&n,&m); init(); while(m--){ scanf("%d %d",&u,&v); add(u,v,1); } find_edgecut(); G.clear(); for(i=0;i<edgenum;i+=2)if(edge[i].cut)G.push_back(i/2); printf("%d\n",G.size()); for(i=0;i<G.size();i++){ printf("%d",G[i]+1); i==G.size()-1?puts(""):printf(" "); } if(T)puts(""); } return 0; }
ZOJ 2588 Burning Bridges 求无向图桥 边双连通裸题,码迷,mamicode.com
时间: 2024-12-28 21:31:19