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;
const int MAXN = 300005;
int N, M;
vector<int> G[MAXN];
vector<int> T[MAXN];
// Biconnected Components
int dis[MAXN];
int low[MAXN];
int compsz[MAXN];
vector<vector<int>> comps;
void DfsBcc(int u, int p = 0) {
static int t = 1;
dis[u] = low[u] = t++;
static vector<int> st;
st.emplace_back(u);
int child = 0;
for (auto v : G[u]) if (v != p) {
if (dis[v]) {
low[u] = min(low[u], dis[v]);
} else {
DfsBcc(v, u);
low[u] = min(low[u], low[v]);
child++;
if ((p == 0 && child > 1) || (p != 0 && low[v] >= dis[u])) {
comps.emplace_back();
vector<int> &cur = comps.back();
cur.emplace_back(u);
while (cur.back() != v) {
cur.emplace_back(st.back());
st.pop_back();
}
}
}
}
if (p == 0 && !st.empty()) {
comps.emplace_back(st);
st.clear();
}
}
void InitBcc() {
for (int i = 1; i <= N; i++) {
if (!dis[i]) {
DfsBcc(i);
}
}
for (int i = 0; i < comps.size(); i++) {
vector<int> &c = comps[i];
sort(begin(c), end(c));
c.resize(unique(begin(c), end(c)) - begin(c));
compsz[i] = c.size();
for (auto j : c) {
T[i + N + 1].emplace_back(j);
T[j].emplace_back(i + N + 1);
}
}
}
int vis[MAXN];
int sz[MAXN];
void DfsSize(int u, int p = 0) {
vis[u] = 1;
sz[u] = (u <= N);
for (auto v : T[u]) if (v != p) {
DfsSize(v, u);
sz[u] += sz[v];
}
}
void DfsCount(int u, long long &ans, int p = 0, int r = 0) {
if (r == 0) {
r = u;
ans += 1ll * sz[u] * (sz[u] - 1) * (sz[u] - 2);
}
if (u <= N) {
for (auto v : T[u]) {
if (v == p) {
ans -= 1ll * (compsz[v - N - 1] - 1) * sz[u] * (sz[u] - 1);
} else {
ans -= 1ll * (compsz[v - N - 1] - 1) * (sz[r] - sz[v]) * (sz[r] - sz[v] - 1);
}
}
}
for (auto v : T[u]) if (v != p) {
DfsCount(v, ans, u, r);
}
}
long long Solve() {
InitBcc();
long long res = 0;
for (int i = 1; i <= N; i++) {
if (!vis[i]) {
DfsSize(i);
DfsCount(i, res);
}
}
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> N >> M;
for (int i = 0; i < M; i++) {
int u, v;
cin >> u >> v;
G[u].emplace_back(v);
G[v].emplace_back(u);
}
cout << Solve() << "\n";
return 0;
}
Compilation message (stderr)
count_triplets.cpp: In function 'void InitBcc()':
count_triplets.cpp:57:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < comps.size(); i++) {
~~^~~~~~~~~~~~~~
# | 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... |