Advertisement
den4ik2003

Untitled

Mar 22nd, 2022
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using std::vector;
  5.  
  6. bool Bit(int mask, int pos) {
  7.   return ((mask >> pos) & 1);
  8. }
  9.  
  10. int main() {
  11.   std::ios_base::sync_with_stdio(false);
  12.   std::cin.tie(0);
  13.   std::cout.tie(0);
  14.   int n;
  15.   std::cin >> n;
  16.   vector<vector<bool>> can_work_together(n, vector<bool>(n));
  17.   for (int i = 0; i < n; ++i) {
  18.     std::string str;
  19.     std::cin >> str;
  20.     for (int j = 0; j < n; ++j) {
  21.       can_work_together[i][j] = (str[j] == 'Y');
  22.     }
  23.   }
  24.   vector<bool> dp(1 << n, false);
  25.   dp[0] = true;
  26.   for (int mask = 1; mask < (1 << n); ++mask) {
  27.     for (int pos = 0; pos < n; ++pos) {
  28.       if (Bit(mask, pos)) {
  29.         for (int neighbour = 0; neighbour < n; ++neighbour) {
  30.           if (can_work_together[pos][neighbour] and Bit(mask, neighbour)
  31.             and dp[mask xor ((1 << pos) | (1 << neighbour))]) {
  32.             dp[mask] = true;
  33.           }
  34.         }
  35.       }
  36.     }
  37.   }
  38.   int max_cnt_of_one = 0;
  39.   for (int mask = 0; mask < (1 << n); ++mask) {
  40.     int temp_cnt_of_one = 0;
  41.     if (dp[mask]) {
  42.       for (int i = 0; i < n; ++i) {
  43.         if (Bit(mask, i)) {
  44.           ++temp_cnt_of_one;
  45.         }
  46.       }
  47.       max_cnt_of_one = std::max(max_cnt_of_one, temp_cnt_of_one);
  48.     }
  49.   }
  50.   std::cout << max_cnt_of_one << std::endl;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement