[BZOJ1123][POI2008]BLO:Tarjan

分析:

找出图中的v-DCC,在搜索树上记录子树的siz,乘法原理即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN=100005;
const int MAXM=500005;
inline LL read(){
    LL x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,ecnt,tot,head[MAXN],dfn[MAXN],low[MAXN],siz[MAXN];
LL ans[MAXN];
bool cut[MAXN];
struct Edge{
    int to,nxt;
}e[MAXM<<2];
inline void add_edge(int bg,int ed){
    ecnt++;
    e[ecnt].to=ed;
    e[ecnt].nxt=head[bg];
    head[bg]=ecnt;
}
void tarjan(int x){
    dfn[x]=low[x]=++tot,siz[x]=1;int cnt=0,sum=0;
    for(int i=head[x];i;i=e[i].nxt){
        int ver=e[i].to;
        if(!dfn[ver]){
            tarjan(ver);
            low[x]=min(low[x],low[ver]);
            siz[x]+=siz[ver];
            if(low[ver]>=dfn[x]){
                ++cnt,ans[x]+=1ll*siz[ver]*(n-siz[ver]),sum+=siz[ver];
                if(x!=1||cnt>1) cut[x]=1;
            }
        }
        else low[x]=min(low[x],dfn[ver]);
    }
    if(cut[x]) ans[x]+=1ll*(n-sum-1)*(sum+1)+(n-1);
    else ans[x]=(n-1)*2;
}
int main(){
    n=read(),m=read();
    for(int i=1;i<=m;i++){
        int u=read(),v=read();
        add_edge(u,v);
        add_edge(v,u);
    }
    tarjan(1);
    for(int i=1;i<=n;i++) printf("%lld\n",ans[i]);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/ErkkiErkko/p/9496232.html