# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
405397 | aryan12 | Duathlon (APIO18_duathlon) | C++17 | 1097 ms | 1048580 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;
#define int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1e5 + 5;
vector<int> g[N];
bool vis[N];
int subtree[N], ans = 0;
int n, m;
void dfs(int node, int par) {
subtree[node] = 1;
vis[node] = true;
for(int i = 0; i < g[node].size(); i++) {
if(g[node][i] != par) {
dfs(g[node][i], node);
subtree[node] += subtree[g[node][i]];
}
}
//cout << "node = " << node << ", subtree[node] = " << subtree[node] << "\n";
int total = subtree[node] - 1, left = n - 1;
for(int i = 0; i < g[node].size(); i++) {
if(g[node][i] != par) {
left -= subtree[g[node][i]];
ans += (subtree[g[node][i]] * left);
}
}
//cout << "node = " << node << ", ans = " << ans << "\n";
}
void Solve() {
cin >> n >> m;
for(int i = 1; i <= m; i++) {
int v, u;
cin >> v >> u;
g[v].push_back(u);
g[u].push_back(v);
}
for(int i = 1; i <= n; i++) {
if(!vis[i]) {
dfs(i, -1);
}
}
cout << ans * 2 << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) {
Solve();
}
}
/*
8 7
1 2
2 3
3 4
2 5
5 8
1 6
6 7
*/
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... |