1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #define ll long long using namespace std; const int maxn = 1e5 + 10; const int maxm = 5e5 + 10;
int head[maxn], ver[maxm << 1], next1[maxm << 1]; int dfn[maxn], low[maxn], Size[maxn]; ll ans[maxn]; bool cut[maxn]; int n, m, tot, num;
void add(int x,int y) { ver[++tot] = y; next1[tot] = head[x]; head[x] = tot; }
void tarjan(int x) { dfn[x] = low[x] = ++num; Size[x] = 1; int flag = 0, sum = 0; for (int i = head[x]; i; i = next1[i]) { int y = ver[i]; if (!dfn[y]) { tarjan(y); low[x] = min(low[x],low[y]); Size[x] += Size[y]; if (low[y] >= dfn[x]) { flag++; ans[x] += (ll)Size[y] * (n - Size[y]); sum += Size[y]; if (x != 1 || flag > 1) cut[x] = 1; } } else low[x] = min(low[x],dfn[y]); } if (cut[x]) ans[x] += (ll)(n - 1 - sum) * (sum + 1) + (n - 1); else ans[x] = 2 * (n - 1); }
int main() {
cin >> n >> m; tot = 1; for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d",&x,&y); if (x == y) continue; add(x, y); add(y,x); } tarjan(1); for (int i = 1; i <= n; i++) printf("%lld\n",ans[i]); return 0; }
|