用tarjan算法求割边(桥)
原理
发现T边(u,v)时若发现v和它的后代不存在一条连接u或其祖先的B边,则删除(u,v)后u和v不连通,因此(u,v)为桥
桥的判定算法
发现T边(u, v)时若low[v]>=dfn[u],则(u,v)为桥
-
基本算法同tarjan经典算法
-
形参加上father,作用为记录u的父亲节点,避免在遍历v时遇到重边重新更新low[u]
-
在“v已被遍历”的位置加上判断 如果v点等于father,那么就不执行更新low[u]值
-
Tarjan算法结束后,遍历所有节点u及其子节点v,如果low[u]==low[v],那么这条边就为割边
有桥的连通图,加边变成双连通图
-
tarjan找桥边并删除桥边
-
将点双连通分量收缩成为一个顶点
-
加回桥边,统计度为1的顶点个数
-
根据规律,需要加入的桥边最少为(n+1)/2条
S - Road Construction POJ - 3352
It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.
The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.
Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.
So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.
Input
The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, vand w, separated by a space, indicating that a road exists between the attractions labelled vand w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.
Output
One line, consisting of an integer, which gives the minimum number of roads that we need to add.
Sample Input
10 12 1 2 1 3 1 4 2 5 2 6 5 6 3 7 3 8 7 8 4 9 4 10 9 10 3 3 1 2 2 3 1 3
Sample Output
2 0
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
const int maxn=5009;
const int maxm=20009;
struct node
{
int to,next;
bool cut;
}edge[maxm];
stack <int> st;
int head[maxn],low[maxn],dfn[maxn],belong[maxn];
int degree[maxn];
bool instack[maxn];
int n,m,e,Bcnt,index,block;
void init()
{
while(!st.empty()) st.pop();
memset(head,-1,sizeof(head));
memset(degree,0,sizeof(degree));
memset(instack,0,sizeof(instack));
memset(belong,-1,sizeof(belong));
Bcnt=e=index=block=0;
}
void add(int u,int v)
{
edge[e].to=v;
edge[e].next=head[u];
edge[e].cut=0;
head[u]=e++; //因为tarjan是有异或的所以边从第零条开始
}
void tarjan(int u,int father)
{
low[u]=dfn[u]=++index;
st.push(u);
instack[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(v==father) continue;
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u]) //此边为割边——桥
{
Bcnt++;
edge[i].cut=1;
edge[i^1].cut=1;
}
}
else if(instack[u]&&low[u]>dfn[v]) low[u]=dfn[v];
}
if(low[u]==dfn[u]) //正常tarjan缩点
{
block++;
for(;;)
{
int v=st.top();
st.pop();
instack[v]=0;
belong[v]=block;
if(v==u) break;
}
}
}
int main()
{
int cas=0;
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
int ans=0;
tarjan(1,-1);
for(int i=1;i<=n;i++)
{
for(int j=head[i];j!=-1;j=edge[j].next)
{
if(edge[j].cut) degree[belong[i]]++; //如果此边是割边
}
}
for(int i=1;i<=block;i++)
{
if(degree[i]==1) ans++;
}
printf("%d\n",(ans+1)/2);
}
return 0;
}