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;
#define mod 1000000007
#define oo 200000000
const int N = 100010;
const int M = 100010;
int n , m ,comp[N] , num[N] , comp_cnt = 0 , u , v , dfs_cnt = 0 , dfs_low[N] , dfs_num[N] , num1[N] , num2[N] , tmp[N] , num3[N] , tmp2[N] , s = 0;
bool vis[N] ;
long long all = 0 , ans = 0, all2 = 0;
vector< int > g[N] ;
void DFS1(int node){
comp[node] = comp_cnt;
num[comp_cnt]++;
vis[node] = true;
for(int i=0;i<g[node].size();i++){
if(vis[g[node][i]] == false)
DFS1(g[node][i]);
}
}
void DFS(int node, int prnt){
dfs_low[node] = dfs_num[node] = dfs_cnt++;
num1[node] = 1;
num2[node] = 1;
tmp[node] = num[comp[node]] - 1;
int s = num[comp[node]] - 1;
vis[node] = true;
vector< int > v;
for(int i=0;i<g[node].size();i++){
if(g[node][i] == prnt) continue;
if(vis[g[node][i]]){
dfs_low[node] = min(dfs_low[node],dfs_num[g[node][i]]);
continue;
}
DFS(g[node][i],node);
dfs_low[node] = min(dfs_low[node],dfs_low[g[node][i]]);
num1[node] += num1[g[node][i]];
if(dfs_low[g[node][i]] == dfs_num[node]){
ans += (long long)num2[g[node][i]] * (num2[g[node][i]] + 1) * (num2[g[node][i]] - 1);
all -= (long long)num2[g[node][i]] * (num2[g[node][i]] + 1) * (num2[g[node][i]] - 1) / 6;
tmp[node] -= num1[g[node][i]];
s -= num1[g[node][i]];
ans += (long long)tmp[node] * num1[g[node][i]] * 2;
all -= (long long)tmp[node] * num1[g[node][i]];
v.push_back(num1[g[node][i]]);
}
else if(dfs_low[g[node][i]] < dfs_num[node]){
num2[node] += num2[g[node][i]];
}
else{
v.push_back(num1[g[node][i]]);
s -= num1[g[node][i]];
tmp[node] -= num1[g[node][i]];
ans += (long long)tmp[node] * num1[g[node][i]] * 2;
all -= (long long)tmp[node] * num1[g[node][i]];
}
}
v.push_back(s);
for(int i=0;i<v.size();i++){
for(int j=i+1;j<v.size();j++){
for(int k=j+1;k<v.size();k++){
all -= (long long)v[i] * v[j] * v[k];
}
}
}
}
int main() {
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
for(int i=1;i<=n;i++){
if(vis[i] == false){
DFS1(i);
all += (long long)num[comp_cnt] * (num[comp_cnt] - 1) * (num[comp_cnt] - 2) / 6;
comp_cnt++;
}
}
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++){
if(vis[i] == false)
DFS(i,-1);
}
ans += (long long)all * 4;
cout << ans << endl;
return 0;
}
Compilation message (stderr)
count_triplets.cpp: In function 'void DFS1(int)':
count_triplets.cpp:16:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[node].size();i++){
~^~~~~~~~~~~~~~~
count_triplets.cpp: In function 'void DFS(int, int)':
count_triplets.cpp:30:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[node].size();i++){
~^~~~~~~~~~~~~~~
count_triplets.cpp:60:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v.size();i++){
~^~~~~~~~~
count_triplets.cpp:61:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=i+1;j<v.size();j++){
~^~~~~~~~~
count_triplets.cpp:62:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k=j+1;k<v.size();k++){
~^~~~~~~~~
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:70: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:72:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&u,&v);
~~~~~^~~~~~~~~~~~~~
# | 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... |