边连通分量+缩点

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)/2;这题需要注意输入的边可能有重边。

代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=5000+10;
int n,m;
vector<int> G[maxn];
int dfs_clock;
int pre[maxn],low[maxn];
int degree[maxn];
int tarjan(int u,int fa)
{
    int lowu=pre[u]=++dfs_clock;
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(v==fa) continue;
        if(!pre[v])
        {
            int lowv=tarjan(v,u);
            lowu=min(lowu,lowv);
        }
        else if(pre[v]<pre[u])
            lowu=min(lowu,pre[v]);
    }
    return low[u]=lowu;
}
bool vis[maxn][maxn];
int main()
{
    scanf("%d%d",&n,&m);
    dfs_clock=0;
    memset(pre,0,sizeof(pre));
    memset(degree,0,sizeof(degree));
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++) G[i].clear();
    for(int i=1;i<=m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
       if(!vis[u][v])//去重边。
        {
            vis[u][v]=vis[v][u]=1;
            G[u].push_back(v);
            G[v].push_back(u);
        }
    }
    tarjan(1,-1);
    for(int u=1;u<=n;u++)
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(low[u]!=low[v]) degree[low[v]]++;
    }
    int cnt=0;
    for(int i=1;i<=n;i++)if(degree[i]==1)
        cnt++;
    printf("%d\n",(cnt+1)/2 );
}
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 10010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
struct edge
{
    int v,next;
    edge(){}
    edge(int v,int next):v(v),next(next){}
}e[N<<1];
int n,step,scc,top,tot;
int head[N],dfn[N],low[N],belong[N],Stack[N],du[N];
bool instack[N],vis[N<<1];
void init()
{
    tot=0;top=0;scc=0;
    FILL(head,-1);FILL(dfn,0);
    FILL(low,0);FILL(instack,false);
    FILL(du,0);FILL(vis,0);
}
void addedge(int u,int v)
{
    e[tot]=edge(v,head[u]);
    head[u]=tot++;
}
void tarjan(int u)
{
    int v;
    dfn[u]=low[u]=++step;
    Stack[top++]=u;
    instack[u]=true;
    for(int i=head[u];~i;i=e[i].next)
    {
        v=e[i].v;
        if(vis[i])continue;
        vis[i]=vis[i^1]=1;
        if(!dfn[v])
        {
            tarjan(v);
            if(low[u]>low[v])low[u]=low[v];
        }
        else if(instack[v])
        {
            if(low[u]>dfn[v])low[u]=dfn[v];
        }
    }
    if(dfn[u]==low[u])
    {
        scc++;
        do
        {
            v=Stack[--top];
            instack[v]=false;
            belong[v]=scc;
        }while(v!=u);
    }
}
void solve()
{
    for(int i=1;i<=n;i++)
        if(!dfn[i])tarjan(i);
    for(int u=1;u<=n;u++)
    {
        for(int i=head[u];~i;i=e[i].next)
        {
            int v=e[i].v;
            if(belong[u]!=belong[v])
            {
                du[belong[u]]++;du[belong[v]]++;
            }
        }
    }
    int sum=0;
    for(int i=1;i<=scc;i++)
        if(du[i]/2==1)sum++;//因为无向图每条边都有正反两个方向,因此所有的点的度都增加了一倍
    printf("%d\n",(sum+1)/2);
}
int main()
{
    int m,u,v;
    while(scanf("%d%d",&n,&m)>0)
    {
        init();
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }
        solve();
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值