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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| #include <cstdio> #include <vector> #define pb push_back using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0'; flag && (x = ~x + 1); } template <class Type, class ...rest> void read (Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write (Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar('0' + x % 10); } typedef long long LL; const int N = 1e5 + 10; LL ans; int top, stk[N]; int stmp, cnt, dfn[N], low[N]; int n, m, sz, tot, w[N << 1], f[N << 1]; vector <int> g[N], e[N << 1]; void tarjan (int u) { ++sz; stk[++top] = u; low[u] = dfn[u] = ++stmp; for (int v : g[u]) if (!dfn[v]) { tarjan(v); low[u] = min(low[u], low[v]); if (low[v] == dfn[u]) { w[++tot] = 0; int x; do { x = stk[top--]; e[tot].pb(x), e[x].pb(tot); ++w[tot]; } while (x ^ v); e[tot].pb(u), e[u].pb(tot), ++w[tot]; } } else low[u] = min(low[u], dfn[v]); } void dfs (int u, int fa) { f[u] = u <= n; for (int v : e[u]) if (v ^ fa) { dfs(v, u); ans += 2ll * w[u] * f[u] * f[v]; f[u] += f[v]; } ans += 2ll * w[u] * f[u] * (sz - f[u]); } int main () { read(n, m); for (int i = 1; i <= n; ++i) w[i] = -1; tot = n; for (int a, b; m; --m) read(a, b), g[a].pb(b), g[b].pb(a); for (int i = 1; i <= n; ++i) if (!dfn[i]) { sz = 0; tarjan(i), --top; dfs(i, 0); } write(ans); return 0; }
|