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 fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1 &x, T2 y) { return y < x ? (x = y, true) : false; }
template<typename T1, typename T2> bool chkmax(T1 &x, T2 y) { return y > x ? (x = y, true) : false; }
void debug_out() { cerr << endl; }
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) { cerr << ' ' << A; debug_out(B...); }
template<typename T> void mdebug_out(T* a, int n) { for (int i = 0; i < n; ++i) cerr << a[i] << ' '; cerr << endl; }
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
#define debug(...) 1337
#define mdebug(a, n) 1337
#endif
template<typename T> ostream& operator << (ostream& stream, const vector<T> &v) { for (auto x : v) stream << x << ' '; return stream; }
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) { return stream << p.first << ' ' << p.second; }
const int N = 100005;
int n, m;
vector<int> G[N];
int tin[N], fup[N], timer;
int used[3 * N];
bool cut[N];
vector<int> block[2 * N];
int blocks;
vector<int> stk;
void dfs1(int v, int p = -1)
{
used[v] = 1;
tin[v] = fup[v] = timer++;
stk.push_back(v);
int cnt_down = 0;
for (int& u : G[v])
{
if (u == p) continue;
if (!used[u])
{
dfs1(u, v);
chkmin(fup[v], fup[u]);
if (fup[u] >= tin[v])
{
// v is a cut
++cnt_down;
cut[v] = true;
auto &b = block[blocks++];
for (; tin[stk.back()] >= tin[u]; stk.pop_back())
b.push_back(stk.back());
b.push_back(v);
}
}
else
{
chkmin(fup[v], tin[u]);
}
}
if (p == -1 && cnt_down <= 1)
cut[v] = false;
}
vector<int> T[3 * N]; // block-cut tree
ll C[3 * N], nC[3 * N];
ll cnt[3 * N];
ll sum[3 * N];
ll sum2[3 * N];
ll ans;
void add_edge(int u, int v)
{
// debug(u, v);
T[u].pb(v);
T[v].pb(u);
}
ll curSum;
void dfs2(int v, int p = -1)
{
used[v] = 1;
sum[v] = nC[v];
for (int& u : T[v]) if (u != p)
{
dfs2(u, v);
sum[v] += sum[u];
}
}
void dfs3(int v, int p = -1)
{
for (int& u : T[v]) if (u != p)
{
dfs3(u, v);
}
if (v <= n) // v is a cut
{
vector<pii> a = {{C[p] - 1, curSum - sum[v]}};
for (int& u : T[v]) if (u != p)
a.emplace_back(C[u] - 1, sum[u]);
ll sumSe = 0;
for (auto& p : a)
sumSe += p.se;
for (int i = 0; i < a.size(); ++i)
{
ll x = a[i].fi;
ll rem = 1 + sumSe - a[i].se;
ans -= x * rem * (rem - 1);
}
}
}
signed main()
{
#ifdef DEBUG
freopen("in", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; ++i)
{
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
for (int v = 1; v <= n; ++v) if (!used[v])
{
// new component
dfs1(v);
}
for (int bi = 0; bi < blocks; ++bi)
{
auto& b = block[bi];
// debug(bi, b);
for (auto& v : b)
{
if (cut[v])
{
add_edge(v, bi + n + 1);
}
else
{
nC[bi + n + 1]++;
}
}
C[bi + n + 1] = b.size();
}
for (int v = 1; v <= n; ++v)
if (cut[v])
nC[v] = C[v] = 1;
// mdebug(C + 1, n + blocks);
ans = 0;
memset(used, 0, sizeof used);
for (int bi = 0; bi < blocks; ++bi)
{
int v = bi + n + 1;
if (!used[v])
{
dfs2(v);
curSum = sum[v];
// debug(curSum);
ans += curSum * (curSum - 1) * (curSum - 2);
dfs3(v);
}
}
cout << ans << '\n';
return 0;
}
/*
6 9
6 2
5 6
4 1
1 2
5 4
6 1
5 3
2 4
2 5
7 10
3 5
2 3
1 5
6 4
5 7
6 5
1 3
4 1
3 6
1 6
10 20
5 6
10 5
7 3
7 4
2 4
10 3
1 10
7 1
8 10
4 6
6 10
10 4
1 3
4 5
4 1
7 2
3 4
9 7
5 1
2 3
*/
Compilation message (stderr)
count_triplets.cpp: In function 'void dfs3(int, int)':
count_triplets.cpp:117:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | for (int i = 0; i < a.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... |