#include <bits/stdc++.h>
using namespace std;
#ifdef KURUMI
#include "algo/debug.h"
#endif
#define endl '\n'
#define fi first
#define se second
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()
#define filter(v) v.resize(unique(all(v)) - v.begin())
#define dbg(x) "[" #x << " = " << x << "]"
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T1, typename T2> T2 rand(T1 l, T2 r) {
return uniform_int_distribution<T2>(l, r)(rng);
}
template<typename T1, typename T2> T2 wrand(T1 l, T2 r, int seed) {
if(seed == 0) return rand(l, r);
else return (seed > 0 ? max(rand(l, r), wrand(l, r, seed - 1)) : min(rand(l, r), wrand(l, r, seed + 1)));
}
template<typename S, typename T> bool maximize(S &a, T b) {
if(a < b) {
a = b;
return true;
}else return false;
}
template<typename S, typename T> bool minimize(S &a, T b) {
if(a > b) {
a = b;
return true;
}else return false;
}
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tp3;
const int N = 4e5 + 3;
int n, m;
int eu[N], ev[N];
vector<int> adj[N];
void input() {
cin >> n >> m;
for(int i = 1; i <= m; i++) {
int u, v; cin >> u >> v;
eu[i] = u;
ev[i] = v;
adj[u].push_back(i);
adj[v].push_back(i);
}
}
int timer, square;
int num[N], low[N], wgh[N];
vector<int> rsq[N];
stack<int> sta;
void dfs(int u, int p) {
num[u] = low[u] = ++timer;
sta.push(u);
for(int id : adj[u]) if(id != p) {
int v = eu[id] ^ ev[id] ^ u;
if(num[v]) minimize(low[u], num[v]);
else {
dfs(v, id);
minimize(low[u], low[v]);
if(low[v] >= num[u]) {
square++;
wgh[square + n] = 0;
while(int c = sta.top()) {
rsq[square + n].push_back(c);
rsq[c].push_back(square + n);
// cerr << square + n << " " << c << endl;
wgh[square + n]++;
sta.pop();
if(c == v) break;
}
rsq[square + n].push_back(u);
rsq[u].push_back(square + n);
wgh[square + n]++;
// cerr << square + n << " " << u << endl;
}
}
}
}
int siz[N];
ll answer = 0;
void work(int u, int p) {
// cerr << dbg(u) << endl;
siz[u] = (u <= n);
for(int v : rsq[u]) if(v != p) {
work(v, u);
answer += wgh[u] * siz[u] * siz[v];
// cerr << dbg(wgh[u]) << dbg(siz[u] * siz[v]) << endl;
siz[u] += siz[v];
}
answer += wgh[u] * (n - siz[u]) * siz[u];
// cerr << dbg(wgh[u]) << dbg((n - siz[u]) * siz[u]) << endl;
}
void process() {
fill(wgh + 1, wgh + 1 + n, -1);
for(int i = 1; i <= n; i++) {
if(!num[i]) {
dfs(i, -1);
work(i, -1);
}
}
cout << answer * 2 << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#define task "DATE"
if(fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int testcase = 1; // cin >> testcase;
for(int i = 1; i <= testcase; i++) {
input();
process();
}
cerr << "Saa, watashtachi no deeto hajimemashou" << endl;
cerr << "Atarashii kiseki wo koko kara hajimeru shining place nee mou ichido kimi to..." << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:138:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
count_triplets.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |