雙聯通模板

來源:互聯網
上載者:User

標籤:ack   init   set   ini   col   pre   puts   std   const   

點雙聯通模板——

hdu 3749

#define Troy 10/18/2017#include <bits/stdc++.h>using namespace std;inline int read(void){    int s=0,k=1;char ch=getchar();    while(ch<‘0‘|ch>‘9‘)    ch==‘-‘?k=-1:0,ch=getchar();    while(ch>47&ch<=‘9‘)    s=s*10+(ch^48),ch=getchar();    return s*k;}const int N=2e4+5;int n,m,q;vector<int> bel[N],bcc[N];struct edges{    int v;edges *last;}edge[N<<1],*head[N>>1];int cnt;inline void push(int u,int v){    edge[++cnt]=(edges){v,head[u]};head[u]=edge+cnt;}int dfn[N],low[N],dfx,stk_u[N],stk_v[N],top,T,size[N],K[N],root,scc,bccno[N];inline void unionn(int x,int y){    bcc[y].push_back(x);    bel[x].push_back(y);    bccno[x]=y;}inline void Tarjan(int x,int fa){    dfn[x]=low[x]=++dfx;    K[x]=T;    for(edges *i=head[x];i;i=i->last)   if(i->v!=fa){        if(!dfn[i->v]){                    stk_u[++top]=x; stk_v[top]=i->v;            Tarjan(i->v,x);            low[x]=min(low[x],low[i->v]);            if(low[i->v]>=dfn[x]){                scc++;                bcc[scc].clear();                int u,v;                while(1){ u=stk_u[top],v=stk_v[top--];                    if(bccno[u]!=scc)   unionn(u,scc);                    if(bccno[v]!=scc)   unionn(v,scc);                    if(u==x&&v==i->v) break;                }            }        }else   if(dfn[i->v]<dfn[x]){                        stk_u[++top]=x; stk_v[top]=i->v;            low[x]=min(low[x],dfn[i->v]);        }    }}inline void init(){    cnt=0;memset(head,0,sizeof(head));    memset(dfn,0,sizeof(dfn));    memset(bccno,0,sizeof(bccno));    for(int i=0;i<=n;i++)        bel[i].clear();    for(int i=1,u,v;i<=m;i++){        u=read(),v=read();        push(u,v);push(v,u);    }}inline void Judge(int a,int b){    for(int i=0;i<bel[a].size();i++)            for(int j=0;j<bel[b].size();j++)            if(bel[a][i]==bel[b][j]&&bcc[bel[a][i]].size()>2){                puts("two or more");                return ;            }    puts("one");}inline void work(){    dfx=0;scc=0;    for(int i=0;i<n;i++)        if(!dfn[i]){            T++;root=i;            Tarjan(i,-1);        }    while(q--){        int a=read(),b=read();        if(K[a]!=K[b])puts("zero");        else            Judge(a,b);    }}int main(){    int t=0;    while(1){        n=read(),m=read(),q=read();        if(n==0&&m==0&&q==0)    break;        t++;        printf("Case %d:\n",t);        init();        work();    }    return 0;}

 

邊雙聯通分量(明日更新

雙聯通模板

相關文章

聯繫我們

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