hdu-3394-无向图的双连通分量

本文介绍了一种算法,用于分析无向图中的桥边和冲突边的数量。通过对图进行深度优先搜索并利用双连通分量的概念,该算法能够有效地识别出哪些边是多余的(即桥边),哪些边可能会发生冲突。文章提供了详细的实现步骤及样例输入输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Railway


Problem Description
There are some locations in a park, and some of them are connected by roads. The park manger needs to build some railways along the roads, and he would like to arrange tourist routes to each circuit. If a railway belongs to more than one tourist routes, there might be clash on it, and if a railway belongs to none tourist route, it doesn’t need to build.
Now we know the plan, and can you tell us how many railways are no need to build and how many railways where clash might happen.
 

Input
The Input consists of multiple test cases. The first line of each test case contains two integers, n (0 < n <= 10000), m (0 <= m <= 100000), which are the number of locations and the number of the railways. The next m lines, each line contains two integers, u, v (0 <= u, v < n), which means the manger plans to build a railway on the road between u and v.
You can assume that there is no loop and no multiple edges.
The last test case is followed by two zeros on a single line, which means the end of the input.
 

Output
Output the number of railways that are no need to build, and the number of railways where clash might happen. Please follow the format as the sample.
 

Sample Input
  
8 10 0 1 1 2 2 3 3 0 3 4 4 5 5 6 6 7 7 4 5 7 0 0
 

Sample Output
  
1 5
 

Author
momodi@whu
 

Source
 


题意:给一个无向图。如果至少有两个环共用了一些边,那么这些边被认为是“冲突边”。如果一些边不在任何一个环中,这些边被认为是“多余边”。你要找出这个图中有多少“多余边”和“冲突边”然后输出条数。无重边

 

1.“多余边”不在任何一个环中,那么多余边一定是桥,所以统计这个无向图中有多少桥即可

2.“冲突边”:如果一个环有n个点刚好n条边,例如(1,2,3,1)这种环,这个环内,一条“冲突边”都没有,但是如果一个环内的边数大于点数,那么这个环内所有边都是“冲突边”(因为有多出来的那些边后,相当于把最外面的大环分割成了内部的几个小环,这些小环和小环之间,小环和大环之间一定会公用一些边,这些边就是“冲突边”,而且可以发现,所有边都会被公用),例如sample里面的(5,6)(5,4)(6,7)(4,7)(5,7),相当于最外面的大环<6,5,4,7,6> , 而里面的边(5,7)把这个大环分割成了两个小环(此处题意分析参考了其他人的)

一个没有关节点(割点)的连通图称为双连通图。


#include<stdio.h>
#include<string.h>
#include<vector>
#include<stack>
#include<algorithm>
#define maxn 10005
using namespace std;
int pre[maxn];      //时间戳
int bccno[maxn];       //记录i结点所属的连通分量编号
int dfs_clock,bcc_cnt,sum,n,cnt1;
vector<int> G[maxn];
struct edge
{
    int U,V;
};
stack<edge> S;
int dfs(int u,int fa)
{
    int v;
    int lowu=pre[u]=++dfs_clock;
    int child=0;
    for(int i=0; i<G[u].size(); i++)
    {
        v=G[u][i];
        edge e;
        e.U=u;
        e.V=v;
        if(!pre[v])
        {
            S.push(e);
            child++;
            int lowv=dfs(v,u);
            lowu=min(lowu,lowv);
            if(lowv>=pre[u])        
            {
                if(lowv>pre[u])      //uv为桥
                {
                    cnt1++;         //桥的计数
                }
                bcc_cnt++;        
                int nume=0;
                for(;;)
                {
                    edge x=S.top();
                    S.pop();
                    nume++;
                    if(bccno[x.U]!=bcc_cnt)
                    {
                        bccno[x.U]=bcc_cnt;             //表示u结点属于bcc_cnt这个连通分量
                    }
                    if(bccno[x.V]!=bcc_cnt)
                    {
                        bccno[x.V]=bcc_cnt;
                    }
                    if(x.U == u && x.V == v) break;
                }
                int cnt=0;
                for(int i=0;i<n;i++)
                {
                    if(bccno[i]==bcc_cnt)
                        cnt++;
                }
                if(nume>cnt)
                    sum+=nume;
            }
        }
        else if(pre[v] < pre[u] && v!=fa)
        {
            S.push(e);
            lowu=min(lowu,pre[v]);
        }
    }
    return lowu;
}
void find_bcc()
{
    sum=0;
    cnt1=0;
    memset(pre,0,sizeof(pre));
    memset(bccno,0,sizeof(bccno));
    dfs_clock=bcc_cnt=0;
    for(int i=0; i<n; i++)
    {
        if(!pre[i])
            dfs(i,-1);
    }
}
int main()
{
    int m,i,a,b,j,cnt;
    while(~scanf("%d%d",&n,&m)&&(n||m))
    {
        cnt=0;
        for(i=0; i<n; i++)
            G[i].clear();
        for(i=1; i<=m; i++)
        {
            scanf("%d%d",&a,&b);
            G[a].push_back(b);
            G[b].push_back(a);
        }
        find_bcc();
        printf("%d %d\n",cnt1,sum);
    }
    return  0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值