码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj1123(割点加路径统计)

时间:2018-03-30 21:55:42      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:cstring   size   scanf   algorithm   ima   min   blog   def   png   

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace  std;

typedef long long ll;

struct my{
  int next;
  int v;
};

const int maxn=100000+10;
ll low[maxn],dfsn[maxn],size_[maxn],adj[maxn],fa,dfn,root,ans[maxn];
my bian[maxn*10];
bool cut[maxn];
int m,n;

void myinsert(int u,int v){
     bian[++fa].v=v;
     bian[fa].next=adj[u];
     adj[u]=fa;
}

void tarjan(ll x){
     dfsn[x]=low[x]=++dfn;
     ll ch=0,sum=0;
     size_[x]=1;
     for (int i=adj[x];i;i=bian[i].next){
        ll v=bian[i].v;
        if(!dfsn[v]){
            tarjan(v);
            size_[x]+=size_[v];
            low[x]=min(low[x],low[v]);
            if(dfsn[x]<=low[v]){
                ch++;
                ans[x]+=size_[v]*(n-size_[v]);
                sum+=size_[v];
                if(root!=x||ch>1){
                    cut[x]=1;
                }
            }
        }
        else low[x]=min(low[x],dfsn[v]);
     }
     if(cut[x]) ans[x]+=(n-sum-1)*(sum+1)+(n-1);
     else ans[x]=2*(n-1);
}

int main(){
    int u,v;
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;i++){
        scanf("%d%d",&u,&v);
        myinsert(u,v);
        myinsert(v,u);
    }
    for (int i=1;i<=n;i++){
        if(!dfsn[i]){
            root=i;
            tarjan(i);
        }
    }
    for (int i=1;i<=n;i++){
       printf("%lld\n",ans[i]);
    }
return 0;
}

 

技术分享图片技术分享图片

 

技术分享图片技术分享图片

 

bzoj1123(割点加路径统计)

标签:cstring   size   scanf   algorithm   ima   min   blog   def   png   

原文地址:https://www.cnblogs.com/lmjer/p/8678317.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!