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;
struct DDS{
int par[1<<18];
int abp(int n){
return par[n]?par[n]=abp(par[n]):n;
}
void merge(int a,int b){
a=abp(a),b=abp(b);
if(a-b)par[a]=b;
}
} dsu;
vector<int>adj[1<<17],adj2[1<<18];
set<pair<int,int>>st[1<<17];
int par[1<<17],dep[1<<17],cycpar[1<<17],CC,N,sz[1<<17];
void dfsC(int n,int p){
dep[n]=dep[p]+1;
par[n]=p;
for(auto i:adj[n]){
if(i==p)continue;
if(!dep[i])
dfsC(i,n);
else if(dep[i]<dep[n])
st[n].insert({dep[i],++CC});
}
}
void dfsM(int n,int p){
for(auto i:adj[n]) {
if(par[i]-n)continue;
dfsM(i,n);
if(st[i].empty())continue;
if(st[i].begin()->first<dep[n]) {
if(st[n].size())
dsu.merge(st[i].begin()->second,st[n].begin()->second);
st[n].insert(*st[i].begin());
} else adj2[n].push_back(dsu.abp(st[i].begin()->second));
}
if(st[n].size())
cycpar[n]=st[n].begin()->second;
else adj2[p].push_back(n);
}
long long TOT;
void dfsA(int n){
sz[n]=n<=N;
long long k=(1ll*N-sz[n])*(N-sz[n]);
for(auto i:adj2[n])dfsA(i),
sz[n]+=sz[i],k-=sz[i]*1ll*sz[i];
k-=(1ll*N-sz[n])*(N-sz[n]);
if(n>N)k*=adj2[n].size()-1;
TOT+=k;
}
bitset<100100>Rt;
int main(){
cin.tie(0)->sync_with_stdio(0);
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
int a,b;cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
CC=N=n;
for(int K=1;K<=n;K++) {
if(dep[K])continue;
Rt[K]=1;
dfsC(1,0);
dfsM(1,0);
}
for(int i=1;i<=n;i++) if(cycpar[i])
adj2[dsu.abp(cycpar[i])].push_back(i);
for(int i=1;i<=N;i++)
if(Rt[i])dfsA(1);
cout<<TOT<<'\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... |