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<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5 + 9;
int n, m, deg[N];
int par[N];
int root(int u) { return par[u] < 0 ? u : par[u] = root(par[u]); }
void Union(int u, int v) {
if ((u = root(u)) == (v = root(v))) return;
if (par[u] > par[v]) swap(u, v);
par[u] += par[v];
par[v] = u;
}
ll calc2(int n) {
return (ll)n * (n - 1) * (n - 2);
}
ll calc(int n) {
return calc2(n) / 3;
}
bool f[N];
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n >> m;
fill(par + 1, par + n + 1, -1);
for (int i = 0, u, v; i < m; ++i) {
cin >> u >> v;
Union(u, v);
++deg[u], ++deg[v];
}
for (int i = 1; i <= n; ++i)
if (deg[i] == 1) f[root(i)] = true;
ll ans(0);
for (int i = 1; i <= n; ++i)
if (par[i] < 0) ans += f[i] ? calc(-par[i]) : calc2(-par[i]);
cout << ans << '\n';
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
# | 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... |