bzoj 2730 [HNOI2012]矿场搭建 点双连通分量

如果只有一个点双,答案为2,随便选两个点。
否则求每个点双里有几个割点,如果割点个数大于等于2,那么这个点双中不用选点,如果个数为1,那么点双中需要选一个不是割点的点。

#include <bits/stdc++.h>
using namespace std;
#define N 510
#define ll long long
int m,tot,cnt,cr,top,ans,Case,scc;
int head[N],nex[N<<1],to[N<<1];
int dfn[N],low[N],iscut[N],st[N];
vector<int>vec[N];
ll num;
void init()
{
    tot=0;cnt=0;scc=0;cr=0;top=0;ans=0;num=1;
    memset(head,0,sizeof(head));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(iscut,0,sizeof(iscut));
}
void add(int x,int y)
{
    tot++;
    nex[tot]=head[x];head[x]=tot;
    to[tot]=y;
}
void tarjan(int x,int y)
{
    dfn[x]=low[x]=++cnt;st[++top]=x;
    for(int i=head[x];i;i=nex[i])
        if(to[i]!=y)
        {
            if(!dfn[to[i]])
            {
                int beg=top;tarjan(to[i],x);
                low[x]=min(low[x],low[to[i]]);
                if(low[to[i]]>=dfn[x])
                {
                    vec[++scc].clear();
                    if(x==1)cr++;
                    else iscut[x]=1;
                    while(top!=beg)vec[scc].push_back(st[top--]);
                    vec[scc].push_back(x);
                }   
            }
            else low[x]=min(low[x],dfn[to[i]]);
        }
}
int main()
{
    //freopen("tt.in","r",stdin);
    while(scanf("%d",&m)&&m)
    {
        init();
        for(int i=1,x,y;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            add(x,y);add(y,x);
        }
        tarjan(1,0);
        iscut[1]=(cr>=2);
        for(int i=1;i<=scc;i++)
        {
            int sz=vec[i].size(),du=0;
            for(int j=0;j<sz;j++)
                if(iscut[vec[i][j]])du++;
            if(du==0)ans+=2,num*=sz*(sz-1)/2;
            if(du==1)ans++,num*=sz-1;
        }
        printf("Case %d: %d %lld\n",++Case,ans,num);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值