51nod 2877熟练使用tarjan的知识

在这里插入图片描述
我一开始想到tarjan 没想到咋做 看了题解直呼妙阿
考虑一个点有多个子块 若子块中的点不能回到父节点 那么这个子块 就和 其他子块是不能互相到达的
这时候只需要记录 已经遍历的多少子块即可 这样不会重复

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f3f3f3f3f

using namespace std;

const int N = 1e6 + 10,M = 5e5 + 10;
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;

int head[N],to[M * 2],last[M * 2],cnt;
void add(int a,int b){
    to[++cnt] = b;
    last[cnt] = head[a];
    head[a] = cnt;
}

int n,m;
ll dfn[N],low[N],times,sz[N],pn; ll ans[N];
void tarjan(int x){
    dfn[x] = low[x] = ++times;
    sz[x] = 1;
    ll pn = 0;
    for(int i = head[x]; i != -1; i = last[i]){
        int j = to[i];
        if(!dfn[j]){
            tarjan(j);
            sz[x] += sz[j];
            if(dfn[x] <= low[j]){
                ans[x] += sz[j] * pn;
                pn += sz[j];
            }
            low[x] = min(low[x],low[j]);
        }else low[x] = min(low[x],dfn[j]);
    }

    ans[x] += pn * (n - pn - 1);
}

int main(){
    memset(head,-1,sizeof head);
    cin >> n >> m;
    for(int i = 1; i <= m; i++){
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);
        add(y,x);
    }

    tarjan(1);

    for(int i = 1; i <= n; i++){
        cout << (ans[i] + n - 1) * 2 << endl;
    }



    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值