#include <iostream> #include <vector> #include <functional> #include <algorithm> using namespace std; struct Edge{ int tar,id; }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n,m; cin >> n >> m; vector<vector<Edge>>G(n+1); for(int i=0;i<m;i++){ int a,b; cin >> a >> b; G[a].push_back({b,i}); G[b].push_back({a,i}); } vector<bool>Visited(n+1, false); int i1,i2,i3; function<int(int)> dfs = [&](int v){ Visited[v]=true; int result=1; for(Edge e : G[v]) if(!Visited[e.tar] && e.id!=i1 && e.id!=i2 && e.id!=i3) result += dfs(e.tar); return result; }; long long result=0; for(i1=0;i1<m;i1++) for(i2=i1+1;i2<m;i2++) for(i3=i2+1;i3<m;i3++){ if(dfs(1)<n) result++; fill(Visited.begin(), Visited.end(), false); } cout << result << "\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 | #include <iostream> #include <vector> #include <functional> #include <algorithm> using namespace std; struct Edge{ int tar,id; }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n,m; cin >> n >> m; vector<vector<Edge>>G(n+1); for(int i=0;i<m;i++){ int a,b; cin >> a >> b; G[a].push_back({b,i}); G[b].push_back({a,i}); } vector<bool>Visited(n+1, false); int i1,i2,i3; function<int(int)> dfs = [&](int v){ Visited[v]=true; int result=1; for(Edge e : G[v]) if(!Visited[e.tar] && e.id!=i1 && e.id!=i2 && e.id!=i3) result += dfs(e.tar); return result; }; long long result=0; for(i1=0;i1<m;i1++) for(i2=i1+1;i2<m;i2++) for(i3=i2+1;i3<m;i3++){ if(dfs(1)<n) result++; fill(Visited.begin(), Visited.end(), false); } cout << result << "\n"; return 0; } |