#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) ((int)a.size())
#ifdef Doludu
template <typename T>
ostream& operator << (ostream &o, vector <T> vec) {
o << "{"; int f = 0;
for (T i : vec) o << (f++ ? " " : "") << i;
return o << "}";
}
void bug__(int c, auto ...a) {
cerr << "\e[1;" << c << "m";
(..., (cerr << a << " "));
cerr << "\e[0m" << endl;
}
#define bug_(c, x...) bug__(c, __LINE__, "[" + string(#x) + "]", x)
#define bug(x...) bug_(32, x)
#define bugv(x...) bug_(36, vector(x))
#define safe bug_(33, "safe")
#else
#define bug(x...) void(0)
#define bugv(x...) void(0)
#define safe void(0)
#endif
const int mod = 998244353, N = 505;
struct BCC { // 0-based, remember to build
int n, nbcc; // note for isolated point
vector <vector <int>> g, _g; // id >= n: bcc
vector <int> pa, dep, low, stk, pa2, dep2, s, r;
void dfs(int v, int p) {
dep[v] = low[v] = ~p ? dep[p] + 1 : 0;
stk.pb(v), pa[v] = p;
for (int u : g[v]) if (u != p) {
if (low[u] == -1) {
dfs(u, v), low[v] = min(low[v], low[u]);
if (low[u] >= dep[v]) {
int id = nbcc++, x;
do {
x = stk.back(), stk.pop_back();
_g[id + n].pb(x), _g[x].pb(id + n);
} while (x != u);
_g[id + n].pb(v), _g[v].pb(id + n);
}
} else low[v] = min(low[v], dep[u]);
}
}
bool is_cut(int x) { return sz(_g[x]) != 1; }
vector <int> bcc(int id) { return _g[id + n]; }
int bcc_id(int u, int v) {
return pa2[dep2[u] < dep2[v] ? v : u] - n; }
void dfs2(int v, int p, int rr) {
dep2[v] = ~p ? dep2[p] + 1 : 0, pa2[v] = p;
s[v] = v < n, r[v] = rr;
for (int u : _g[v]) if (u != p) dfs2(u, v, rr), s[v] += s[u];
}
ll build() {
low.assign(n, -1);
for (int i = 0; i < n; ++i) if (low[i] == -1)
dfs(i, -1), dfs2(i, -1, i);
ll ans = 0;
for (int i = 0; i < n + nbcc; ++i) {
int x = i < n ? -1 : sz(_g[i]);
ll base = 1ll * n * (n - 1) / 2;
for (int j : _g[i]) if (j != pa2[i]) {
base -= 1ll * s[j] * (s[j] - 1) / 2;
}
if (i != r[i]) {
base -= 1ll * (s[r[i]] - s[i]) * (s[r[i]] - s[i] - 1) / 2;
}
ans += x * base;
}
return ans;
}
void add_edge(int u, int v) {
g[u].pb(v), g[v].pb(u); }
BCC (int _n) : n(_n), nbcc(0), g(n), _g(2 * n),
pa(n), dep(n), low(n), stk(), pa2(n * 2),
dep2(n * 2), s(n * 2), r(n * 2) {}
};
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int n, m;
cin >> n >> m;
BCC bcc(n);
for (int i = 0, u, v; i < m; ++i) {
cin >> u >> v, --u, --v;
bcc.add_edge(u, v);
}
cout << bcc.build() * 2 << "\n";
}
| # | 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... |