# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
103640 | zubec | Duathlon (APIO18_duathlon) | C++14 | 1163 ms | 1049600 KiB |
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;
typedef long long ll;
typedef long double ld;
const int N = 100100;
int n, m;
vector <int> g[N];
ll sz[N];
ll ans = 0;
bool used[N];
void dfs(int v, int p){
sz[v] = 1;
used[v] = 1;
for (int i = 0; i < g[v].size(); i++){
int to = g[v][i];
if (to == p)
continue;
dfs(to, v);
sz[v] += sz[to];
}
}
void dfs2(int v, int p, int n){
ll cursum = n-sz[v];
for (int i = 0; i < g[v].size(); i++){
int to = g[v][i];
if (to == p)
continue;
dfs2(to, v, n);
ans += cursum*sz[to]*2;
cursum += sz[to];
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
//freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
cin >> n >> m;
for (int i = 1; i <= m; i++){
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++){
if (!used[i]){
dfs(i, 0);
dfs2(i, 0, sz[i]);
}
}
cout << ans;
}
/**
4 3
1 2
2 3
3 4
5 4
1 2
3 4
4 5
4 1
*/
Compilation message (stderr)
# | 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... |