Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using std::vector;
- bool Bit(int mask, int pos) {
- return ((mask >> pos) & 1);
- }
- int main() {
- std::ios_base::sync_with_stdio(false);
- std::cin.tie(0);
- std::cout.tie(0);
- int n;
- std::cin >> n;
- vector<vector<bool>> can_work_together(n, vector<bool>(n));
- for (int i = 0; i < n; ++i) {
- std::string str;
- std::cin >> str;
- for (int j = 0; j < n; ++j) {
- can_work_together[i][j] = (str[j] == 'Y');
- }
- }
- vector<bool> dp(1 << n, false);
- dp[0] = true;
- for (int mask = 1; mask < (1 << n); ++mask) {
- for (int pos = 0; pos < n; ++pos) {
- if (Bit(mask, pos)) {
- for (int neighbour = 0; neighbour < n; ++neighbour) {
- if (can_work_together[pos][neighbour] and Bit(mask, neighbour)
- and dp[mask xor ((1 << pos) | (1 << neighbour))]) {
- dp[mask] = true;
- }
- }
- }
- }
- }
- int max_cnt_of_one = 0;
- for (int mask = 0; mask < (1 << n); ++mask) {
- int temp_cnt_of_one = 0;
- if (dp[mask]) {
- for (int i = 0; i < n; ++i) {
- if (Bit(mask, i)) {
- ++temp_cnt_of_one;
- }
- }
- max_cnt_of_one = std::max(max_cnt_of_one, temp_cnt_of_one);
- }
- }
- std::cout << max_cnt_of_one << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement