#include <bits/stdc++.h> #define PB push_back #define ST first #define ND second #define _ ios_base::sync_with_stdio(0); cin.tie(0); //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const int nax = 500 * 1000 + 10; int n, m; bool in_tree[nax], visited[nax]; int d[nax], low[nax], nr; vector<pi> V[nax]; pi e[nax]; int cnt; void dfs(int x) { visited[x] = 1; d[x] = nr++; low[x] = d[x]; for(auto nbh : V[x]) { if(!visited[nbh.ST]) { in_tree[nbh.ND] = 1; dfs(nbh.ST); low[x] = min(low[x], low[nbh.ST]); } else if(!in_tree[nbh.ND]) { low[x] = min(low[x], d[nbh.ST]); } } if(low[x] >= d[x] && x != 1) { cnt++; } } int main() {_ cin >> n >> m; for(int a,b,i = 1; i <= m; ++i) { cin >> a >> b; e[i] = {a, b}; } ll ans = 0; for(int i = 1; i <= m; ++i) { for(int j = 1; j <= m; ++j) { if(i == j) continue; for(int k = 1; k <= n; ++k) V[k].clear(); for(int k = 1; k <= m; ++k) { if(k != i && k != j) { V[e[k].ST].emplace_back(e[k].ND, k); V[e[k].ND].emplace_back(e[k].ST, k); } in_tree[k] = 0; } nr = 0; cnt = 0; for(int k = 1; k <= n; ++k) { visited[k] = 0; } dfs(1); bool con = 1; for(int k = 1; k <= n; ++k) { if(!visited[k]) con = 0; } //~ cout << i << " " << j << " " << con << " " << cnt << "\n"; if(!con) ans += m-2; else ans += cnt; } } cout << ans/6; }
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 | #include <bits/stdc++.h> #define PB push_back #define ST first #define ND second #define _ ios_base::sync_with_stdio(0); cin.tie(0); //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const int nax = 500 * 1000 + 10; int n, m; bool in_tree[nax], visited[nax]; int d[nax], low[nax], nr; vector<pi> V[nax]; pi e[nax]; int cnt; void dfs(int x) { visited[x] = 1; d[x] = nr++; low[x] = d[x]; for(auto nbh : V[x]) { if(!visited[nbh.ST]) { in_tree[nbh.ND] = 1; dfs(nbh.ST); low[x] = min(low[x], low[nbh.ST]); } else if(!in_tree[nbh.ND]) { low[x] = min(low[x], d[nbh.ST]); } } if(low[x] >= d[x] && x != 1) { cnt++; } } int main() {_ cin >> n >> m; for(int a,b,i = 1; i <= m; ++i) { cin >> a >> b; e[i] = {a, b}; } ll ans = 0; for(int i = 1; i <= m; ++i) { for(int j = 1; j <= m; ++j) { if(i == j) continue; for(int k = 1; k <= n; ++k) V[k].clear(); for(int k = 1; k <= m; ++k) { if(k != i && k != j) { V[e[k].ST].emplace_back(e[k].ND, k); V[e[k].ND].emplace_back(e[k].ST, k); } in_tree[k] = 0; } nr = 0; cnt = 0; for(int k = 1; k <= n; ++k) { visited[k] = 0; } dfs(1); bool con = 1; for(int k = 1; k <= n; ++k) { if(!visited[k]) con = 0; } //~ cout << i << " " << j << " " << con << " " << cnt << "\n"; if(!con) ans += m-2; else ans += cnt; } } cout << ans/6; } |