|
| 1 | +/* |
| 2 | + |
| 3 | +time: O() |
| 4 | +space: O() |
| 5 | +
|
| 6 | +Runtime: 149 ms, faster than 50.41% of C++ online submissions for Parallel Courses II. |
| 7 | +Memory Usage: 37.1 MB, less than 17.48% of C++ online submissions for Parallel Courses II. |
| 8 | +
|
| 9 | +bfs |
| 10 | +*/ |
| 11 | + |
| 12 | +#include <bits/stdc++.h> |
| 13 | + |
| 14 | +using namespace std; |
| 15 | + |
| 16 | +#define all(x) begin(x), end(x) |
| 17 | +typedef vector<int> vi; |
| 18 | +typedef vector<vector<int>> vvi; |
| 19 | + |
| 20 | +typedef int bint; |
| 21 | + |
| 22 | +class Solution { |
| 23 | +public: |
| 24 | + bint rel[16] = {}; |
| 25 | + int required[16] = {}; |
| 26 | + int fulfilled[16]; |
| 27 | + int k; |
| 28 | + int n; |
| 29 | + int minNumberOfSemesters(int course_count, vector<vector<int>>& relations, int max_to_take) { |
| 30 | + k = max_to_take; |
| 31 | + n = course_count; |
| 32 | + /// n=2, toTakeNextTerm = 0x...110 |
| 33 | + // can_take_first_term = (1<<(n+1)) - 2; |
| 34 | + for (auto & relation : relations){ |
| 35 | + rel[relation[1]] |= 1<<relation[0]; |
| 36 | + ++required[relation[1]]; |
| 37 | + // can_take_first_term &= ~1<<relation[1]; |
| 38 | + } |
| 39 | + |
| 40 | + vi takens = {0}; |
| 41 | + bfs(takens); |
| 42 | + |
| 43 | + return term_count; |
| 44 | + } |
| 45 | + |
| 46 | + int term_count = 0; |
| 47 | + int seen[65536] = {}; |
| 48 | + void bfs(vi & takens){ |
| 49 | + vi to_takes; |
| 50 | + for (auto & taken : takens){ |
| 51 | + /// get course can be taken this term according to taken |
| 52 | + auto can_take_this_term = get_can_take_this_term(taken); |
| 53 | + if (can_take_this_term == 0){ |
| 54 | + return; |
| 55 | + } |
| 56 | + /// select k courses from can_take_this_term |
| 57 | + auto selects_this_term = get_selects_this_term(can_take_this_term); |
| 58 | + for (auto & new_taken : selects_this_term){ |
| 59 | + int to_take = taken | new_taken; |
| 60 | + if (!seen[to_take]){ |
| 61 | + seen[to_take] = 1; |
| 62 | + to_takes.push_back(to_take); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + ++term_count; |
| 67 | + bfs(to_takes); |
| 68 | + } |
| 69 | + |
| 70 | + /// O(n2) |
| 71 | + inline int get_can_take_this_term(int & taken){ |
| 72 | + memset(fulfilled+1, 0, 4*n); |
| 73 | + bint can_take_this_term = 0; |
| 74 | + /// O(n2) |
| 75 | + for (int j = 1; j<=n; ++j){ |
| 76 | + // bint fulfilled_j = rel[j] & taken; |
| 77 | + // while (fulfilled_j!=0){ |
| 78 | + // if (fulfilled_j & 1) ++fulfilled[j]; |
| 79 | + // fulfilled_j >>= 1; |
| 80 | + // } |
| 81 | + fulfilled[j] = __builtin_popcount(rel[j] & taken); |
| 82 | + } |
| 83 | + for (int i=1; i<=n; ++i){ |
| 84 | + if (fulfilled[i] >= required[i]){ |
| 85 | + can_take_this_term |= 1<<i; |
| 86 | + } |
| 87 | + } |
| 88 | + can_take_this_term &= ~taken; |
| 89 | + return can_take_this_term; |
| 90 | + } |
| 91 | + |
| 92 | + inline vi get_selects_this_term(int & can_take_this_term){ |
| 93 | + // int cnt_can_take_this_term = 0; |
| 94 | + // int tmp = can_take_this_term; |
| 95 | + // while (tmp!=0){ |
| 96 | + // if (tmp & 1) ++cnt_can_take_this_term; |
| 97 | + // tmp >>= 1; |
| 98 | + // } |
| 99 | + // if (cnt_can_take_this_term<=k) return {can_take_this_term}; |
| 100 | + if (__builtin_popcount(can_take_this_term)<=k) return {can_take_this_term}; |
| 101 | + |
| 102 | + vi pos_1; |
| 103 | + pos_1.reserve(n); |
| 104 | + for (int i=0; can_take_this_term!=0; ++i){ |
| 105 | + if (can_take_this_term & 1) pos_1.push_back(i); |
| 106 | + can_take_this_term >>= 1; |
| 107 | + } |
| 108 | + int N = pos_1.size(); |
| 109 | + |
| 110 | + string bitmask(k, 1); |
| 111 | + bitmask.resize(N, 0); |
| 112 | + |
| 113 | + vi res; |
| 114 | + do { |
| 115 | + int select = 0; |
| 116 | + for (int i = 0; i < N; ++i) // [0..N-1] integers |
| 117 | + { |
| 118 | + if (bitmask[i]) { |
| 119 | + select |= 1<<pos_1[i]; |
| 120 | + } |
| 121 | + } |
| 122 | + res.push_back(select); |
| 123 | + } while (std::prev_permutation(bitmask.begin(), bitmask.end())); |
| 124 | + |
| 125 | + return res; |
| 126 | + } |
| 127 | +}; |
| 128 | + |
| 129 | +const static auto initialize = [] { |
| 130 | + std::ios::sync_with_stdio(false); |
| 131 | + std::cin.tie(nullptr); |
| 132 | + std::cout.tie(nullptr); |
| 133 | + return nullptr; |
| 134 | +}(); |
0 commit comments