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>
#pragma GCC optimize("Ofast")
using namespace std;
#define all(v) v.begin(), v.end()
#define F first
#define S second
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll>pil;
const int N=1e5+5;
int n, m, sz[N], nn;
vector<int>g[N], nd, kd[N];
bool mk[N];
void dfs(const int &v){
mk[v]=1;
sz[v]=1;
nd.push_back(v);
for(int u: g[v]){
if(!mk[u]){
kd[v].push_back(u);
dfs(u);
sz[v]+=sz[u];
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>m;
if(m>=n)
return 0;
for(int i=0;i<m;++i){
int u, v;
cin>>u>>v;
g[--u].push_back(--v);
g[v].push_back(u);
}
ll ans=0;
for(int i=0;i<n;++i){
if(!mk[i]){
dfs(i);
for(int v: nd){
nn=nd.size();
ans+=1LL*(nn-1)*(nn-2);
ans-=1LL*(nn-sz[v])*(nn-sz[v]-1);
for(int u: kd[v])
ans-=1LL*sz[u]*(sz[u]-1);
}
nd.clear();
}
}
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... |