#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;
}
namespace treeSubtask {
int vol[100009];
void dfs (int nod, int tata)
{
vol[nod] = 1;
for (auto it : v[nod])
if (it != tata)
dfs (it, nod), vol[nod] += vol[it];
}
long long solveTree ()
{
dfs (1, -1);
long long ans = 0;
for (int x=1; x<=N; x++)
{
int sum = 0;
for (auto it : v[x])
{
int currSize = 0;
if (vol[it] > vol[x]) currSize = N - vol[x];
else currSize = vol[it];
ans += 1LL * sum * currSize;
sum += currSize;
}
}
return ans * 2LL;
}
};
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);
}
printf ("%lld\n", treeSubtask::solveTree ());
return 0;
lev[1] = 1, nodes = N;
dfs (1, -1), init (1, -1);
long long ans = 0;
for (int x=1; x<=N; x++)
{
long long before = ans;
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
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:115:15: warning: unused variable 'before' [-Wunused-variable]
long long before = ans;
^~~~~~
count_triplets.cpp:100: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:104: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 |
1 |
Execution timed out |
1079 ms |
1048576 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1079 ms |
1048576 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1099 ms |
1048576 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1048576 KB |
Output is correct |
2 |
Correct |
7 ms |
1048576 KB |
Output is correct |
3 |
Correct |
7 ms |
1048576 KB |
Output is correct |
4 |
Correct |
9 ms |
1048576 KB |
Output is correct |
5 |
Correct |
9 ms |
1048576 KB |
Output is correct |
6 |
Correct |
9 ms |
1048576 KB |
Output is correct |
7 |
Correct |
9 ms |
1048576 KB |
Output is correct |
8 |
Correct |
9 ms |
1048576 KB |
Output is correct |
9 |
Correct |
9 ms |
1048576 KB |
Output is correct |
10 |
Incorrect |
9 ms |
1048576 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
1048576 KB |
Output is correct |
2 |
Correct |
120 ms |
1048576 KB |
Output is correct |
3 |
Correct |
92 ms |
1048576 KB |
Output is correct |
4 |
Correct |
92 ms |
1048576 KB |
Output is correct |
5 |
Correct |
100 ms |
1048576 KB |
Output is correct |
6 |
Correct |
83 ms |
1048576 KB |
Output is correct |
7 |
Correct |
105 ms |
1048576 KB |
Output is correct |
8 |
Correct |
72 ms |
1048576 KB |
Output is correct |
9 |
Correct |
149 ms |
1048576 KB |
Output is correct |
10 |
Incorrect |
63 ms |
1048576 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1048576 KB |
Output is correct |
2 |
Correct |
8 ms |
1048576 KB |
Output is correct |
3 |
Execution timed out |
1119 ms |
1048576 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
1048576 KB |
Output is correct |
2 |
Correct |
74 ms |
1048576 KB |
Output is correct |
3 |
Execution timed out |
1096 ms |
1048576 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1079 ms |
1048576 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1079 ms |
1048576 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |