poj 3177 Redundant Paths 加入最少的边,使得无向图为一个双连通分支 有重边

时间:2022-07-06 18:42:56
In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another. 

Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way. 

There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.

Input

Line 1: Two space-separated integers: F and R 

Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

Output

Line 1: A single integer that is the number of new paths that must be built.

Sample Input

7 71 2
2 3
3 4
2 5
4 5
5 6
5 7

Sample Output

2

Hint

Explanation of the sample: 

One visualization of the paths is: 
   1   2   3   +---+---+         |   |       |   | 6 +---+---+ 4      / 5     /     /  7 +
Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions. 
   1   2   3   +---+---+     :   |   |   :   |   | 6 +---+---+ 4      / 5  :     /     :    /      : 7 + - - - - 
Check some of the routes: 
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2 
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4 
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7
 
Every pair of fields is, in fact, connected by two routes. 

It's possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.




#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
//给你一个无向图,要求你加入最少的边,使得最后得到的图为一个双连通分支。
//所谓的双连通分支,即不存在桥的连通分支.
//可以求出所有的桥,把桥删掉。然后把所有的连通分支求出来,显然这些连通分支就是
//原图中的双连通分支。把它们缩成点,然后添上刚才删去的桥,就构成了一棵树。在树上添
//边使得树变成一个双连通分支即可
//只要求输出一共需要添加多少条边,统计度为1的节点(设共有x个),
//然后直接输出(x+1)/2即可.


///此题中有重边
const int maxn=80000;
//hash判重
int hash[maxn];
int repeat[maxn];
int ind[maxn];
void init_hash()
{
    memset(hash,-1,sizeof(hash));
    memset(repeat,0,sizeof(repeat));
    memset(ind,0,sizeof(ind));
}
int ishash(int u,int t,int index)//true 表示已经存在
{
    int tmp=u*10000+t;
    int ret=tmp%maxn;
    while(hash[ret]!=-1&&hash[ret]!=tmp)
    {
        ret=(ret+1)%maxn;
    }
    if(hash[ret]==tmp) return ind[ret];
    hash[ret]=tmp;
    ind[ret]=index;
    return -1;
}
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是割边
            {
                //判断重边
                if(repeat[index])continue;
                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();
        init_hash();
        memset(isbridge,0,sizeof(isbridge));
        for(int i=1;i<=E;i++)
        {
            int u,t,index=i;
            scanf("%d%d",&u,&t);
            if(u>t) swap(u,t);
            int ret;
            if((ret=ishash(u,t,index))!=-1)
            {
                repeat[ret]=1;
                continue;
            }
            addedge(u,t,index,l++);
            addedge(t,u,index,l++);
        }
        calc();
        //删除桥边 缩点
        rebuildgraph();
        printf("%d\n",(Xdu1+1)/2);
    }
    return 0;
}