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 int long long
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define sp <<' '<<
#define pb push_back
#define FOR(i,a) for(int i=0;i<(a);i++)
#define FORE(i,a,b) for(int i=(a);i<(b);i++)
#define cont(a) for(auto el:a) cout<<el<<' '; cout<<endl;
#define contp(a) for(auto el:a) cout<<el.fi<<'-'<<el.se<<' ';cout<<endl;
#define DEBUG(x) cout<<#x sp ":" sp x<<endl;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef long long ll;
#define endl '\n'
#define mid (l+r)/2
const int MAXN=2e5+5;
int n,m;
vi adj[MAXN];
int low[MAXN], dep[MAXN], sub[MAXN], leaf[MAXN];
bool vis[MAXN];
int ans;
int back;
int tut[MAXN];
int cc;
int sz[MAXN];
int comp[MAXN];
void dfs(int nd,int ata,int h,bool f){
//true--> component size bulma
//false--> answer bulma
dep[nd]=h;
sub[nd]=0;
low[nd]=h;
if(f){
comp[nd]=cc;
sz[cc]++;
}
multiset<int> s;
for(auto kom:adj[nd]){
if(kom==ata) continue;
if(vis[kom]){
if(dep[kom]<dep[nd]){ //benden giden back edge
if(low[nd]>=dep[kom]) tut[nd]=nd;
low[nd]=min(low[nd],dep[kom]);
}
continue;
}
vis[kom]=true;
dfs(kom,nd,h+1,f);
sub[nd]+=sub[kom];
if(!f) s.insert(sub[kom]);
if(low[nd]>=low[kom]) tut[nd]=tut[kom];
low[nd]=min(low[nd],low[kom]);
}
if(!f){
int kalan=sz[comp[nd]]-sub[nd]-1;
s.insert(kalan);
int ekle=0;
/*DEBUG(nd);
DEBUG(low[nd]);
DEBUG(tut[nd]);
DEBUG(dep[nd]);
DEBUG(dep[tut[nd]]);*/
if(low[nd]<=h){ //back edge varsa
int bir=max(0LL,(dep[nd]-low[nd]-1)); //cycleda olan yukarı kısım
int iki=kalan-bir; //cycleda olmayan yukarı kısım
ekle+=bir*iki;
bir=max(0LL,dep[tut[nd]]-dep[nd]-1); //cycleda olan aşağı kısım
iki=sub[tut[nd]]; //cycyleda olmayan aşağı kısım
ekle+=bir*iki;
}
ekle*=2;
for(auto el:s) ekle+=(sz[comp[nd]]-1-el)*el; //subtreeler arası
//DEBUG(ekle);
//cout<<endl;
ans+=ekle;
}
sub[nd]++;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
FOR(i,m){
int a,b;
cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
cc=0;
FORE(i,1,n+1){
if(!vis[i]){
vis[i]=true;
dfs(i,-1,0,true);
cc++;
}
}
FORE(i,1,n+1) vis[i]=false;
ans=0;
FORE(i,1,n+1){
if(!vis[i]){
vis[i]=true;
dfs(i,-1,0,false);
}
}
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... |