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>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int n, m;
vector<vector<int>> graph;
vector<int> sz;
ll ans = 0;
int N = 0;
void dfs(int nd, int ss){
N++;
sz[nd] = 1;
for(int x: graph[nd]) if(x != ss) dfs(x, nd), sz[nd]+=sz[x];
}
void dfs2(int nd, int ss){
for(int x: graph[nd]) if(x != ss) dfs2(x, nd);
ans+=ll(N-sz[nd])*ll(sz[nd]-1);
for(int x: graph[nd]) if(x != ss) ans+=ll(N-sz[x]-1)*ll(sz[x]);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
graph.resize(n);
sz.resize(n, 0);
for(int i = 0; i < m; i++){
int a, b; cin >> a >> b; a--, b--;
graph[a].pb(b);
graph[b].pb(a);
}
for(int i = 0; i < n; i++) if(!sz[i]){
N = 0;
dfs(i, -1);
dfs2(i, -1);
}
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... |