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
| #include <iostream> #include <vector> using namespace std; int n,ans; bool visb[30],viss[30];
vector<pair<int,int> > vec; void dfs(int x){ if(x==n+1){ ans++; return; } for(int i=0;i<n;i++){ if((vec[i].first==x||vec[i].second==x) && !visb[x] && !viss[i]){ visb[x] = true; viss[i] = true; dfs(x+1); viss[i] = false; visb[x] = false; } } } int main() { cin >> n; for(int i=0;i<n;i++){ int x,y; cin >> x >> y; vec.push_back({x,y}); } if(n!=0) dfs(1); cout << ans << endl; return 0; }
|