#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int M = 5e5 + 10;
int n, m;
struct edge {
int to, nxt;
} E[M << 1];
int head[N], s;
void add_edge(int x, int y) {
E[++s] = (edge) {
y, head[x]
};
head[x] = s;
}
bool flag[N];
int dfn[N], cnt;
int low[N];
long long ans[N];
int sum[N];
void tarjan(int x) {
dfn[x] = low[x] = ++cnt;
sum[x] = 1;
int child = 0, tot = 0;
for (int i = head[x]; i; i = E[i].nxt) {
int y = E[i].to;
// if(y==fa) continue;
if (!dfn[y]) {
tarjan(y);
sum[x] += sum[y];
if (low[y] >= dfn[x]) {
child++;
ans[x] += 1ll * sum[y] * (n - sum[y]);
tot += sum[y];
}
low[x] = min(low[x], low[y]);
} else
low[x] = min(low[x], dfn[y]);
}
if (child >= 2 || x != 1)
ans[x] += 1ll * (tot + 1) * (n - tot - 1) + (n - 1);
else
ans[x] = n - 1 << 1;
}
int main() {
scanf("%d%d", &n, &m);
while (m--) {
int x, y;
scanf("%d%d", &x, &y);
add_edge(x, y);
add_edge(y, x);
}
tarjan(1);
for (int i = 1; i <= n; i++)
printf("%lld\n", ans[i]);
return 0;
}
一本通1526:Blockade
最新推荐文章于 2024-08-30 17:17:35 发布
这篇文章展示了如何用C++实现tarjan算法来解决图中的割点问题,通过构建边结构和深度优先搜索,计算每个节点在连通分量中的贡献值。
摘要由CSDN通过智能技术生成