天天看點

hdu 3749 點雙連通分量

留個模闆(求所有點雙)

求所有點雙時開個棧維護。注意求點雙時記錄一下每個子樹開始的位置,如果這個點是對于這個子樹的割點,那麼點雙是這個子樹在棧中剩下的點+這個點本身。

#include <bits/stdc++.h>
using namespace std;
#define N 5100
#define M 21000
int n,m,Q,tot,cnt,tim,Case,top,scc;
int head[N],nex[M],to[M],st[N],tmp[N],num[N];
int deep[N],dfn[N],low[N],fa[N],bel[N];
vector<int>vec[N];
void init()
{
    tot=;cnt=;tim=;top=;scc=;
    memset(head,,sizeof(head));
    memset(dfn,,sizeof(dfn));
    memset(low,,sizeof(low));
    memset(bel,,sizeof(bel));
    for(int i=;i<=n;i++)vec[i].clear();
}
void add(int x,int y)
{
    tot++;
    nex[tot]=head[x];head[x]=tot;
    to[tot]=y;  
}
void tarjan(int x,int y)
{
    deep[x]=deep[y]+;fa[x]=y;
    dfn[x]=low[x]=++cnt;
    bel[x]=tim;st[++top]=x;
    for(int i=head[x];i;i=nex[i])
        if(to[i]!=y)
        {
            if(!dfn[to[i]])
            {
                int pre=top;
                tarjan(to[i],x);low[x]=min(low[x],low[to[i]]);
                if(low[to[i]]>=dfn[x])
                {
                    scc++;int t=;
                    while(top!=pre)
                        tmp[++t]=st[top--];
                    tmp[++t]=x;num[scc]=t;
                    for(int j=;j<=t;j++)
                        vec[tmp[j]].push_back(scc);
                }
            }
            else low[x]=min(low[x],dfn[to[i]]);
        }
}
int main()
{
    //freopen("tt.in","r",stdin);
    while(scanf("%d%d%d",&n,&m,&Q)&&n)
    {
        init();
        printf("Case %d:\n",++Case);
        for(int i=,x,y;i<=m;i++)
        {
            scanf("%d%d",&x,&y);x++;y++;
            add(x,y);add(y,x);
        }
        for(int i=;i<=n;i++)
            if(!dfn[i])tim++,tarjan(i,);
        for(int i=;i<=n;i++)
            sort(vec[i].begin(),vec[i].end());
        for(int x,y;Q--;)
        {
            scanf("%d%d",&x,&y);x++;y++;
            if(bel[x]!=bel[y]){puts("zero");continue;}
            int sz1=vec[x].size(),sz2=vec[y].size(),ans=;
            for(int i=,j=;i<sz1;i++)
            {
                while(j<sz2&&vec[y][j]<vec[x][i])j++;
                if(j<sz2&&vec[y][j]==vec[x][i])ans=vec[x][i];
            }
            if(!ans)puts("one");
            else if(num[ans]==)puts("one");
            else puts("two or more");
        }
    }
    return ;
}