//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")
#include <bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
template<typename T>
bool umin(T &a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool umax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#ifdef KIVI
#define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define DEBUG while (false)
#define LOG(...)
#endif
const int max_n = (1e5 + 11) * 2, inf = 1000111222;
vector <int> edge[max_n];
int tin[max_n], tout[max_n], used[max_n], timer = 0, art[max_n], id[max_n];
vector <int> q, current;
vector <vector <int> > cmp;
inline void dfs (int v, int p = -1) {
used[v] = 1;
tin[v] = timer++;
current.pb(v);
tout[v] = tin[v];
q.pb(v);
for (int to : edge[v]) {
if (to == p) {
continue;
}
if (used[to]) {
umin(tout[v], tin[to]);
}
else {
dfs(to, v);
umin(tout[v], tout[to]);
if (tout[to] >= tin[v]) {
art[v] = tin[v] > 0 || tin[to] > 1;
cmp.pb(vector <int>{v});
while (cmp.back().back() != to) {
cmp.back().pb(q.back());
q.pop_back();
}
}
}
}
}
vector <int> e[max_n];
int sz[max_n], n;
ll res[max_n];
ll ans = 0;
inline void calc (int v, int p = -1) {
ans += sz[v] * (sz[v] - 1ll) * (sz[v] - 2ll);
ans += sz[v] * (sz[v] - 1ll) * (len(current) - sz[v]) * 2ll;
ll cur = sz[v];
for (int to : e[v]) {
if (to == p) {
continue;
}
calc(to, v);
ans += cur * sz[to] * (sz[v] - cur) * 2ll;
sz[v] += sz[to];
}
ans += cur * (sz[v] - cur) * (len(current) - sz[v]) * 2ll;
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
int m;
cin >> n >> m;
for (int i = 0, a, b; i < m; i++) {
a = i + 1, b = i + 2;
cin >> a >> b;
--a, --b;
edge[a].pb(b);
edge[b].pb(a);
}
int new_id = 0;
for (int ver = 0; ver < n; ver++) {
if (used[ver]) {
continue;
}
current.clear();
dfs(ver);
for (int i : current) {
if (art[i]) {
id[i] = new_id++;
}
}
for (auto &i : cmp) {
int now = new_id++, cnt = 0;
for (int x : i) {
if (art[x]) {
e[id[x]].pb(now);
e[now].pb(id[x]);
// LOG(id[x], now);
}
else {
++cnt;
id[x] = now;
}
}
for (int x : i) {
if (art[x]) {
ans += cnt * (cnt - 1ll);
}
}
}
// LOG(new_id);
for (int i : current) {
++sz[id[i]];
}
calc(id[ver]);
}
cout << ans << '\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... |