486B - OR in Matrix - CodeForces Solution


greedy hashing implementation *1300

Please click on ads to support us..

Python Code:

rows,columns=map(int,input().split()) ; mat=[] ; Rows_visited=set() ; soul=[] ; Columns_visited=set()
for row in range(rows):
    mat.append(list(map(int,input().split())))
    arr=[]
    for column in range(columns):
        arr.append("")
    soul.append(arr)

for row in range(rows):
    for column in range(columns):
        if mat[row][column]==0:
            if row not in Rows_visited:
                Rows_visited.add(row)
                for i in range(columns):
                    soul[row][i]=0
            if column not in Columns_visited:
                Columns_visited.add(column)
                for i in range(rows):
                    soul[i][column]=0
for row in range(rows):
    for column in range(columns):
        if soul[row][column]=="":
            soul[row][column]=1

cond=False
for row in range(rows):
    for column in range(columns):
        if mat[row][column]==0:
            if 1 in soul[row]:
                cond=True ; break
            for i in range(rows):
                if soul[i][column]==1:
                    cond=True ; break
        else:
            ones=False
            for i in range(rows):
                if soul[i][column]==1:
                    ones=True ; break
            if ones or 1 in soul[row]:
                pass
            else:
                cond=True; break
    if cond:print("NO") ; break
else:
    print("YES")
    for i in range(rows):
        print(*soul[i])

C++ Code:

#include <bits/stdc++.h>
#define ll                              long long
#define vr                              vector
#define pb                              push_back
#define vri                             vector <ll>
#define vrs                             vector <string>
#define take(arr,n)                     for(ll i=0; i<n; i++){ll a; cin>>a; arr[i]=a;}
#define takevrs(arr,n)                  for(ll i=0; i<n; i++){string s; cin>>s; arr[i]=s;}
#define showpair(v,n)                   for(ll i=0; i<n; i++){ cout<<v[i].first<<" "<<v[i].second<<" \n ";}

#define show(arr)                       for(auto i:arr) cout<<i<<" ";
#define sortv(v)                        sort(v.begin(),v.end());
#define mp                              make_pair
#define takepairii(v,n)                 for(ll i=0; i<n; i++){ ll x; ll y;cin>>x>>y; v.push_back(mp(x,y)); }
#define takepairss(v,n)                 for(ll i=0; i<n; i++){ string x; string y;cin>>x>>y; v.push_back(mp(x,y)); }
#define takepairis(v,n)                 for(ll i=0; i<n; i++){ ll x; string y;cin>>x>>y; v.push_back(mp(x,y)); }
 
using namespace std;

void solve(){
    
    int m,n;
    cin>>m>>n;
    int a[m][n];
    int b[m][n];
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){
            cin>>b[i][j];
            a[i][j]=0;
        }
    }
    int count=0,flag=0;
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){
            if(b[i][j]==1){
                int check=0,r=0,c=0;
                flag=1;
                for(int k=0; k<m; k++){
                    if(b[k][j]==1) check++,r++;
                }
                for(int k=0; k<n; k++){
                    if(b[i][k]==1) check++,c++;
                }
                if(check==m+n){
                    count++;
                     a[i][j]=1;
                }
                else if(r<m && c<n){
                    cout<<"NO\n";
                    return;
                }
            }   
        }
    }
    if(!flag){
        cout<<"YES\n";
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                cout<<'0'<<" ";
            }       
            cout<<"\n";
        }
        return;
    }
    if(count>0){
       cout<<"YES\n";
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                cout<<a[i][j]<<" ";
            }       
            cout<<"\n";
        } 
    }
    else cout<<"NO\n";
    
}

int main() {
    
    solve();
    // int t;
    // cin>>t;
    // while(t--){
    //     solve();
    // }
}


Comments

Submit
0 Comments
More Questions

1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold
1268. Search Suggestions System
841. Keys and Rooms
152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String