#include <bits/stdc++.h>
using namespace std;
int n, m;
int x[10000];
int y[10000];
vector < int > E[10000];
bool used[10000];
int low[10000];
int in_ord_arr[10000];
int parent[10000];
int in_ord;
long long ans;
void dfs(int vertex){
in_ord++;
in_ord_arr[vertex] = in_ord;
low[vertex] = in_ord;
used[vertex] = true;
bool back = false;
for(int v : E[vertex]){
if(!used[v]){
parent[v] = vertex;
dfs(v);
low[vertex] = min(low[vertex], low[v]);
}
else{
if(v != parent[vertex] or back){
low[vertex] = min(low[vertex], in_ord_arr[v]);
}
else{
back = true;
}
}
}
//cout << "vertex = " << vertex << " low = " << low[vertex] << endl;
if(low[vertex] == in_ord_arr[vertex] and vertex != 1){
ans ++;
}
}
int main(){
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < m; ++i)
{
cin >> x[i] >> y[i];
}
for (int t = 0; t < m; ++t)
{
E[x[t]].push_back(y[t]);
E[y[t]].push_back(x[t]);
}
//dfs(1);
//cout << ans << endl;
//return 0;
long long total_ans = 0;
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < m; ++j)
{
if(i == j) continue;
for (int t = 0; t <= n; ++t)
{
E[t].clear();
in_ord_arr[t] = 0;
in_ord = 0;
ans = 0;
used[t] = false;
low[t] = 0;
parent[t] = -1;
}
for (int t = 0; t < m; ++t)
{
if(t != i and t != j){
E[x[t]].push_back(y[t]);
E[y[t]].push_back(x[t]);
}
}
dfs(1);
if(in_ord != n){
//cout << x[i] << "-" << y[i] << " and " << x[j] << "-" << y[j] << endl;
total_ans += m - 2;
}
else{
total_ans += ans;
}
}
}
cout << total_ans/6 << endl;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | #include <bits/stdc++.h> using namespace std; int n, m; int x[10000]; int y[10000]; vector < int > E[10000]; bool used[10000]; int low[10000]; int in_ord_arr[10000]; int parent[10000]; int in_ord; long long ans; void dfs(int vertex){ in_ord++; in_ord_arr[vertex] = in_ord; low[vertex] = in_ord; used[vertex] = true; bool back = false; for(int v : E[vertex]){ if(!used[v]){ parent[v] = vertex; dfs(v); low[vertex] = min(low[vertex], low[v]); } else{ if(v != parent[vertex] or back){ low[vertex] = min(low[vertex], in_ord_arr[v]); } else{ back = true; } } } //cout << "vertex = " << vertex << " low = " << low[vertex] << endl; if(low[vertex] == in_ord_arr[vertex] and vertex != 1){ ans ++; } } int main(){ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> x[i] >> y[i]; } for (int t = 0; t < m; ++t) { E[x[t]].push_back(y[t]); E[y[t]].push_back(x[t]); } //dfs(1); //cout << ans << endl; //return 0; long long total_ans = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { if(i == j) continue; for (int t = 0; t <= n; ++t) { E[t].clear(); in_ord_arr[t] = 0; in_ord = 0; ans = 0; used[t] = false; low[t] = 0; parent[t] = -1; } for (int t = 0; t < m; ++t) { if(t != i and t != j){ E[x[t]].push_back(y[t]); E[y[t]].push_back(x[t]); } } dfs(1); if(in_ord != n){ //cout << x[i] << "-" << y[i] << " and " << x[j] << "-" << y[j] << endl; total_ans += m - 2; } else{ total_ans += ans; } } } cout << total_ans/6 << endl; } |
English