HDU 3390 Railway 边双连通分量

题目描述:

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 100 11 22 33 03 44 55 66 77 45 70 0

Output:

1 5

  由题目意思,图中的桥是无用的,那些双连通分量中的边多于节点多,就全是冲突边。

/*
 * @author ipqhjjybj
 * @date  20130622
 */
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

#include <iostream>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <utility>

#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <string>
#include <memory.h>
using namespace std;

#define inf 0x3f3f3f3f
#define MAXN 10007
#define MAXM 100010
#define clr(x,k) memset((x),(k),sizeof(x))
#define cpy(x,k) memcpy((x),(k),sizeof(x))
#define Base 10000

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
struct Edge{
    int st,ed;
    Edge(){};
    Edge(int a,int b){
        st = a,ed = b;
    }
};
int n,m;
vector<int> viMap[MAXN];
vector<Edge> block[MAXN];
stack<Edge> sEtack;
int low[MAXN],dfn[MAXN],vs[MAXN];
int tcc,sig;
int ans1,ans2;
void init(){
    int a,b;
    for(int i = 0;i <= n;i++) viMap[i].clear(),block[i].clear();
    for(int i = 0;i < m;i++){
        scanf("%d %d",&a,&b);
        viMap[a].push_back(b);
        viMap[b].push_back(a);
    }
    clr(low,0);
    clr(dfn,0);
    ans1 = ans2 = sig = tcc = 0;
    while(!sEtack.empty()) sEtack.pop();
}
int Tarjan(int cur,int &sig,int &tcc,int from){
    low[cur] = dfn[cur]=++sig;
    for(vector<int>::iterator it = viMap[cur].begin();it != viMap[cur].end();it++){
            if(!dfn[*it]){
                sEtack.push(Edge(cur,*it));
                Tarjan(*it,sig,tcc,cur);
                low[cur]=min(low[cur],low[*it]);
                if(dfn[cur]<=low[*it]){   // cur<*it 是一个桥, 当dfn[cur]==low[*it]时,说明这是个连通的回路
                    for(Edge temp;!sEtack.empty();){   //sEtack.empty() 栈不可能为空。理论上不会用到
                        temp = sEtack.top();
                        if(dfn[temp.st]<dfn[*it]) break; //说明temp.st在*it之前被遍历到,放进栈中的是那些与由*it查询到的边,即*it的子树(子树中的那些连通向量在之前已在搜索中被排除)
                                                // 也为了防止重复压栈
                        block[tcc].push_back(temp);sEtack.pop();
                    }
                    block[tcc++].push_back(Edge(cur,*it));
                    sEtack.pop();
                    if(dfn[cur]<low[*it]) ans1++; // (cur,*it) 是一个桥,桥是无用的,记录
                }
            }else if(*it != from && dfn[*it]<dfn[cur]){  //因为建立在深搜之上
                low[cur] = min(low[cur],dfn[*it]);
                sEtack.push(Edge(cur,*it));
            }
    }
    return 0;
}
int main(){
    //freopen("3394.in","r",stdin);
    while(scanf("%d %d",&n,&m)){
        if(!n&&!m)
            break;
        init();
        for(int i = 0;i < n;i++)
            if(!dfn[i])
                Tarjan(i,sig,tcc,-1);
        for(int i = 0;i < tcc;i++){
            clr(vs,0);
            int temp=0;
            for(int j = 0;j < block[i].size();j++){
                if(!vs[block[i][j].st]) vs[block[i][j].st]=1,temp++;
                if(!vs[block[i][j].ed]) vs[block[i][j].ed]=1,temp++;
            }
            if(temp<block[i].size()) ans2+=block[i].size();
        }
        printf("%d %d\n",ans1,ans2);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值