HDU 2767 Proving Equivalences (強聯通)

來源:互聯網
上載者:User

標籤:

大意:至少加幾條邊讓圖聯通。


思路:讓圖聯通的話肯定會與度數有關。所以找到入出度最大的補上即可。


#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 nv = 21000;const int ne = 51000;struct node{    int to,next;}q[ne<<1];int head[ne<<1],dfn[nv],num[nv],in[nv],stk[nv],low[nv];int cnt,top,tim,scc,out[nv],n,mi;bool vis[nv];void Add(int a,int b){    q[cnt].to = b;    q[cnt].next = head[a];    head[a] = cnt++;}void Tarjan(int u){    dfn[u] = low[u] = tim++;    stk[top++] = u;    vis[u] = 1;    for(int i = head[u];~i;i=q[i].next){        int v = q[i].to;        if(!dfn[v]){            Tarjan(v);            low[u] = min(low[u],low[v]);        }        else if(vis[v]){            low[u] = min(low[u],dfn[v]);        }    }    if(dfn[u]== low[u]){        scc++;        while(top>0&&stk[top] != u){            int now = stk[--top];            num[now] = scc;            vis[now] = 0 ;        }    }}void init(){    scc =  cnt = top = 0;    tim =  1;    memset(head,-1,sizeof(head));    memset(in,0,sizeof(in));    memset(out,0,sizeof(out));    memset(dfn,0,sizeof(dfn));    memset(low,0,sizeof(low));    memset(vis,false,sizeof(vis));    memset(num,0,sizeof(num));}int main(){    int n,m,i,j,k,cla,a,b;    scanf("%d",&cla);    while( cla-- ){        init();        scanf("%d%d",&n,&m);        for(i = 0;i < m;++ i){            scanf("%d%d",&a,&b);            Add(a,b);        }        for(i = 1;i <= n;++ i)            if(!dfn[i])                Tarjan(i);        if(scc==1){            puts("0");continue;        }        for(i = 1;i <= n;++ i){            for(j = head[i] ;~j;j = q[j].next){                int v = q[j].to;                if(num[i]!=num[v]){                    out[num[i]]++;                    in[num[v] ]++;                }            }        }        int s2 = 0,s1 = 0;        for(i = 1;i <= scc;++ i ){            if(!in[i])                s1++;            if(!out[i])                s2++;        }        printf("%d\n",max(s1,s2));    }    return 0;}


HDU 2767 Proving Equivalences (強聯通)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.