BZOJ 1123: [POI2008]BLO tarjan求割点

本文介绍了一种解决时空隧道问题的方法,通过求割点来分析图中的关键节点,并利用乘法原理计算答案。提供了完整的C++代码实现,可用于解决类似问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

时空隧道


仔细观察样例会发现其实(x,y)和(y,x)是不同的…


求割点…然后直接根据乘法原理计算答案就好了…


代码如下:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
using namespace std;
const int maxn=100000+5,maxm=500000+5;
int n,m,hd[maxn],to[maxm*2],nxt[maxm*2],cnt,dfn[maxn],low[maxn*2],tim;
long long ans[maxn];
inline void add(int x,int y){
    to[cnt]=y;nxt[cnt]=hd[x];hd[x]=cnt++;
}
inline long long tarjan(int root,int f){
    dfn[root]=low[root]=++tim;long long tmp=0,sum=0;ans[root]=(n-1)<<1;int cnt=0,siz;
    for(int i=hd[root];i!=-1;i=nxt[i])
        if(to[i]!=f){
            if(!dfn[to[i]]){
                siz=tarjan(to[i],root);low[root]=min(low[root],low[to[i]]);
                if(low[to[i]]>=dfn[root])
                    ans[root]+=2LL*siz*sum,sum+=siz;
                else
                    tmp+=siz;
            }
            else
                low[root]=min(low[root],dfn[to[i]]);
        }
    ans[root]+=2LL*(n-(sum+1))*sum;
    return sum+tmp+1;
}
signed main(void){
    tim=cnt=0;
    scanf("%d%d",&n,&m);
    memset(hd,-1,sizeof(hd));
    memset(dfn,0,sizeof(dfn));
    for(int i=1,x,y;i<=m;i++)
        scanf("%d%d",&x,&y),add(x,y),add(y,x);
    tarjan(1,-1);
    for(int i=1;i<=n;i++)
        printf("%lld\n",ans[i]);
    return 0;   
}

by >_< NeighThorn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值