This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 1e5 + 10;
int n, m;
vector <int> e[N], g[N];
vector <int> stk;
int num[N], low[N], ti, cnt, comp[N], a[N], sz[N];
long long ans;
void dfs(int u, int p) {
num[u] = low[u] = ++ti;
stk.push_back(u);
for (auto v: g[u]) {
if (v == p) continue;
if (!num[v]) {
dfs(v, u);
low[u] = min(low[u], low[v]);
} else low[u] = min(low[u], num[v]);
}
if (low[u] != num[u]) return;
++cnt;
while (1) {
int v = stk.back();
stk.pop_back();
comp[v] = cnt;
++a[cnt];
if (u == v) break;
}
}
void solve(int u, int p) {
sz[u] = a[u];
for (auto v: e[u]) {
if (v == p) continue;
solve(v, u);
sz[u] += sz[v];
}
int cur = n - sz[u];
for (auto v: e[u]) {
if (v == p) continue;
ans += 2LL * sz[v] * cur * a[u];
cur += sz[v];
}
ans += 2LL * (n - sz[u]) * (a[u] - 1) * (a[u] - 1);
for (auto v: e[u]) {
if (v == p) continue;
ans += 2LL * sz[v] * (a[u] - 1) * (a[u] - 1);
}
ans += a[u] * (a[u] - 1) * (a[u] - 2);
}
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
while (m--) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
for (int u = 1; u <= n; ++u) assert(num[u]);
for (int u = 1; u <= n; ++u)
for (auto v: g[u])
if (comp[u] != comp[v])
e[comp[u]].push_back(comp[v]);
solve(1, 0);
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |