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 <iostream>
#include <vector>
#include <map>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pll;
bool sub3=true;
ll N,M;
ll x,y;
ll A,B,C,ans;
bool V[300005];
vector<ll> grafo[300005];
ll S[300005];
map <pll,ll> E;
void DFSsub3(int k){
B++;
if(V[k])
return;
A++;
C++;
V[k]=true;
for(int f:grafo[k])
DFSsub3(f);}
void DFS(int k, int p=-1){
for(int f:grafo[k])
if(f!=p)
DFS(f,k);
E[{p,k}]=1;
for(int f:grafo[k])
if(f!=p)
E[{p,k}]+=E[{k,f}];
E[{k,p}]=C-E[{p,k}];
}
int main(){
cin>>N>>M;
for(int i=0;i<M;i++){
cin>>x>>y;
grafo[x].push_back(y);
grafo[y].push_back(x);
}
for(int i=1;i<N+1;i++)
if(grafo[i].size()>2)
sub3=false;
sub3=false;
if(sub3){
for(int i=1;i<N+1;i++)
if(!V[i]){
A=B=0;
DFSsub3(i);
if(B-1==2*A)
ans+=(A*(A-1)*(A-2));
else
ans+=(A*(A-1)*(A-2))/3;
}
cout<<ans;}
else{
for(int i=1;i<N+1;i++)
if(!V[i]){
DFSsub3(i);
DFS(i);
}
for(ll i=1;i<N+1;i++){
A=B=0;
for(ll f:grafo[i]){
A+=E[{i,f}];
B+=E[{i,f}]*E[{i,f}];
}
A=A*A;
ans+=A-B;
}
cout<<ans;
}
}
# | 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... |