题意:给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图。
链接:点我
kuangbin模板题,分析链接:点我
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
const int MAXN = ;//点数
const int MAXM = ;//边数,因为是无向图,所以这个值要*2
struct Edge
{
int to,next;
bool cut;//是否是桥标记
}edge[MAXM];
int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong数组的值是1~block
int Index,top;
int block;//边双连通块数
bool Instack[MAXN];
int bridge;//桥的数目
void addedge(int u,int v)
{
edge[tot].to = v;edge[tot].next = head[u];edge[tot].cut=false;
head[u] = tot++;
}
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)continue;
if( !DFN[v] )
{
Tarjan(v,u);
if( Low[u] > Low[v] )Low[u] = Low[v];
if(Low[v] > DFN[u])
{
bridge++;
edge[i].cut = true;
edge[i^].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 init()
{
tot = ;
memset(head,-,sizeof(head));
}
int du[MAXN];//缩点后形成树,每个点的度数
void solve(int n)
{
memset(DFN,,sizeof(DFN));
memset(Instack,false,sizeof(Instack));
Index = top = block = ;
Tarjan(,);
int ans = ;
memset(du,,sizeof(du));
for(int i = ;i <= n;i++)
for(int j = head[i];j != -;j = edge[j].next)
if(edge[j].cut)
du[Belong[i]]++;
for(int i = ;i <= block;i++)
if(du[i]==)
ans++;
//找叶子结点的个数ans,构造边双连通图需要加边(ans+1)/2
printf("%d\n",(ans+)/);
}
int main()
{
int n,m;
int u,v;
while(scanf("%d%d",&n,&m)==)
{
init();
while(m--)
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
solve(n);
}
return ;
}