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=1e5+5;
int n,m;
vi adj[MAXN];
int ans;
/*----------BLOCK-CUT TREE----------- */
bool vis[MAXN], isarc[MAXN];
int dep[MAXN], low[MAXN];
vi st;
vi bcc[MAXN*2];
int cnt; //şuanki grafta kaç eleman var
int ind;
void dfs(int nd, int ata,int h){
dep[nd]=h;
low[nd]=h;
int ch=0;
st.pb(nd);
cnt++;
for(auto kom:adj[nd]){
if(kom==ata) continue;
if(vis[kom]){
if(dep[kom]<dep[nd]) low[nd]=min(low[nd],dep[kom]);
continue;
}
vis[kom]=true;
dfs(kom,nd,h+1);
ch++;
if(low[kom]>=dep[nd]){ //we should create new block
//komsuyu tüm graftan ayıran node == nd
ind++;
bcc[nd].pb(ind); //nodedan yeni bloğa
int el;
do{
el=st.back();
st.pop_back();
bcc[ind].pb(el);
}while(el!=kom); //bloktan elemanlara edge çek
if(ata!=-1) isarc[nd]=true;
}
low[nd]=min(low[nd],low[kom]);
}
if(ata==-1 and ch>1) isarc[nd]=true;
}
/*----------DFS----------- */
int sub[MAXN];
bool vis2[MAXN];
void dfs2(int nd,int ata){
sub[nd]=(nd<=n); //ben blok muyum yoksa node muyum
for(auto kom:bcc[nd]){
if(kom==ata) continue;
if(vis2[kom]) continue;
vis2[kom]=true;
dfs2(kom,nd);
sub[nd]+=sub[kom];
if(nd>n){
//ben bloksam
//bcc[nd].size() --> bende kaç eleman var
int bir=bcc[nd].size(); //bu bloktan 1 tane
int iki=sub[kom]*(sub[kom]-1); //subtreemdeki nodelardan 2 tane seç
ans-=max(0LL,bir*iki);
}
}
//beni root kabul edip hesaplama yapıyorum
//benim üstümde kalan subtreeyi hesaplamadık
if(nd>n){
int bir=bcc[nd].size();
int iki=(cnt-sub[nd])*(cnt-sub[nd]-1);
ans-=bir*iki;
}
}
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);
}
ans=n*(n-1)*(n-2);
ind=n;
FORE(j,1,n+1){
if(!vis[j]){
vis[j]=true;
dfs(j,-1,0);
dfs2(j,-1);
}
}
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... |