【luogu P3469 [POI2008]BLO-Blockade】 题解

题目链接:https://www.luogu.org/problemnew/show/P3469

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
const int maxn = 500000 + 10;
ll n, m, ans[maxn];
ll dfn[maxn], low[maxn], tim, size[maxn];
bool iscut[maxn];
struct edge{
    ll from, to, next;
}e[maxn<<2];
ll head[maxn], cnt;
void add(ll u, ll v)
{
    e[++cnt].from = u;
    e[cnt].next = head[u];
    e[cnt].to = v;
    head[u] = cnt;
}
void tarjan(ll x, ll fa)
{
    ll child = 0, sum = 0;
    dfn[x] = low[x] = ++tim;
    size[x] = 1;
    for(ll i = head[x]; i != -1; i = e[i].next)
    {
        ll v = e[i].to;
        if(!dfn[v])
        {
            tarjan(v,fa);
            size[x] += size[v];
            low[x] = min(low[x], low[v]);
            if(low[v] >= dfn[x]) 
            {
                ans[x] += size[v]*(n - size[v]);
                sum += size[v];
                child++;
                if(x != fa)
                iscut[x] = 1;
            }
            if(x == fa) child++;
        }
        low[x] = min(low[x], dfn[v]);
    }
    if(x == fa && child >= 2) iscut[fa] = 1;
    if(!iscut[x]) ans[x] = 2*(n-1);
    else ans[x] += (n-sum-1)*(sum+1)+n-1;
}
int main()
{
    memset(head, -1, sizeof(head));
    scanf("%lld%lld",&n,&m);
    for(ll i = 1; i <= m; i++)
    {
        ll u, v;
        scanf("%lld%lld",&u,&v);
        add(u,v),add(v,u);
    }
    tarjan(1,1);
    for(ll i = 1; i <= n; i++)
    printf("%lld\n",ans[i]);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/MisakaAzusa/p/9415929.html