#include<bits/stdc++.h>
#define MAXN 200007
using namespace std;
int n,m,a,b;
vector<int> v[MAXN];
long long sz[MAXN],sum[MAXN],comp[MAXN],ans;
void dfs(int x,int p){
sz[x]=1;
for(int i=0;i<v[x].size();i++){
if(v[x][i]==p)continue;
dfs(v[x][i],x);
sum[x]+=sz[v[x][i]]*(sz[v[x][i]]-1);
sz[x]+=sz[v[x][i]];
}
}
void dfs2(int x,int p,int s){
comp[x]=s;
for(int i=0;i<v[x].size();i++){
if(v[x][i]==p)continue;
dfs2(v[x][i],x,s);
}
sum[x]+=(s-sz[x])*(s-sz[x]-1);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>a>>b;
v[a].push_back(b);
v[b].push_back(a);
}
for(int i=1;i<=n;i++){
if(sz[i]==0){
dfs(i,0);
dfs2(i,0,sz[i]);
}
}
for(int i=1;i<=n;i++){
ans+=(long long) (comp[i]-1)*(comp[i]-2)-sum[i];
}
cout<<ans<<"\n";
return 0;
}
Compilation message
count_triplets.cpp: In function 'void dfs(int, int)':
count_triplets.cpp:12:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
count_triplets.cpp: In function 'void dfs2(int, int, int)':
count_triplets.cpp:24:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
615 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
615 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1057 ms |
904528 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5212 KB |
Output is correct |
2 |
Correct |
3 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
4 ms |
5212 KB |
Output is correct |
5 |
Correct |
3 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5208 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5120 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
5168 KB |
Output is correct |
11 |
Correct |
3 ms |
5164 KB |
Output is correct |
12 |
Correct |
3 ms |
5164 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5236 KB |
Output is correct |
15 |
Correct |
3 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
3 ms |
5212 KB |
Output is correct |
19 |
Correct |
2 ms |
5212 KB |
Output is correct |
20 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
11888 KB |
Output is correct |
2 |
Correct |
32 ms |
11820 KB |
Output is correct |
3 |
Correct |
29 ms |
11860 KB |
Output is correct |
4 |
Correct |
32 ms |
11852 KB |
Output is correct |
5 |
Correct |
35 ms |
11856 KB |
Output is correct |
6 |
Correct |
37 ms |
16088 KB |
Output is correct |
7 |
Correct |
35 ms |
14676 KB |
Output is correct |
8 |
Correct |
37 ms |
13900 KB |
Output is correct |
9 |
Correct |
36 ms |
13212 KB |
Output is correct |
10 |
Correct |
32 ms |
11856 KB |
Output is correct |
11 |
Correct |
29 ms |
11864 KB |
Output is correct |
12 |
Correct |
30 ms |
11748 KB |
Output is correct |
13 |
Correct |
29 ms |
11868 KB |
Output is correct |
14 |
Correct |
27 ms |
11552 KB |
Output is correct |
15 |
Correct |
24 ms |
11356 KB |
Output is correct |
16 |
Correct |
18 ms |
10328 KB |
Output is correct |
17 |
Correct |
21 ms |
12244 KB |
Output is correct |
18 |
Correct |
21 ms |
12112 KB |
Output is correct |
19 |
Correct |
21 ms |
12232 KB |
Output is correct |
20 |
Correct |
29 ms |
12220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Runtime error |
638 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
12112 KB |
Output is correct |
2 |
Correct |
31 ms |
11720 KB |
Output is correct |
3 |
Runtime error |
776 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
615 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
615 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |