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 ll long long
using namespace std;
const int N = (int)1e5 + 7;
ll ans;
vector<int> gr[N], ngr[N];
vector<int> order;
int n, m;
int used[N], col[N], tin[N], tout[N], fup[N];
int head[N], sz[N], ssz[N];
int cl, tiktak;
void precalc(int v, int pr = 0) {
used[v] = 1;
tin[v] = fup[v] = ++tiktak;
for (int to : gr[v]) {
if (to == pr) continue;
if (!used[to]) {
precalc(to, v);
fup[v] = min(fup[v], fup[to]);
} else {
fup[v] = min(fup[v], tin[to]);
}
if (tin[v] >= fup[to]) {
// not bridge
col[v] = col[to];
}
}
if (!col[v]) col[v] = ++cl;
tout[v] = tiktak;
sz[col[v]]++;
head[col[v]] = v;
}
int root;
void predfs(int v, int pr) {
ssz[v] = sz[v];
for (int to : ngr[v]) {
if (to == pr) continue;
predfs(to, v);
ssz[v] += ssz[to];
}
}
void dfs(int v, int pr) {
used[v] = 1;
for (int to : ngr[v]) {
if (to == pr) continue;
ans += (ssz[root] - ssz[v]) * 2LL * ssz[to];
dfs(to, v);
}
ans += sz[v] * (sz[v] - 1) * (sz[v] - 2);
ans += (ssz[root] - sz[v]) * 2LL * ((sz[v] - 1) + (sz[v] - 1) * 1LL * (sz[v] - 2));
}
main() {
scanf("%d %d", &n, &m);
for (int i = 1, u, v; i <= m; i++) {
scanf("%d %d", &u, &v);
gr[u].push_back(v);
gr[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
if (!used[i]) {
precalc(i);
}
}
for (int i = 1; i <= n; i++) {
for (int to : gr[i]) {
if (col[i] != col[to]) {
ngr[col[i]].push_back(col[to]);
}
}
}
memset(used, 0, sizeof used);
for (int i = 1; i <= cl; i++) {
if (!used[i]) {
root = i;
predfs(i, i);
dfs(i, i);
}
}
cout << ans;
}
Compilation message (stderr)
count_triplets.cpp:62:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:63:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
count_triplets.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
# | 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... |