HDU 3394 Railway (點雙聯通+圈內判邊數)

來源:互聯網
上載者:User

標籤:

大意:給定M條邊,問有有多少邊是不在環上(或環內)的,有多少邊是有衝突的(什麼是衝突?即在一個環內有多條邊將環分割開,即這樣的邊+上環上邊的總數)


思路:求橋的個數比較容易處理,直接(low[v]>dfn[u]即可)主要是怎麼找衝突邊,我們知道他們一定在一個聯通分量內,所以我們將求出的 一組聯通分量拿出來,進行遍曆看是否所有的連的邊在當前的棧中,在的話邊數++,因為是無向圖,所以最後要除二與聯通分量的點相比較即可。



前向星:


#include<map>#include<queue>#include<cmath>#include<cstdio>#include<stack>#include<iostream>#include<cstring>#include<algorithm>#define LL int#define inf 0x3f3f3f3f#define eps 1e-8#include<vector>#define ls l,mid,rt<<1#define rs mid+1,r,rt<<1|1using namespace std;const int Ma = 21000;struct node{    int to,next;}q[Ma*10];//注意看題目邊的數目*2int 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;}



HDU 3394 Railway (點雙聯通+圈內判邊數)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.