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
int const N=2e5+5;
int const mod=1e9+7;
vector<int> adj[N];
vector<int> mem[N];
int sz[N];
int c=0;
void dfs(int node,int par){
mem[c].push_back(node);
for(int i:adj[node])
if(i!=par){
dfs(i,node);
sz[node]+=sz[i];
}
sz[node]++;
}
signed main(){
int n,m;
cin>>n>>m;
for (int i = 0; i < m; ++i)
{
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int ans=0;
for (int i = 1; i <=n; ++i)
if(sz[i]==0){
dfs(i,0);
int cc=sz[i];
ans+=cc*(cc-1)*(cc-1);
ans+=cc*cc;
for(int j:mem[c]){
ans-=sz[j]*sz[j];
ans-=(cc-sz[j])*(cc-sz[j]);
}
c++;
}
cout<<ans<<endl;
return 0;
}
# | 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... |