#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _ <<' '<<
#define print(x) cout<<#x<<": "<<(x)<<'\n'
ll n,m,c=-1,g,b,ans=0;
vector<vector<ll>> adj, bcc;
vector<ll> dp, num, low, art;
stack<ll> s;
void dfs(const int u, const int p) {
num[u]=low[u]=++c;
s.push(u);
++g;
for (const auto v:adj[u]) {
if (v==p) continue;
if (num[v]==-1) {
dfs(v, u);
if (low[v]>=num[u]) {
art[u]=1;
bcc[u].push_back(n+b);
while (bcc[n+b].empty() || bcc[n+b].back()!=v) {
bcc[n+b].push_back(s.top());
s.pop();
}
++b;
}
}
}
}
void dfs2(const int u) {
dp[u]=u<n;
for (const auto v:bcc[u]) {
dfs2(v);
dp[u]+=dp[v];
if (u>=n) ans-=bcc[u].size()*dp[v]*(dp[v]-1);
}
if (u>=n) ans-=bcc[u].size()*(g-dp[u])*(g-1-dp[u]);
}
void tj() {
for (int u=0; u<n; u++) if (num[u]==-1) {
s={};
s.push(u);
int d=0;
b=0;
g=1;
for (const auto v:adj[u]) {
if (num[v]==-1) {
dfs(v, u);
++d;
bcc[u].push_back(n+b);
while (bcc[n+b].empty() || bcc[n+b].back()!=v) {
bcc[n+b].push_back(s.top());
s.pop();
}
++b;
}
}
if (d>1) art[u]=1;
ans+=g*(g-1)*(g-2);
dfs2(u);
}
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cout.tie(0);
cin>>n>>m;
adj.resize(n);
bcc.resize(n<<2);
dp.assign(n<<2, 0);
for (int i=0; i<m; i++) {
int a,b;
cin>>a>>b;
adj[--a].push_back(--b);
adj[b].push_back(a);
}
num.resize(n, -1);
low=num;
art.assign(n, 0);
tj();
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... |