#include <vector> #include <iostream> struct Order { int a; int b; }; class Solution { public: Solution(int aN, int aK, const std::vector<Order>& aOrders) : n(aN) , k(aK) , orders(aOrders) , result{0} {} int run() { std::vector<bool> soldiers; soldiers.push_back(false); check(soldiers, 0, true); check(soldiers, 0, false); return result; } void check(std::vector<bool> soldiers, int numberOfPlaced, bool next) { soldiers.push_back(next); if (next) { numberOfPlaced++; } if (soldiers.size() > n + 1 || numberOfPlaced > k) { return; } if (soldiers.size() == n + 1) { if (numberOfPlaced == k) { if (checkCombination(soldiers)) { result++; result = result % 2; } } return; } check(soldiers, numberOfPlaced, true); check(soldiers, numberOfPlaced, false); } bool checkCombination(std::vector<bool> soldiers) { for (auto& order : orders) { if (soldiers[order.a] && (!soldiers[order.b])) { soldiers[order.a] = false; soldiers[order.b] = true; } } bool rangeStarted{false}; bool rangeFinished{false}; for (int i = 1; i < soldiers.size(); i++) { if (!rangeStarted && soldiers[i]) { rangeStarted = true; } if (rangeStarted && (!soldiers[i])) { rangeFinished = true; } if (rangeFinished && soldiers[i]) { return false; } } return true; } int n; int k; const std::vector<Order>& orders; int result; }; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n, m; std::cin >> n >> m; std::vector<Order> orders; orders.reserve(m); for (int i = 0; i < m; i++) { Order order; std::cin >> order.a >> order.b; orders.push_back(order); } for (int k = 1; k <= n; k++) { Solution solution(n, k, orders); int result = solution.run(); std::cout << result << (k < n ? " " : ""); } std::cout << "\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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #include <vector> #include <iostream> struct Order { int a; int b; }; class Solution { public: Solution(int aN, int aK, const std::vector<Order>& aOrders) : n(aN) , k(aK) , orders(aOrders) , result{0} {} int run() { std::vector<bool> soldiers; soldiers.push_back(false); check(soldiers, 0, true); check(soldiers, 0, false); return result; } void check(std::vector<bool> soldiers, int numberOfPlaced, bool next) { soldiers.push_back(next); if (next) { numberOfPlaced++; } if (soldiers.size() > n + 1 || numberOfPlaced > k) { return; } if (soldiers.size() == n + 1) { if (numberOfPlaced == k) { if (checkCombination(soldiers)) { result++; result = result % 2; } } return; } check(soldiers, numberOfPlaced, true); check(soldiers, numberOfPlaced, false); } bool checkCombination(std::vector<bool> soldiers) { for (auto& order : orders) { if (soldiers[order.a] && (!soldiers[order.b])) { soldiers[order.a] = false; soldiers[order.b] = true; } } bool rangeStarted{false}; bool rangeFinished{false}; for (int i = 1; i < soldiers.size(); i++) { if (!rangeStarted && soldiers[i]) { rangeStarted = true; } if (rangeStarted && (!soldiers[i])) { rangeFinished = true; } if (rangeFinished && soldiers[i]) { return false; } } return true; } int n; int k; const std::vector<Order>& orders; int result; }; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n, m; std::cin >> n >> m; std::vector<Order> orders; orders.reserve(m); for (int i = 0; i < m; i++) { Order order; std::cin >> order.a >> order.b; orders.push_back(order); } for (int k = 1; k <= n; k++) { Solution solution(n, k, orders); int result = solution.run(); std::cout << result << (k < n ? " " : ""); } std::cout << "\n"; return 0; } |