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];
vi sub[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;
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);
if(!f){
for(auto el:sub[kom]) sub[nd].pb(el);
s.insert(sub[kom].size());
}
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].size()-1;
int ekle=0;
for(auto el:sub[nd]){
//DEBUG(el);
//cont(sub[el]);
ekle+=(sub[el].size()-1);
if(low[el]<dep[el]){
ekle+=(dep[el]-low[el]-1);
}
}
//DEBUG(nd);
ekle*=2;
//DEBUG(ekle);
//cont(s);
//DEBUG(sub[nd].size());
//cont(sub[nd]);
for(auto el:s) ekle+=(sub[nd].size()-el)*el;
//DEBUG(ekle);
ans+=ekle;
sub[nd].pb(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;
}
Compilation message (stderr)
count_triplets.cpp: In function 'void dfs(long long int, long long int, long long int, bool)':
count_triplets.cpp:83:13: warning: unused variable 'kalan' [-Wunused-variable]
83 | int kalan=sz[comp[nd]]-sub[nd].size()-1;
| ^~~~~
# | 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... |