#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <map> using namespace std; int vis[10000]; vector<int> v[10000]; map<pair<int, int>, int> S; void dfs(int u) { vis[u] = 1; for (int i = 0; i < v[u].size(); i++) { int v1 = v[u][i]; auto it = S.find({u, v1}); if (!vis[v1] && it->second) { dfs(v1); } } } int main() { int n, m; scanf("%d %d", &n, &m); vector<pair<int, int>> edges; for (int i = 0; i < m; i++) { int x, y; scanf("%d %d", &x, &y); --x, --y; auto it = S.find({x, y}); auto it2 = S.find({y, x}); if (it == S.end()) { v[x].push_back(y); v[y].push_back(x); S.insert(pair<pair<int, int>, int>({x, y}, 1)); S.insert(pair<pair<int, int>, int>({y, x}, 1)); } else { it->second++; it2->second++; } edges.push_back({x, y}); edges.push_back({y, x}); } int ret = 0; for (int i = 0; i < m; i++) { auto it = S.find(edges[2 * i]); auto it2 = S.find(edges[2 * i + 1]); it->second--; it2->second--; for (int j = i + 1; j < m; j++) { auto it = S.find(edges[2 * j]); auto it2 = S.find(edges[2 * j + 1]); it->second--; it2->second--; for (int k = j + 1; k < m; k++) { auto it = S.find(edges[2 * k]); auto it2 = S.find(edges[2 * k + 1]); it->second--; it2->second--; memset(vis, 0, 4 * n); int skl = 0; for (int l = 0; l < n; l++) { if (!vis[l]) { dfs(l); skl++; } } if (skl > 1) { ret++; } it->second++; it2->second++; } it->second++; it2->second++; } it->second++; it2->second++; } printf("%d\n", ret); return 0; }
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 107 108 | #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <map> using namespace std; int vis[10000]; vector<int> v[10000]; map<pair<int, int>, int> S; void dfs(int u) { vis[u] = 1; for (int i = 0; i < v[u].size(); i++) { int v1 = v[u][i]; auto it = S.find({u, v1}); if (!vis[v1] && it->second) { dfs(v1); } } } int main() { int n, m; scanf("%d %d", &n, &m); vector<pair<int, int>> edges; for (int i = 0; i < m; i++) { int x, y; scanf("%d %d", &x, &y); --x, --y; auto it = S.find({x, y}); auto it2 = S.find({y, x}); if (it == S.end()) { v[x].push_back(y); v[y].push_back(x); S.insert(pair<pair<int, int>, int>({x, y}, 1)); S.insert(pair<pair<int, int>, int>({y, x}, 1)); } else { it->second++; it2->second++; } edges.push_back({x, y}); edges.push_back({y, x}); } int ret = 0; for (int i = 0; i < m; i++) { auto it = S.find(edges[2 * i]); auto it2 = S.find(edges[2 * i + 1]); it->second--; it2->second--; for (int j = i + 1; j < m; j++) { auto it = S.find(edges[2 * j]); auto it2 = S.find(edges[2 * j + 1]); it->second--; it2->second--; for (int k = j + 1; k < m; k++) { auto it = S.find(edges[2 * k]); auto it2 = S.find(edges[2 * k + 1]); it->second--; it2->second--; memset(vis, 0, 4 * n); int skl = 0; for (int l = 0; l < n; l++) { if (!vis[l]) { dfs(l); skl++; } } if (skl > 1) { ret++; } it->second++; it2->second++; } it->second++; it2->second++; } it->second++; it2->second++; } printf("%d\n", ret); return 0; } |