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 Nmax = 4e5 + 5;
typedef long long ll;
int n, m, x, y, i, Ids, total;
int w[Nmax], level[Nmax], low[Nmax];
stack<int> st;
ll ans, tp[Nmax];
bool used[Nmax];
vector< vector<int> > bico;
vector<int> v[Nmax], edge[Nmax];
void dfs(int node, int dad = 0)
{
low[node] = level[node] = level[dad] + 1;
st.push(node);
for(auto it : v[node])
{
if(it == dad) continue;
if(level[it])
{
low[node] = min(low[node], level[it]);
continue;
}
dfs(it, node);
low[node] = min(low[node], low[it]);
if(low[it] >= level[node])
{
bico.push_back(vector<int> ());
int x;
do
{
x = st.top();
st.pop();
bico.back().push_back(x);
} while(x != it);
bico.back().push_back(node);
}
}
}
ll sel3(int x) { return (ll) x * (x-1) * (x-2); }
ll sel2(int x) { return (ll) x * (x-1); }
void add_edge(int x, int y)
{
edge[x].push_back(y);
edge[y].push_back(x);
}
void dfss(int node, int dad = 0)
{
used[node] = 1;
w[node] = (node <= n);
for(auto it : edge[node])
if(it != dad)
{
dfss(it, node);
w[node] += w[it];
if(node > n) tp[node] += sel2(w[it]);
}
}
void solve(int node, int dad = 0)
{
for(auto it : edge[node])
if(it != dad) solve(it, node);
if(node > n) return;
for(auto it : edge[node])
if(it != dad) ans -= tp[it];
else ans -= tp[it] - sel2(w[node]) + sel2(total - w[it]);
}
int main()
{
// freopen("input", "r", stdin);
// freopen("output", "w", stdout);
cin >> n >> m;
for(i=1; i<=m; ++i)
{
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
for(i=1; i<=n; ++i)
if(!level[i])
dfs(i);
for(i=0; i<bico.size(); ++i)
for(auto node : bico[i])
add_edge(node, n+i+1);
for(i=1; i<=n; ++i)
if(!used[i])
{
dfss(i);
total = w[i];
ans += sel3(total);
solve(i);
}
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:105:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<bico.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... |