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;
typedef long long ll;
typedef long double ld;
const int N = 200100;
int n, m;
vector <int> g[N];
ll sz[N];
bool used[N];
int tin[N], fup[N], tim;
vector <int> stck;
vector <vector <int> > cmp;
void findCutpoints(int v, int p){
tin[v] = fup[v] = ++tim;
stck.push_back(v);
used[v] = 1;
int sons = 0;
for (int i = 0; i < g[v].size(); i++){
int to = g[v][i];
if (to == p)
continue;
if (!used[to]){
findCutpoints(to, v);
fup[v] = min(fup[v], fup[to]);
if (tin[v] <= fup[to]){
cmp.push_back({v});
while(cmp.back().back() != to){
cmp.back().push_back(stck.back());
stck.pop_back();
}
}
} else {
fup[v] = min(fup[v], tin[to]);
}
}
}
ll ans = 0;
void dfs1(int v, int p){
sz[v] = v <= n;
used[v] = 1;
for (int i = 0; i < g[v].size(); i++){
int to = g[v][i];
if (to == p)
continue;
dfs1(to, v);
sz[v] += sz[to];
}
}
void dfs2(int v, int p, ll n){
if (v <= n){
for (int i = 0; i < g[v].size(); i++){
int to = g[v][i];
if (to == p)
ans -= sz[v]*(sz[v]-1)*((ll)cmp[to-n-1].size()-1); else
ans -= (n-sz[to])*(n-sz[to]-1)*((ll)cmp[to-n-1].size()-1);
}
}
for (int i = 0; i < g[v].size(); i++){
int to = g[v][i];
if (to == p)
continue;
dfs2(to, v, n);
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
//freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
cin >> n >> m;
for (int i = 1; i <= m; i++){
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++){
if (!used[i]){
findCutpoints(i, 0);
if (!stck.empty()){
cmp.push_back(stck);
stck.clear();
}
}
}
for (int i = 1; i <= n; i++)
g[i].clear();
for (int i = 0; i < cmp.size(); i++){
for (int j = 0; j < cmp[i].size(); j++){
g[cmp[i][j]].push_back(n+i+1);
g[n+i+1].push_back(cmp[i][j]);
}
}
memset(used, 0, sizeof(used));
for (int i = 1; i <= n; i++){
if (!used[i]){
dfs1(i, 0);
ans += sz[i]*(sz[i]-1)*(sz[i]-2);
dfs2(i, 0, sz[i]);
}
}
cout << ans;
}
/**
5 6
1 2
2 3
3 1
2 4
4 5
2 5
4 4
1 2
2 3
3 4
4 2
*/
Compilation message (stderr)
count_triplets.cpp: In function 'void findCutpoints(int, int)':
count_triplets.cpp:27:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < g[v].size(); i++){
~~^~~~~~~~~~~~~
count_triplets.cpp:26:9: warning: unused variable 'sons' [-Wunused-variable]
int sons = 0;
^~~~
count_triplets.cpp: In function 'void dfs1(int, int)':
count_triplets.cpp:54:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < g[v].size(); i++){
~~^~~~~~~~~~~~~
count_triplets.cpp: In function 'void dfs2(int, int, ll)':
count_triplets.cpp:64:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < g[v].size(); i++){
~~^~~~~~~~~~~~~
count_triplets.cpp:71:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < g[v].size(); i++){
~~^~~~~~~~~~~~~
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:101:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < cmp.size(); i++){
~~^~~~~~~~~~~~
count_triplets.cpp:102:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < cmp[i].size(); j++){
~~^~~~~~~~~~~~~~~
# | 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... |