-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicnic.cpp
53 lines (42 loc) · 1.28 KB
/
picnic.cpp
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
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;
bool isAllChecked(vector<bool>& check) {
for (auto p : check) {
if (!p) return false;
}
return true;
}
int recursion(int n, vector<bool> check, vector<pair<int, int>>& pairedId) {
if (isAllChecked(check)) return 1;
int result = 0;
for (int i = n; i < pairedId.size(); i++) {
if (check[pairedId[i].first] || check[pairedId[i].second]) continue;
check[pairedId[i].first] = true;
check[pairedId[i].second] = true;
result += recursion(i, check, pairedId);
check[pairedId[i].first] = false;
check[pairedId[i].second] = false;
}
return result;
}
int main()
{
int numTestCase, students, pairs;
cin >> numTestCase;
for (int i = 0; i < numTestCase; i++) {
vector<pair<int, int>> pairedId;
cin >> students >> pairs;
vector<bool> check(students, 0);
for (int j = 0; j < pairs; j++) {
int pairX, pairY;
cin >> pairX >> pairY;
if (pairX > pairY) swap(pairX, pairY);
pairedId.push_back(make_pair(pairX, pairY));
}
cout << recursion(0, check, pairedId) << endl;
}
return 0;
}