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 ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define INF 100000000000000000
#define modulo 1000000007
#define mod 998244353
#define int long long int
using namespace std;
vector<int> adj[300005];
vector<int> sub(300005, 1);
int ans = 0;
void dfs(int x, int pre){
sub[x] = 1;
for(auto y : adj[x]){
if(y == pre) continue;
dfs(y, x);
sub[x] += sub[y];
}
}
void calc(int x, int pre){
ans += 2ll * (sub[x] - 1) * (sub[1] - sub[x]);
int w = 0;
for(auto y : adj[x]){
if(y == pre) continue;
ans += 2ll * sub[y] * w;
w += sub[y];
}
for(auto y : adj[x]){
if(y == pre) continue;
calc(y, x);
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
for(int i = 0; i < m; i++){
int x, y;
cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
}
dfs(1, -1);
calc(1, -1);
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... |