// Marcin Knapik // Potyczki algorytmiczne - dzień 5 zadanie A1 #pragma GCC optimize "O3" #include<bits/stdc++.h> using namespace std; #define f first #define s second #define sz(s) (int)s.size() #define all(s) s.begin(), s.begin() #define pb push_back #define mp make_pair #define ii pair<int, int> #define ll long long #define vi vector<int> #define vii vector<ii> #define vvi vector<vi> int rep[1000007]; int Find(int x){ return x == rep[x] ? x : rep[x] = Find(rep[x]); } void Union(int a, int b){ a = Find(a); b = Find(b); rep[a] = b; } void dfs(int start, int przodek, int & pre, vi & low, vi & preorder, ll & ans, vvi & G){ preorder[start] = pre++; low[start] = preorder[start]; bool pierwszy = 1; for(auto & u : G[start]){ if(u == przodek and pierwszy){ pierwszy = false; continue; } if(preorder[u]) low[start] = min(low[start], preorder[u]); else{ dfs(u, start, pre, low, preorder, ans, G); low[start] = min(low[start], low[u]); } } if(przodek != -1 and low[start] == preorder[start]) ans++; } int main(){ ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vii kraw; for(int i = 0; i < m; i++){ int a, b; cin >> a >> b; kraw.pb({a, b}); } ll ans = 0; for(int i = 0; i < m; i++) for(int j = i + 1; j < m; j++){ for(int k = 1; k <= n; k++) rep[k] = k; vvi G(n + 1); for(int k = 0; k < m; k++) if(i != k and j != k){ Union(kraw[k].f, kraw[k].s); G[kraw[k].f].pb(kraw[k].s); G[kraw[k].s].pb(kraw[k].f); } bool spoojny = 1; for(int k = 1; k < n; k++) spoojny &= Find(k) == Find(k + 1); if(!spoojny) ans += m - 2; else{ vi low(n + 1); vi preorder(n + 1); int pre = 1; dfs(1, -1, pre, low, preorder, ans, G); } } cout << ans / 3 << '\n'; }
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 | // Marcin Knapik // Potyczki algorytmiczne - dzień 5 zadanie A1 #pragma GCC optimize "O3" #include<bits/stdc++.h> using namespace std; #define f first #define s second #define sz(s) (int)s.size() #define all(s) s.begin(), s.begin() #define pb push_back #define mp make_pair #define ii pair<int, int> #define ll long long #define vi vector<int> #define vii vector<ii> #define vvi vector<vi> int rep[1000007]; int Find(int x){ return x == rep[x] ? x : rep[x] = Find(rep[x]); } void Union(int a, int b){ a = Find(a); b = Find(b); rep[a] = b; } void dfs(int start, int przodek, int & pre, vi & low, vi & preorder, ll & ans, vvi & G){ preorder[start] = pre++; low[start] = preorder[start]; bool pierwszy = 1; for(auto & u : G[start]){ if(u == przodek and pierwszy){ pierwszy = false; continue; } if(preorder[u]) low[start] = min(low[start], preorder[u]); else{ dfs(u, start, pre, low, preorder, ans, G); low[start] = min(low[start], low[u]); } } if(przodek != -1 and low[start] == preorder[start]) ans++; } int main(){ ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vii kraw; for(int i = 0; i < m; i++){ int a, b; cin >> a >> b; kraw.pb({a, b}); } ll ans = 0; for(int i = 0; i < m; i++) for(int j = i + 1; j < m; j++){ for(int k = 1; k <= n; k++) rep[k] = k; vvi G(n + 1); for(int k = 0; k < m; k++) if(i != k and j != k){ Union(kraw[k].f, kraw[k].s); G[kraw[k].f].pb(kraw[k].s); G[kraw[k].s].pb(kraw[k].f); } bool spoojny = 1; for(int k = 1; k < n; k++) spoojny &= Find(k) == Find(k + 1); if(!spoojny) ans += m - 2; else{ vi low(n + 1); vi preorder(n + 1); int pre = 1; dfs(1, -1, pre, low, preorder, ans, G); } } cout << ans / 3 << '\n'; } |