关闭

poj 3177 双连通分量+缩点

标签: 双连通分量+缩点
86人阅读 评论(0) 收藏 举报
分类:


Description

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 7
1 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.



双连通图及分量

在无向连通图中,如果删除该图的任何一个结点都不能改变该图的连通性,则该图为双连通的无向图。一个连通的无向图是双连通的,当且仅当它没有割点。

 

双连通分支,就是图的极大双连通子图。特殊的,点双连通分支又叫做块。

 

无向图转化为双连通图:双连通分量+缩点 之后构造双连通图

添加边数=(树中度为1的节点数+1)/2


#include <iostream>
#include <algorithm>
#include <cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<cmath>
#include<cstdlib>
#include<vector>
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
const int MAX=10010;
int dfn[MAX],low[MAX],head[MAX],belong[MAX],is_stack[MAX];
int degree[MAX];//存储双连通分量的缩点的度
stack<int> s;
int n,m,cnt,k,scc;//scc为双连通分量的个数
struct Edge
{
    int to,next;
    Edge(){}
    Edge(int x,int y):to(x),next(y){}
}edge[10*MAX];
void addedge(int cu,int cv)
{
    edge[k].to=cv;
    edge[k].next=head[cu];
    head[cu]=k++;
}
void tarjan(int u,int fa)
{
    low[u]=dfn[u]=++cnt;
    s.push(u);
    is_stack[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(i==(fa^1))
            continue;
        if(!dfn[v])
        {
            tarjan(v,i);
            low[u]=min(low[u],low[v]);
        }
        else if(is_stack[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        scc++;
        while(1)
        {
            int v=s.top();
            s.pop();
            is_stack[v]=0;
            belong[v]=scc;
            if(v==u)
                break;
        }
    }
}
int main()
{
    while(cin>>n)
    {
        cin>>m;
        k=cnt=scc=0;
        memset(head,-1,sizeof(head));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(is_stack,0,sizeof(is_stack));
        memset(degree,0,sizeof(degree));
        while(!s.empty())
            s.pop();
        for(int i=0;i<=m-1;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }
        for(int i=1;i<=n;i++)//点1~n
            if(!dfn[i])
                tarjan(1,-1);
          //得到了belong数组的具体信息
       // for(int i=1;i<=n;i++)
         //   cout<<belong[i]<<" ";
        for(int i=1;i<=n;i++)
        {
            for(int j=head[i];j!=-1;j=edge[j].next)
            {
                int v=edge[j].to;
                if(belong[i]!=belong[v])
                    degree[belong[i]]++;
            }
        }
        int sum=0;
        for(int i=1;i<=n;i++)
            if(degree[i]==1)
                sum+=1;
        cout<<(sum+1)/2<<endl;
    }
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:45335次
    • 积分:2576
    • 等级:
    • 排名:第14231名
    • 原创:213篇
    • 转载:16篇
    • 译文:0篇
    • 评论:3条
    最新评论