Josif_tepe

Untitled

Mar 18th, 2026
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7. short n, m;
  8. short mat[4][5], idx[4][5];
  9. short cnt_ones;
  10.  
  11. short dp[4][5][1 << 20];
  12. bool rec(short i, short j, int bitmask) {
  13.     if(__builtin_popcount(bitmask) == cnt_ones) {
  14.         return true;
  15.     }
  16.     if(dp[i][j][bitmask] != -1) {
  17.         return dp[i][j][bitmask];
  18.     }
  19.     short ones = __builtin_popcount(bitmask);
  20.     bool res = false;
  21.    
  22.     if(ones % 2 == 1) {
  23.         for(int k = 0; k < m; k++) {
  24.             if(k != j and mat[i][k] == 1) {
  25.                 if(!(bitmask & (1 << idx[i][k]))) {
  26.                     int new_mask = (bitmask | (1 << idx[i][k]));
  27.                     res = max(res, rec(i, k, new_mask));
  28.                 }
  29.             }
  30.         }
  31.     }
  32.     else {
  33.         for(int k = 0; k < n; k++) {
  34.             if(k != i and mat[k][j] == 1) {
  35.                 if(!(bitmask & (1 << idx[k][j]))) {
  36.                     int new_mask = (bitmask | (1 << idx[k][j]));
  37.                     res = max(res, rec(k, j, new_mask));
  38.                 }
  39.             }
  40.         }
  41.     }
  42.     dp[i][j][bitmask] = res;
  43.     return res;
  44. }
  45. int main()
  46. {
  47.     ios_base::sync_with_stdio(false);
  48.     memset(dp, -1, sizeof dp);
  49.     cin >> n >> m;
  50.     cnt_ones = 0;
  51.    
  52.     short id = 0;
  53.     for(int i = 0; i < n; i++) {
  54.         for(int j = 0; j < m; j++) {
  55.             cin >> mat[i][j];
  56.            
  57.             if(mat[i][j] == 1) {
  58.                 cnt_ones++;
  59.                
  60.                 idx[i][j] = id;
  61.                 id++;
  62.             }
  63.             else {
  64.                 idx[i][j] = 0;
  65.             }
  66.            
  67.         }
  68.     }
  69.    
  70.     bool res = false;
  71.    
  72.     for(int i = 0; i < n; i++) {
  73.         for(int j = 0; j < m; j++ ){
  74.             if(mat[i][j] == 1) {
  75.                 res = rec(i, j, (1 << idx[i][j]));
  76.                
  77.                 if(res) {
  78.                     break;
  79.                 }
  80.             }
  81.         }
  82.         if(res) {
  83.             break;
  84.         }
  85.     }
  86.    
  87.     if(res) {
  88.         cout << "DA" << endl;
  89.     }
  90.     else {
  91.         cout << "NE" << endl;
  92.     }
  93.     return 0;
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment