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>
using namespace std;
#define N 300100
long long dep[N],id[N],low[N],CC,CC2,in[N],par[N],nod1,nod2,sz[N],ans;
struct unionfind{
int par[N];
int abp(int n){
return(par[n]==n?n:par[n]=abp(par[n]));
}
void init(){
for(int i=0;i<N;i++)
par[i]=i;
}
void merge(int a,int b){
a=abp(a),b=abp(b);
if(a-b)par[a]=b;
}
} CYCLE;
set<pair<int,int>>st[N];
vector<int>adj1[N],adj2[N];
void tarjan(int n,int p){
nod1++;
low[n]=id[n]=++CC2;
for(auto i:adj1[n]){
if(i==p)continue;
if(!id[i]){
dep[i]=dep[n]+1;
tarjan(i,n);
low[n]=min(low[n],low[i]);
if(low[i]>id[n])
adj2[n].push_back(i);
} else if(dep[i]<dep[n])
st[n].insert({dep[i],++CC}),
low[n]=min(low[n],low[i]);
}
}
void dfscyc(int n){
for(auto i:adj1[n]){
if(dep[i]-dep[n]-1)
continue;
dfscyc(i);
if(st[i].size()&&st[i].begin()->first<dep[n]){
if(st[n].size()) CYCLE.merge(st[i].begin()
->second,st[n].begin()->second);
st[n].insert(*st[i].begin());
}
}
if(st[n].size())
in[n]=st[n].begin()->second;
}
void dfs2(int n,int p){
for(auto i:adj1[n])
if(dep[i]==dep[n]+1)
dfs2(i,n);
adj2[CYCLE.abp(in[n])].push_back(n);
par[CYCLE.abp(in[n])]=p;
}
void dfs3(int n){
long long sum=0,x;
for(auto i:adj2[n]){
dfs3(i);
sum+=sz[n]*sz[i];
sz[n]+=sz[i];
}
x=sz[n];
sz[n]+=n<=nod2;
sum+=(nod1-sz[n])*x;
ans+=(n>nod2?adj2[n].size()-1:1)*sum*2;
}
int main(){
cin.tie(0)->sync_with_stdio(0);
CYCLE.init();
int n,m;
cin>>n>>m;
CC=nod2=n;
while(m--){
int a,b;
cin>>a>>b;
adj1[a].push_back(b);
adj1[b].push_back(a);
}
for(int i=1;i<=n;i++)
if(!id[i]){
nod1=0;
int prv=CC;
tarjan(i,0),
dfscyc(i),dfs2(i,0);
for(int j=prv;j++<CC;)
if(CYCLE.par[j]==j)
adj2[par[j]].push_back(j);
dfs3(i);
}
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... |