题意:给定一无向连通图G,问最少再加多少条边使图变成双连通分量(删除任意一条边后任意两点之间仍然可达)。
题解:
- 易知求出图中各个极大双连通分量,对其进行缩点(所有low值相同的点)。
- 缩点以后形成的新图G‘,易知G’为连通图,若图中所有点度数均为2,可知图G‘满足双连通分量条件,所以我们统计G’中度数为1的点,假设其个数为n,用归纳法可知,连接这n个点至少需要(n+1)/2(向下取整)条边。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
class node
{
public:
int x;
node* next;
node():next(0){}
};
class adjList
{
private:
int N;
node** adj;
public:
adjList(int n):N(n),adj(new node*[n])
{
memset(adj,0,sizeof(node*)*(N));
}
void adjInsert(int a,int b)
{
node*newNode = new node;
newNode->x = b;
newNode->next = adj[a];
adj[a] = newNode;
}
node* adjNode(int x)
{
return adj[x];
}
};
class solve
{
private:
int N,R;
adjList* graph;
int* DFN;
int* low;
int* bccIndex;
int* degree;
int index;
public:
solve(int n,int r):N(n),R(r),index(1)
{
graph = new adjList(N+5);
DFN = new int[N+5];
low = new int[N+5];
degree = new int[N+5];
memset(DFN,0,sizeof(int)*(N+1));
memset(degree,0,sizeof(int)*(N+5));
processIn();
tarjan(1,0);
calcDegree();
addEdge();
}
void processIn();
void tarjan(int v,int u);
void calcDegree();
void addEdge();
};
void solve::processIn()
{
int a,b;
while(R--)
{
scanf("%d%d",&a,&b);
graph->adjInsert(a,b);
graph->adjInsert(b,a);
}
return ;
}
void solve::tarjan(int v,int u)
{
DFN[v] = low[v] = index++;
for(node* tmpNode = graph->adjNode(v);tmpNode != NULL;tmpNode = tmpNode->next)
{
int w = tmpNode->x;
if(w != u&&w != v)
{
if(!DFN[w]) //树边
{
tarjan(w,v);
low[v] = min(low[v],low[w]);
}
else if(DFN[w] < DFN[v]) //后向边
{
low[v] = min(low[v],DFN[w]);
}
}
}
return ;
}
void solve::calcDegree()
{
node* tmpNode;
for(int u = 1;u <= N;u++)
{
for(tmpNode = graph->adjNode(u);tmpNode != NULL;tmpNode = tmpNode->next)
{
int v = tmpNode->x;
if(v != u&&low[u] != low[v]) //low值一样的都在同一个双连通分量
{
degree[low[u]]++;
degree[low[v]]++;
}
}
}
return ;
}
void solve::addEdge()
{
int num = 0;
for(int i = 1;i <= N;i++)
{
if(degree[i] == 2) //无向图每边计算了两次
{
num++;
}
}
printf("%d\n",(num+1)>>1); //使叶节点连通的最小边数
return ;
}
int main()
{
int n,r;
while(~scanf("%d%d",&n,&r))
{
solve poj_3352(n,r);
}
return 0;
}