#include <bits/stdc++.h>
using namespace std;
int n, m;
typedef vector<int> vi;
typedef pair<int,int> ii;
struct node{
int depth = -1;
int low;
int parent;
bool vis = false;
bool isArti = false;
vi adj;
};
vector<unordered_set<int> > biconnected;
stack<int> s; ///this is only needed for biconnected components
///note: this will ignore single nodes with no edges
static node g[200005];
int rootChildren = 0;
void dfs(int u){
if(g[u].depth == -1) g[u].depth = g[g[u].parent].depth + 1;
g[u].low = g[u].depth;
g[u].vis = true;
for(int i = 0;i < g[u].adj.size();i++){
int v = g[u].adj[i];
if(!g[v].vis){
g[v].parent = u;
if(g[u].depth == 0) rootChildren++;
s.push(u); ///s traces the path of tree traversal
dfs(v);
g[u].low = min(g[u].low,g[v].low);
if(g[v].low >= g[u].depth){
g[u].isArti = true;
g[u].low = min(g[u].low,g[v].low);
unordered_set<int> newSet;
int nn;
biconnected.push_back(newSet); ///create new biconnected component
do{
assert(!s.empty());
nn = s.top();
s.pop();
biconnected.back().insert(nn);
}while(nn != u);
}
///if(g[v].low > g[u].depth{
///u,v is a bridge
///}
}
else if(v != g[u].parent){
g[u].low = min(g[u].low,g[v].low);
}
s.push(u);
}
}
struct node2{
long long sz;
long long dp = 0;
bool isArti = false;
bool vis = false;
int r;
int p = -1;
vi adj;
};
vector<node2> bct; ///block-cut tree
long long ans = 0ll;
long long dp(int u, int p){
bct[u].dp = bct[u].sz;
for(int j = 0;j < bct[u].adj.size();j++){
int v = bct[u].adj[j];
if(v != p)
bct[u].dp += dp(v,u);
}
return bct[u].dp;
}
void countWays(int u, long long total){
long long sz = bct[u].sz;
if(bct[u].vis) return;
bct[u].vis = true;
if(!bct[u].isArti){
ans += sz * (sz - 1) * (sz + bct[u].adj.size() - 2);
ans += 2ll * sz * (sz + bct[u].adj.size() - 2) * (total - sz);
}
long long others = bct[bct[u].r].dp - bct[u].dp;
for(int j = 0;j < bct[u].adj.size();j++){
int v = bct[u].adj[j];
if(!bct[v].vis){
countWays(v,total);
long long extra = 0ll;
if(!bct[u].isArti) extra = bct[u].adj.size()-2;
ans += 2ll * (bct[u].sz + extra) * bct[v].dp * others;
others += bct[v].dp;
}
}
}
int main()
{
//freopen("i.txt","r",stdin);
cin >> n >> m;
ii edges[m];
for(int i = 0;i < m;i++){
int a, b;
cin >> a >> b;
a--;
b--;
g[a].adj.push_back(b);
g[b].adj.push_back(a);
edges[i] = ii(a,b);
}
for(int i = 0;i < n;i++){
if(!g[i].vis){
rootChildren = 0;
g[i].depth = 0;
dfs(i);
if(rootChildren > 1) g[i].isArti = true;
else g[i].isArti = false; ///this line is needed
}
}
unordered_map<int,int> artis;
for(int i = 0;i < n;i++){
if(g[i].isArti){
artis[i] = bct.size();
node2 nn;
nn.sz = 1;
nn.isArti = true;
bct.push_back(nn);
}
}
for(int i = 0;i < biconnected.size();i++){
node2 nn;
nn.sz = biconnected[i].size();
nn.isArti = false;
for(unordered_set<int>::iterator it = biconnected[i].begin();it != biconnected[i].end();it++){
//cout << *it << " ";
if(artis.find(*it) != artis.end()){
nn.adj.push_back(artis[*it]);
bct[artis[*it]].adj.push_back(bct.size());
nn.sz--;
}
}
bct.push_back(nn);
//cout << "\n";
}
for(int i = 0;i < bct.size();i++){
//root = i;
//dp(i);
countWays(i,dp(i,-1));
}
/*
long long ans = 0ll;
for(int i = 0;i < bct.size();i++){
long long total = bct[bct[i].r].dp - bct[i].sz;
long long sz = bct[i].sz;
if(!bct[i].isArti){
ans += sz * (sz - 1) * (sz + bct[i].adj.size() - 2);
ans += 2ll * sz * (sz + bct[i].adj.size() - 2) * total;
}
long long others = bct[bct[i].r].dp - bct[i].dp;
for(int j = 0;j < bct[i].adj.size();j++){
int v = bct[i].adj[j];
if(v != bct[i].p){
long long extra = 0ll;
if(!bct[i].isArti) extra = bct[i].adj.size()-2;
ans += 2ll * (bct[i].sz + extra) * bct[v].dp * others;
others += bct[v].dp;
}
}
//if(!bct[i].isArti) ans += (bct[i].sz) * (bct[i].sz - 1) * (bct[i].adj.size());
//cout << i << " " << ans << "\n";
}
*/
cout << ans;
}
/*
8 7
8 5
8 1
7 5
8 6
3 1
3 2
8 3
*/
/*
10 12
6 4
10 3
4 2
4 1
4 3
6 5
9 1
9 4
2 1
9 3
8 5
7 2
*/
Compilation message
count_triplets.cpp: In function 'void dfs(int)':
count_triplets.cpp:25:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i < g[u].adj.size();i++){
~~^~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'long long int dp(int, int)':
count_triplets.cpp:71:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0;j < bct[u].adj.size();j++){
~~^~~~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'void countWays(int, long long int)':
count_triplets.cpp:87:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0;j < bct[u].adj.size();j++){
~~^~~~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:134:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i < biconnected.size();i++){
~~^~~~~~~~~~~~~~~~~~~~
count_triplets.cpp:151:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i < bct.size();i++){
~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
8192 KB |
Output is correct |
2 |
Correct |
9 ms |
8192 KB |
Output is correct |
3 |
Correct |
10 ms |
8192 KB |
Output is correct |
4 |
Correct |
9 ms |
8192 KB |
Output is correct |
5 |
Correct |
9 ms |
8192 KB |
Output is correct |
6 |
Correct |
8 ms |
8192 KB |
Output is correct |
7 |
Runtime error |
18 ms |
16120 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
8192 KB |
Output is correct |
2 |
Correct |
9 ms |
8192 KB |
Output is correct |
3 |
Correct |
10 ms |
8192 KB |
Output is correct |
4 |
Correct |
9 ms |
8192 KB |
Output is correct |
5 |
Correct |
9 ms |
8192 KB |
Output is correct |
6 |
Correct |
8 ms |
8192 KB |
Output is correct |
7 |
Runtime error |
18 ms |
16120 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
218 ms |
33384 KB |
Output is correct |
2 |
Correct |
229 ms |
33544 KB |
Output is correct |
3 |
Execution timed out |
1071 ms |
41480 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
21 ms |
16896 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1065 ms |
49652 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
21 ms |
16896 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1064 ms |
49720 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
8192 KB |
Output is correct |
2 |
Correct |
9 ms |
8192 KB |
Output is correct |
3 |
Correct |
10 ms |
8192 KB |
Output is correct |
4 |
Correct |
9 ms |
8192 KB |
Output is correct |
5 |
Correct |
9 ms |
8192 KB |
Output is correct |
6 |
Correct |
8 ms |
8192 KB |
Output is correct |
7 |
Runtime error |
18 ms |
16120 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
8192 KB |
Output is correct |
2 |
Correct |
9 ms |
8192 KB |
Output is correct |
3 |
Correct |
10 ms |
8192 KB |
Output is correct |
4 |
Correct |
9 ms |
8192 KB |
Output is correct |
5 |
Correct |
9 ms |
8192 KB |
Output is correct |
6 |
Correct |
8 ms |
8192 KB |
Output is correct |
7 |
Runtime error |
18 ms |
16120 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |