#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long LL;
int n, m;
pair<int, int> ed[200010];
vector<int> input[100010], link[100010], linkrev[100010], bcc[100010];
bool ch[100010];
int arr[100010], re, c, col[100010];
LL subtree[100010], dp[100010];
LL ans;
unordered_set<LL> us;
void give_dir(int num)
{
if(ch[num])return;
ch[num]=true;
for(int i=0; i<input[num].size(); i++){
if(!us.count((LL)num*200000+(LL)input[num][i])&&!us.count((LL)input[num][i]*200000+(LL)num)){
link[num].pb(input[num][i]);
linkrev[input[num][i]].pb(num);
us.insert((LL)num*200000+(LL)input[num][i]);
us.insert((LL)input[num][i]*200000+(LL)num);
//printf("%d -> %d\n", num, input[num][i]);
}
give_dir(input[num][i]);
}
}
void dfs1(int num)
{
if(ch[num])return;
ch[num]=true;
for(int i=0; i<link[num].size(); i++){
dfs1(link[num][i]);
}
arr[++re]=num;
}
void dfs2(int num, int color)
{
if(col[num])return;
col[num]=color;
for(int i=0; i<linkrev[num].size(); i++){
dfs2(linkrev[num][i], color);
}
}
void tree_dp(int num)
{
if(dp[num])return;
dp[num]=subtree[num];
ans+=subtree[num]*(subtree[num]-1)*(subtree[num]-2);
for(int i=0; i<bcc[num].size(); i++){
if(dp[bcc[num][i]])continue;
tree_dp(bcc[num][i]);
dp[num]+=dp[bcc[num][i]];
}
}
void BCC_dfs(int num)
{
if(ch[num])return;
ch[num]=true;
LL temp=0;
for(int i=0; i<bcc[num].size(); i++){
ans+=(subtree[num]-1)*(subtree[num]-1)*dp[bcc[num][i]]*2;
temp+=dp[bcc[num][i]];
ans-=subtree[num]*dp[bcc[num][i]]*dp[bcc[num][i]];
}
ans+=subtree[num]*temp*temp;
for(int i=0; i<bcc[num].size(); i++){
LL tnum=dp[num], tbcc=dp[bcc[num][i]];
dp[num]=temp-tbcc+subtree[num];
dp[bcc[num][i]]=tnum;
BCC_dfs(bcc[num][i]);
dp[bcc[num][i]]=tbcc;
dp[num]=tnum;
}
}
int main()
{
scanf("%d %d", &n, &m);
for(int i=1; i<=m; i++){
int a, b;
scanf("%d %d", &a, &b);
input[a].pb(b);
input[b].pb(a);
ed[i].F=a;
ed[i].S=b;
}
for(int i=1; i<=n; i++){
give_dir(i);
}
memset(ch, 0, sizeof(ch));
for(int i=1; i<=n; i++){
if(!ch[i])dfs1(i);
}
for(int i=n; i>=1; i--){
if(!col[arr[i]])dfs2(arr[i], ++c);
subtree[col[arr[i]]]++;
}
for(int i=1; i<=m; i++){
if(col[ed[i].F]!=col[ed[i].S]){
bcc[col[ed[i].F]].pb(col[ed[i].S]);
bcc[col[ed[i].S]].pb(col[ed[i].F]);
}
}
memset(ch, 0, sizeof(ch));
for(int i=1; i<=c; i++)tree_dp(i);
for(int i=1; i<=c; i++)BCC_dfs(i);
printf("%lld", ans);
}
Compilation message
count_triplets.cpp: In function 'void give_dir(int)':
count_triplets.cpp:20:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<input[num].size(); i++){
~^~~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'void dfs1(int)':
count_triplets.cpp:35:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<link[num].size(); i++){
~^~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'void dfs2(int, int)':
count_triplets.cpp:44:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<linkrev[num].size(); i++){
~^~~~~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'void tree_dp(int)':
count_triplets.cpp:53:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<bcc[num].size(); i++){
~^~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'void BCC_dfs(int)':
count_triplets.cpp:64:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<bcc[num].size(); i++){
~^~~~~~~~~~~~~~~~
count_triplets.cpp:70:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<bcc[num].size(); i++){
~^~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:81:10: 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:84:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9848 KB |
Output is correct |
2 |
Correct |
11 ms |
9848 KB |
Output is correct |
3 |
Correct |
10 ms |
9848 KB |
Output is correct |
4 |
Correct |
10 ms |
9848 KB |
Output is correct |
5 |
Correct |
11 ms |
9848 KB |
Output is correct |
6 |
Correct |
11 ms |
9848 KB |
Output is correct |
7 |
Incorrect |
10 ms |
9848 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9848 KB |
Output is correct |
2 |
Correct |
11 ms |
9848 KB |
Output is correct |
3 |
Correct |
10 ms |
9848 KB |
Output is correct |
4 |
Correct |
10 ms |
9848 KB |
Output is correct |
5 |
Correct |
11 ms |
9848 KB |
Output is correct |
6 |
Correct |
11 ms |
9848 KB |
Output is correct |
7 |
Incorrect |
10 ms |
9848 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
292 ms |
38204 KB |
Output is correct |
2 |
Correct |
277 ms |
38292 KB |
Output is correct |
3 |
Correct |
291 ms |
35936 KB |
Output is correct |
4 |
Correct |
292 ms |
36920 KB |
Output is correct |
5 |
Correct |
267 ms |
33496 KB |
Output is correct |
6 |
Correct |
270 ms |
34568 KB |
Output is correct |
7 |
Correct |
278 ms |
33592 KB |
Output is correct |
8 |
Correct |
287 ms |
34256 KB |
Output is correct |
9 |
Correct |
280 ms |
32548 KB |
Output is correct |
10 |
Correct |
296 ms |
32980 KB |
Output is correct |
11 |
Correct |
238 ms |
29368 KB |
Output is correct |
12 |
Correct |
220 ms |
29064 KB |
Output is correct |
13 |
Correct |
230 ms |
28216 KB |
Output is correct |
14 |
Correct |
208 ms |
27964 KB |
Output is correct |
15 |
Correct |
160 ms |
25656 KB |
Output is correct |
16 |
Correct |
166 ms |
25272 KB |
Output is correct |
17 |
Correct |
17 ms |
12280 KB |
Output is correct |
18 |
Correct |
17 ms |
12152 KB |
Output is correct |
19 |
Correct |
16 ms |
12280 KB |
Output is correct |
20 |
Correct |
16 ms |
12152 KB |
Output is correct |
21 |
Correct |
16 ms |
12152 KB |
Output is correct |
22 |
Correct |
17 ms |
12152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
10104 KB |
Output is correct |
2 |
Correct |
12 ms |
10104 KB |
Output is correct |
3 |
Correct |
12 ms |
10104 KB |
Output is correct |
4 |
Correct |
12 ms |
10232 KB |
Output is correct |
5 |
Correct |
12 ms |
10104 KB |
Output is correct |
6 |
Correct |
13 ms |
10232 KB |
Output is correct |
7 |
Correct |
12 ms |
10104 KB |
Output is correct |
8 |
Correct |
14 ms |
10104 KB |
Output is correct |
9 |
Correct |
14 ms |
10104 KB |
Output is correct |
10 |
Correct |
14 ms |
10104 KB |
Output is correct |
11 |
Correct |
14 ms |
10104 KB |
Output is correct |
12 |
Correct |
12 ms |
10104 KB |
Output is correct |
13 |
Correct |
12 ms |
10116 KB |
Output is correct |
14 |
Correct |
14 ms |
10104 KB |
Output is correct |
15 |
Correct |
14 ms |
9976 KB |
Output is correct |
16 |
Correct |
13 ms |
9976 KB |
Output is correct |
17 |
Correct |
13 ms |
10104 KB |
Output is correct |
18 |
Correct |
13 ms |
10104 KB |
Output is correct |
19 |
Correct |
14 ms |
10104 KB |
Output is correct |
20 |
Correct |
12 ms |
10104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
264 ms |
32184 KB |
Output is correct |
2 |
Correct |
292 ms |
32120 KB |
Output is correct |
3 |
Correct |
262 ms |
32184 KB |
Output is correct |
4 |
Correct |
320 ms |
32176 KB |
Output is correct |
5 |
Correct |
329 ms |
32184 KB |
Output is correct |
6 |
Correct |
355 ms |
38540 KB |
Output is correct |
7 |
Correct |
424 ms |
36160 KB |
Output is correct |
8 |
Correct |
317 ms |
35232 KB |
Output is correct |
9 |
Correct |
347 ms |
34092 KB |
Output is correct |
10 |
Correct |
361 ms |
32188 KB |
Output is correct |
11 |
Correct |
338 ms |
32300 KB |
Output is correct |
12 |
Correct |
282 ms |
32156 KB |
Output is correct |
13 |
Correct |
278 ms |
32320 KB |
Output is correct |
14 |
Correct |
260 ms |
30956 KB |
Output is correct |
15 |
Correct |
234 ms |
29688 KB |
Output is correct |
16 |
Correct |
145 ms |
24072 KB |
Output is correct |
17 |
Correct |
206 ms |
31436 KB |
Output is correct |
18 |
Correct |
201 ms |
31540 KB |
Output is correct |
19 |
Correct |
198 ms |
31832 KB |
Output is correct |
20 |
Correct |
241 ms |
31656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
10104 KB |
Output is correct |
2 |
Correct |
12 ms |
10104 KB |
Output is correct |
3 |
Incorrect |
13 ms |
10104 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
354 ms |
32128 KB |
Output is correct |
2 |
Correct |
285 ms |
32444 KB |
Output is correct |
3 |
Incorrect |
328 ms |
34436 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9848 KB |
Output is correct |
2 |
Correct |
11 ms |
9848 KB |
Output is correct |
3 |
Correct |
10 ms |
9848 KB |
Output is correct |
4 |
Correct |
10 ms |
9848 KB |
Output is correct |
5 |
Correct |
11 ms |
9848 KB |
Output is correct |
6 |
Correct |
11 ms |
9848 KB |
Output is correct |
7 |
Incorrect |
10 ms |
9848 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9848 KB |
Output is correct |
2 |
Correct |
11 ms |
9848 KB |
Output is correct |
3 |
Correct |
10 ms |
9848 KB |
Output is correct |
4 |
Correct |
10 ms |
9848 KB |
Output is correct |
5 |
Correct |
11 ms |
9848 KB |
Output is correct |
6 |
Correct |
11 ms |
9848 KB |
Output is correct |
7 |
Incorrect |
10 ms |
9848 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |