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


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

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

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

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

题意:给定n个点m条边。要求

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

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

POJ3177【边双连通分量缩点】

还不会双连通分量的朋友,请扣->这里<-题意:No response.思路:在一个边双连通分量里面,所有的结点的low[ ]都是一样的哟。 所以可以缩点哟。 缩完点以后一定要证明哟。//#incl...

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

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

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

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

POJ3177 Redundant Paths 3352的双胞胎题 双连通分量

虽然基本上和3352是一样的题意,但是要数据比较严格,这题有可能出现重边,而3352不会有重边。 思路: 直接在POJ3352,即上一篇博文上的代码稍微改动。 新建一个char matc...

POJ3177_Redundant_Paths_边双连通分量_tarjan

题意: 给一个图,问你最少添加多少条边可以成为一个双连通图(就是去掉任何一条边后图仍然连通) 题解:【摘自北大的集训课件】 只需在求出所有的桥以后,把桥边删除,原图变成了多个连通块,则每个连通块...

POJ3177 求边双连通分量

POJ这两天真是专业坑人啊,每次被坑都是被POJ坑,累觉不爱。从输出double用%fWA了一天,到OJ垃圾判个题要十分钟,还有scanf里面多打个z RE了2个小时,总之被坑永远在POJ.多了就不吐...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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