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>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define rfr(i, n, m) for(int i = (n); i >= (m); i --)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
#include <time.h>
#include <cmath>
#include <deque>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int i_inf = 1e9+1;
const ll inf = 2e18;
const ll mod = 998244353;
const ld eps = 1e-13;
const ld pi = 3.14159265359;
const int mxn = 2e5+5;
mt19937 _rand(time(NULL));
int n, m;
vector<pii> g[mxn];
int l[mxn], r[mxn];
int d[mxn];
bool vis[mxn];
int mind[mxn];
bool bridge[mxn];
void dfs(int u, int p){
vis[u] = true;
for(auto e : g[u]){
if(vis[e.st]){
if(e.st != p) mind[u] = min(mind[u], d[e.st]);
continue;
}
d[e.st] = d[u]+1;
dfs(e.st, u);
mind[u] = min(mind[u], mind[e.st]);
if(mind[e.st] > d[u]){
bridge[e.nd] = true;
}
}
}
ll sz[mxn];
int link[mxn];
ll gr;
void dfs2(int u){
sz[gr]++;
vis[u] = true;
link[u] = gr;
for(auto e : g[u]){
if(vis[e.st] || bridge[e.nd]) continue;
dfs2(e.st);
}
}
vector<int> v[mxn];
ll sub[mxn];
ll ans = 0;
void dfs3(int u, int p){
sub[u] = sz[u];
ll s = 0;
for(auto e : v[u]){
if(e == p) continue;
dfs3(e, u);
sub[u] += sub[e];
s += sub[e]*sub[e];
}
ll rem = n-sub[u];
s += rem*rem;
ll all = n-sz[u];
ll temp = sz[u] * (all*all-s);
ans += temp;
ll temp2 = all*(sz[u]-1)*(sz[u]-2)+all*(sz[u]-1);
ans += 2*temp2;
ans += sz[u]*(sz[u]-1)*(sz[u]-2);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
fr(i, 0, m){
cin >> l[i] >> r[i];
--l[i], --r[i];
g[l[i]].pb({r[i], i});
g[r[i]].pb({l[i], i});
}
fr(i, 0, n) mind[i] = n;
dfs(0, 0);
memset(vis, false, sizeof(vis));
fr(i, 0, n){
if(vis[i]) continue;
dfs2(i);
++gr;
}
fr(i, 0, m){
if(!bridge[i]) continue;
int a = link[l[i]];
int b = link[r[i]];
v[a].pb(b);
v[b].pb(a);
}
dfs3(0, 0);
cout<<ans<<endl;
}
# | 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... |