【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;
}
隐约雷鸣,阴霾天空,但盼风雨来,能留你在此。
隐约雷鸣,阴霾天空,即使天无雨,我亦留此地。
![](https://images.cnblogs.com/cnblogs_com/MisakaAzusa/1169114/o_a0c3752e09f79052b15f108c0cf3d7ca7acbd5b0.gif)