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;
int nodes, N, M, lev[100009], dp[100009], vol[200009], papa[200009];
long long sumProd[200009];
stack < pair < int, int > > stk;
vector < int > v[100009], h[200009];
void addComp (pair < int, int > whenToStop)
{
pair < int, int > curr;
vector < int > currComp;
do {
curr = stk.top ();
stk.pop ();
currComp.push_back (curr.first);
currComp.push_back (curr.second);
}while (curr != whenToStop);
sort (currComp.begin (), currComp.end ());
currComp.erase (unique (currComp.begin (), currComp.end ()), currComp.end ());
/* for (auto it : currComp)
printf ("%d ", it);
printf ("\n");*/
nodes ++;
for (auto it : currComp)
h[it].push_back (nodes),
h[nodes].push_back (it);
}
void dfs (int nod, int tata)
{
dp[nod] = lev[nod];
for (auto it : v[nod])
if (lev[it] == 0)
{
lev[it] = lev[nod] + 1;
stk.push ({it, nod});
dfs (it, nod);
if (dp[it] < dp[nod])
dp[nod] = dp[it];
if (dp[it] >= lev[nod])
addComp ({it, nod});
}
for (auto it : v[nod])
if (it != tata)
{
if (lev[it] < dp[nod])
dp[nod] = lev[it];
}
}
void init (int nod, int tata)
{
vol[nod] = (nod <= N), papa[nod] = tata;
int sum = 0;
for (auto it : h[nod])
if (it != tata)
init (it, nod), vol[nod] += vol[it],
sumProd[nod] += 1LL * sum * vol[it],
sum += vol[it];
sumProd[nod] += 1LL * sum * (N - vol[nod]);
sumProd[nod] <<= 1LL;
}
int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);
scanf ("%d %d", &N, &M);
while (M --)
{
int x, y;
scanf ("%d %d", &x, &y);
v[x].push_back (y);
v[y].push_back (x);
}
lev[1] = 1, nodes = N;
dfs (1, -1), init (1, -1);
long long ans = 0;
for (int x=1; x<=N; x++)
{
ans += sumProd[x];
for (auto comp : h[x])
{
ans += sumProd[comp];
if (papa[comp] == x)
ans -= 2LL * (N - vol[comp]) * vol[comp];
else
ans -= 2LL * (N - vol[x]) * vol[x];
}
}
printf ("%lld\n", ans);
return 0;
}
Compilation message (stderr)
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:71: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:75:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d %d", &x, &y);
~~~~~~^~~~~~~~~~~~~~~~~
# | 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... |