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, v and w, separated by a space, indicating that a road exists between the attractions labelled v and 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
Sample Input 1 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 Sample Input 2 3 3 1 2 2 3 1 3
Sample Output
Output for Sample Input 1 2 Output for Sample Input 2 0
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
//给你一个无向图,要求你加入最少的边,使得最后得到的图为一个双连通分支。
//所谓的双连通分支,即不存在桥的连通分支.
//可以求出所有的桥,把桥删掉。然后把所有的连通分支求出来,显然这些连通分支就是
//原图中的双连通分支。把它们缩成点,然后添上刚才删去的桥,就构成了一棵树。在树上添
//边使得树变成一个双连通分支即可
//只要求输出一共需要添加多少条边,统计度为1的节点(设共有x个),
//然后直接输出(x+1)/2即可.
///此题中没有重边
const int maxn=4000;
struct edge
{
int t,index;
int next;
};
int V,E;
int p[maxn];
edge G[maxn];
int l;
void init()
{
memset(p,-1,sizeof(p));
l=0;
}
void addedge(int u,int t,int index,int l)
{
G[l].t=t;
G[l].index=index;
G[l].next=p[u];
p[u]=l;
}
int isbridge[maxn];
//tarjan 求割点 割边(没有重边)
int cut[maxn];//cut[i]非0表示i是割点
int color[maxn];//颜色:0表示没有访问,1表示正在访问,2表示访问结束
int lowc[maxn];//表示i及i的子孙相连的辈分最高的祖先节点所在的深度
int d[maxn];//表示i节点在树中的深度
int root;//根节点
int fath;//父节点
int pcnt;//割点个数
int egcnt;//割边个数
int egt[maxn];
int egu[maxn];
int egv[maxn];
void dfs(int u,int fath,int deep)
{
color[u]=1;//正在访问
lowc[u]=d[u]=deep;//深度
int tot=0;//子树个数
for(int i=p[u];i!=-1;i=G[i].next)
{
int t=G[i].t,index=G[i].index;
if(t!=fath&&color[t]==1)
{
lowc[u]=min(lowc[u],d[t]);
}
if(color[t]==0)
{
dfs(t,u,deep+1);
tot++;//子树加1
lowc[u]=min(lowc[u],lowc[t]);
//求割点
//if((u==root&&tot>1)||(u!=root&&lowc[t]>=d[u])) cut[u]=1;//不能将pscnt++写到这里
//求割边
if(lowc[t]>d[u]) //edge[u][t]=true; u->t是割边
{
//判断重边
egu[egcnt]=u;
egv[egcnt]=t;
egt[egcnt++]=index;
isbridge[index]=1;
}
}
}
color[u]=2;
}
void calc()
{
pcnt=egcnt=0;
memset(color,0,sizeof(color));
memset(lowc,0,sizeof(lowc));
memset(d,0,sizeof(d));
memset(cut,0,sizeof(cut));
root=1;
dfs(1,-1,1);
//for(int i=1;i<=V;i++) if(cut[i]) pcnt++;
}
//去掉桥边 缩点
int tp[maxn];
edge tG[maxn];
int tl;
void taddedge(int u,int t,int index,int l)
{
tG[l].t=t;
tG[l].index=index;
tG[l].next=tp[u];
tp[u]=l;
}
int vis[maxn];
int belg[maxn];//节点i属于第几块
void find(int u,int id)
{
vis[u]=1;belg[u]=id;
for(int i=p[u];i!=-1;i=G[i].next)
{
int t=G[i].t,index=G[i].index;
if(!isbridge[index]&&!vis[t])
{
find(t,id);
}
}
}
int Xdu1;
int du[maxn];//重建图后加点的度
void rebuildgraph()
{
memset(tp,-1,sizeof(tp));
tl=0;
int det=0;//缩点后节点个数
memset(vis,0,sizeof(vis));
memset(belg,0,sizeof(belg));
memset(du,0,sizeof(du));
for(int i=1;i<=V;i++)
{
if(!vis[i])
{
find(i,++det);
}
}
for(int i=0;i<egcnt;i++)//缩点后再加上桥边
{
int u=egu[i],v=egv[i];
int tu=belg[u],tv=belg[v];
taddedge(tu,tv,i+1,tl++);
taddedge(tv,tu,i+1,tl++);
}
//无向图 故最后度要除以2
for(int i=1;i<=det;i++)
{
for(int j=tp[i];j!=-1;j=tG[j].next)
{
du[i]++;
du[tG[j].t]++;
}
}
Xdu1=0;
for(int i=1;i<=det;i++)
{
if(du[i]/2==1) Xdu1++;
}
}
int main()
{
while(scanf("%d%d",&V,&E)==2)
{
init();
memset(isbridge,0,sizeof(isbridge));
for(int i=1;i<=E;i++)
{
int u,t,index=i;
scanf("%d%d",&u,&t);
addedge(u,t,index,l++);
addedge(t,u,index,l++);
}
calc();
//删除桥边 缩点
rebuildgraph();
printf("%d\n",(Xdu1+1)/2);
}
return 0;
}