Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <vector>
- #include <queue>
- #include <bits/stdc++.h>
- using namespace std;
- int n,m;
- int mat[5][6];
- int kolku;
- short dp[4][5][(1<<20)];
- int idx[5][6];
- int f(int i,int j,int k)
- {
- if(__builtin_popcount(k) == kolku) {
- return true;
- }
- if(dp[i][j][k] != -1) {
- return dp[i][j][k];
- }
- int bits = __builtin_popcount(k);
- int result = 0;
- if(bits % 2 == 1) {
- for(int p = 0; p < m; p++) {
- if(p != j and mat[i][p] == 1) {
- if(!(k & (1 << idx[i][p]))) {
- result = max(result, f(i, p, (k | (1 << idx[i][p]))));
- }
- }
- }
- }
- else {
- for(int p = 0; p < n; p++) {
- if(p != i and mat[p][j] == 1) {
- if(!(k & (1 << idx[p][j]))) {
- result = max(result, f(p, j, (k | (1 << idx[p][j]))));
- }
- }
- }
- }
- return dp[i][j][k]=result;
- }
- int main()
- {
- cin>>n>>m;
- kolku = 0;
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<m;j++)
- {
- cin>>mat[i][j];
- if(mat[i][j]==1)kolku++;
- }
- }
- int cnt=0;
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<m;j++)
- {
- if(mat[i][j]==1)
- {
- idx[i][j]=cnt;
- cnt++;
- }
- else
- {
- idx[i][j]=0;
- }
- }
- }
- memset(dp,-1,sizeof dp);
- bool ok=false;
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<m;j++)
- {
- if(mat[i][j]==1)
- ok=f(i,j,(1<<(idx[i][j])));
- if(ok)break;
- }
- if(ok)break;
- }
- if(ok)cout<<"DA"<<endl;
- else cout<<"NE"<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement