#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <deque> #include <climits> using namespace std; void input(int &n, int &m, vector<vector<int>> &G, vector<pair<int, int>> &roads, vector<vector<int>> &road_cnt) { int a, b; cin >> n >> m; G.resize(n + 1); road_cnt.resize(n + 1, vector<int>(n + 1, 0)); for (int i = 0; i < m; i++) { cin >> a >> b; roads.push_back({a, b}); road_cnt[a][b]++; road_cnt[b][a]++; G[a].push_back(b); G[b].push_back(a); } } void dfs(int x, int &k, vector<int> &visitted, vector<vector<int>> &G, int &counter, vector<vector<int>> &road_cnt) { if (visitted[x] == k) return; visitted[x] = k; counter++; for (int v : G[x]) if (road_cnt[x][v] > 0) dfs(v, k, visitted, G, counter, road_cnt); } void set_roads(vector<vector<int>> &road_cnt, vector<pair<int, int>> &roads, int &a, int &b, int &c, int val) { road_cnt[roads[a].first][roads[a].second] += val; road_cnt[roads[a].second][roads[a].first] += val; road_cnt[roads[b].first][roads[b].second] += val; road_cnt[roads[b].second][roads[b].first] += val; road_cnt[roads[c].first][roads[c].second] += val; road_cnt[roads[c].second][roads[c].first] += val; } int solve (int &n, int &m, vector<vector<int>> &G, vector<pair<int, int>> &roads, vector<vector<int>> &road_cnt) { vector<int> visitted(n + 1); int result = 0, k = 1; for (int a = 0; a < m; a++) { for (int b = a + 1; b < m; b++) { for (int c = b + 1; c < m; c++) { set_roads(road_cnt, roads, a, b, c, -1); int counter = 0; dfs(1, k, visitted, G, counter, road_cnt); k++; if (counter < n) result++; set_roads(road_cnt, roads, a, b, c, 1); } } } return result; } int main() { ios_base::sync_with_stdio(0); int n, m; vector<vector<int>> G; vector<pair<int, int>> roads; vector<vector<int>> road_cnt; input(n, m, G, roads, road_cnt); cout << solve(n, m, G, roads, road_cnt) << "\n"; 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 | #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <deque> #include <climits> using namespace std; void input(int &n, int &m, vector<vector<int>> &G, vector<pair<int, int>> &roads, vector<vector<int>> &road_cnt) { int a, b; cin >> n >> m; G.resize(n + 1); road_cnt.resize(n + 1, vector<int>(n + 1, 0)); for (int i = 0; i < m; i++) { cin >> a >> b; roads.push_back({a, b}); road_cnt[a][b]++; road_cnt[b][a]++; G[a].push_back(b); G[b].push_back(a); } } void dfs(int x, int &k, vector<int> &visitted, vector<vector<int>> &G, int &counter, vector<vector<int>> &road_cnt) { if (visitted[x] == k) return; visitted[x] = k; counter++; for (int v : G[x]) if (road_cnt[x][v] > 0) dfs(v, k, visitted, G, counter, road_cnt); } void set_roads(vector<vector<int>> &road_cnt, vector<pair<int, int>> &roads, int &a, int &b, int &c, int val) { road_cnt[roads[a].first][roads[a].second] += val; road_cnt[roads[a].second][roads[a].first] += val; road_cnt[roads[b].first][roads[b].second] += val; road_cnt[roads[b].second][roads[b].first] += val; road_cnt[roads[c].first][roads[c].second] += val; road_cnt[roads[c].second][roads[c].first] += val; } int solve (int &n, int &m, vector<vector<int>> &G, vector<pair<int, int>> &roads, vector<vector<int>> &road_cnt) { vector<int> visitted(n + 1); int result = 0, k = 1; for (int a = 0; a < m; a++) { for (int b = a + 1; b < m; b++) { for (int c = b + 1; c < m; c++) { set_roads(road_cnt, roads, a, b, c, -1); int counter = 0; dfs(1, k, visitted, G, counter, road_cnt); k++; if (counter < n) result++; set_roads(road_cnt, roads, a, b, c, 1); } } } return result; } int main() { ios_base::sync_with_stdio(0); int n, m; vector<vector<int>> G; vector<pair<int, int>> roads; vector<vector<int>> road_cnt; input(n, m, G, roads, road_cnt); cout << solve(n, m, G, roads, road_cnt) << "\n"; return 0; } |