poj 3177 双连通分量+缩点

原创 2016年05月31日 10:22:43


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;
}


POJ3177【边双连通分量缩点】

还不会双连通分量的朋友,请扣->这里

poj 3177 Redundant Paths 边双连通分量+缩点

题意:给定n个点m条边。要求
  • CHCXCHC
  • CHCXCHC
  • 2014年07月11日 14:27
  • 636

POJ 3177 Redundant Paths(边双连通分量+缩点)

POJ 3177 Redundant Paths(边双连通分量+缩点) http://poj.org/problem?id=3177 题意:给你一个无向连通图,问你至少需要添加几条边能使得该图是一...

POJ 3177&&3352题解 tarjan算割边 求双连通分量 缩点

首先说一下,身为一个初学者,虽然一遍就AC了,但实际上做这两道模板题花了不少时间,学这个东西还是不能着急,要尽量理解原理才去写代码,不然就算背下来也没什么意思。 POJ题目传送门:http://p...

poj 3177 Redundant Paths(边双连通分量+缩点)★

题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走。现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立的路。两条独立的路是指:没有公共边的路,...
  • Ezereal
  • Ezereal
  • 2016年09月13日 23:18
  • 867

POJ 3177 / POJ 3352 : Redundant Paths / Road Construction - 边双连通分量,缩点

题意:给定现有的R条直接连接2个牧场的路,F-1分析:见代码及注释……《图论算法理论实现应用》——P4123177Accepted700K16MSG++2320B2014-03-14 16:55:30...

poj 3177 poj 3352 (边双连通分量裸题+缩点)

题目:http://poj.org/problem?id=3177 题意: 这两题几乎一样,都是给一个连通图,问最少加几条边可以得到边双连通图。 分析: 先求出边双连通分量(low[i]值相...

Poj 3352 Road Construction & Poj 3177 Redundant Paths(边双连通分量+缩点)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9465 ...

[POJ3177]Redundant Paths 边双连通分量 做题笔记

题目来源:http://poj.org/problem?id=3177 解题思路:http://www.cnblogs.com/frog112111/p/3367039.html 分析:在同一个...
  • mhlwsk
  • mhlwsk
  • 2016年03月08日 23:06
  • 526
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj 3177 双连通分量+缩点
举报原因:
原因补充:

(最多只允许输入30个字)