HDU 3394 Railway (点双联通+圈内判边数)

大意:给定M条边,问有有多少边是不在环上(或环内)的,有多少边是有冲突的(什么是冲突?即在一个环内有多条边将环分割开,即这样的边+上环上边的总数)


思路:求桥的个数比较容易处理,直接(low[v]>dfn[u]即可)主要是怎么找冲突边,我们知道他们一定在一个联通分量内,所以我们将求出的 一组联通分量拿出来,进行遍历看是否所有的连的边在当前的栈中,在的话边数++,因为是无向图,所以最后要除二与联通分量的点相比较即可。



前向星:


#include
#include
#include
#include
#include
#include
#include
#include
#define LL int
#define inf 0x3f3f3f3f
#define eps 1e-8
#include
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1
using namespace std;
const int Ma = 21000;
struct node{
    int to,next;
}q[Ma*10];//注意看题目边的数目*2
int head[Ma*10],dfn[Ma],num[Ma],stk[Ma],du[Ma],low[Ma];
int cnt,top,tim,scc,sum,n,tmp[Ma],S,CNT,ans;
bool vis[Ma],bj[Ma];

void Add(int a,int b){
    q[cnt].to = b;
    q[cnt].next = head[a];
    head[a] = cnt++;
}

void init(){
    ans = CNT  = scc = cnt = top = 0;
    tim =  1;
    memset(head,-1,sizeof(head));
    for(int i = 0;i < n;++ i){
        num[i] = low[i]  = dfn[i] = 0;
        du[i] = vis[i]  = 0;
    }
}
void Judge(){
    int E = 0;
    for(int i = 0;i < S;++ i){
        int u = tmp[i];
        for(int j = head[u];~j;j=q[j].next){
            int v = q[j].to;
            if( bj[v] ){
                E++;

            }
        }
    }
    E /= 2;
    if(E > S)
        CNT += E;
}

void Tarjan(int u,int To){
    low[u] = dfn[u] = tim++;
    vis[u] = true;
    stk[top++] = u;

    for(int i = head[u]; ~i ; i = q[i].next){
        int v = q[i].to;
        if(i == (To^1)) continue;
        if(!vis[v]){
            Tarjan(v,i);
            low[u] = min(low[u],low[v]);
            if(low[v]>dfn[u])
                ans++;
            if(low[v] >= dfn[u]){
                memset(bj,false,sizeof(bj));
                S = 0;
                stk[top] = -1;
                tmp[S++] = u;bj[u] = true;
                while(stk[top] != v){
                    int now = stk[--top];
                    tmp[S++] = now;
                    bj[now] = true;
                }
                Judge();
            }
        }
        else
            low[u] = min(low[u],dfn[v]);
    }
}
int main(){
    int m,i,j,k,a,b;
    while(~scanf("%d%d",&n,&m)){
        if(!n&&!m) break;
        init();
        for(i = 0;i < m;++ i){
            scanf("%d%d",&a,&b);
            Add(a,b);Add(b,a);
        }

        for(i = 0;i < n;++ i)
            if(!dfn[i])
                Tarjan(i,-1);
        printf("%d %d\n",ans,CNT);
    }
    return 0;
}



你可能感兴趣的:(模板,连通分量)