Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <vector>
- #include <cstring>
- using namespace std;
- short n, m;
- short mat[4][5], idx[4][5];
- short cnt_ones;
- short dp[4][5][1 << 20];
- bool rec(short i, short j, int bitmask) {
- if(__builtin_popcount(bitmask) == cnt_ones) {
- return true;
- }
- if(dp[i][j][bitmask] != -1) {
- return dp[i][j][bitmask];
- }
- short ones = __builtin_popcount(bitmask);
- bool res = false;
- if(ones % 2 == 1) {
- for(int k = 0; k < m; k++) {
- if(k != j and mat[i][k] == 1) {
- if(!(bitmask & (1 << idx[i][k]))) {
- int new_mask = (bitmask | (1 << idx[i][k]));
- res = max(res, rec(i, k, new_mask));
- }
- }
- }
- }
- else {
- for(int k = 0; k < n; k++) {
- if(k != i and mat[k][j] == 1) {
- if(!(bitmask & (1 << idx[k][j]))) {
- int new_mask = (bitmask | (1 << idx[k][j]));
- res = max(res, rec(k, j, new_mask));
- }
- }
- }
- }
- dp[i][j][bitmask] = res;
- return res;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- memset(dp, -1, sizeof dp);
- cin >> n >> m;
- cnt_ones = 0;
- short id = 0;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- cin >> mat[i][j];
- if(mat[i][j] == 1) {
- cnt_ones++;
- idx[i][j] = id;
- id++;
- }
- else {
- idx[i][j] = 0;
- }
- }
- }
- bool res = false;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++ ){
- if(mat[i][j] == 1) {
- res = rec(i, j, (1 << idx[i][j]));
- if(res) {
- break;
- }
- }
- }
- if(res) {
- break;
- }
- }
- if(res) {
- cout << "DA" << endl;
- }
- else {
- cout << "NE" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment