1475F - Unusual Matrix - CodeForces Solution


2-sat brute force constructive algorithms *1900

Please click on ads to support us..

Python Code:

from sys import stdin, stdout
def get_ints(): return map(int, stdin.readline().split())
def PRINT(s): stdout.write(s + '\n')


for w in range(int(stdin.readline())):
    n = int(stdin.readline())
    A = []
    B = []
    for i in range(n):
        A.append([int(x) for x in list(stdin.readline()[:-1])])
    stdin.readline()
    for i in range(n):
        B.append([int(x) for x in list(stdin.readline()[:-1])])

    for i in range(n):
        if A[i][0] != B[i][0]:
                        for j in range(n):
                A[i][j] = 1 - A[i][j]
    for j in range(n):
        if A[0][j] != B[0][j]:
                        for i in range(n):
                A[i][j] = 1 - A[i][j]
    PRINT(["NO","YES"][A==B])

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define ppb pop_back
#define int long long
#define itn long long
#define endl "\n"
#define fi first
#define se second
#define forf(i, a, b) for (int i = (a); i < (b); i++)
#define forr(i, a, b) for (int i = (a); i >=(b); i -= 1)
#define prdouble(x) cout<< fixed << setprecision(10)<< x;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int,int>> vpii;
typedef pair<ll, ll> pll;
typedef unordered_map<int,int> umap;
#define yeah cout<<"YES"<<endl;
#define nope cout<<"NO"<<endl;
#define all(v) v.begin(),v.end()
#define print(vec, l, r)                                                       \
    for (int i = l; i < r; i++)                                               \
        cout << vec[i] << " ";                                                 \
    cout << endl;
#define debug(v) cout<<v<<endl;

const int N=1e5+5;
const ll mod = 1e9 + 7;
const ll inf = 1e17;

long long ceil(int n,int i)
{
    return (n+i-1)/i;
}

long long power(long long a,long long b,long long m=mod){
    long long p=1;
    a%=m;
    while(b){
        if(b&1) p=(p*1LL*a)%m;
        a=(a*1LL*a)%m;
        b>>=1;
    }
    p%=m;
    return p;
}


void solve()
{
    int n;
    cin>>n;
    vvi a(n);
    vvi b(n);
    forf(i,0,n)
    {
        string s;
        cin>>s;
        forf(j,0,n)
        {
            if(s[j]=='1') a[i].pb(1);
            else a[i].pb(0);
        }
    }
    forf(i,0,n)
    {
        string s;
        cin>>s;
        forf(j,0,n)
        {
            if(s[j]=='1') b[i].pb(1);
            else b[i].pb(0);
        }
    }
    vvi temp(n);
    forf(i,0,n)
    {
        forf(j,0,n)
        {
            if(a[i][j]!=b[i][j]) temp[i].pb(1);
            else temp[i].pb(0);
        }
    }
    int k=-1;
    set<int> row,col;
    forf(i,0,n)
    {
        forf(j,0,n)
        {
            if(temp[i][j])
            {
                k=i;
                row.insert(k);
                break;
            }
        }
        if(k!=(-1))
        {
            forf(j,0,n)
            {
                if(temp[i][j]==0)
                {
                    col.insert(j);
                }
            }
            break;
        }
    }
    forf(i,0,n)
    {
        if(i==k) continue;
        forf(j,0,n)
        {
            if(col.find(j)!=col.end())
            {
                if(temp[i][j]==0)
                {
                    row.insert(i);
                    break;
                }
            }
            else
            {
                if(temp[i][j])
                {
                    row.insert(i);
                    break;
                }
            }
        }
    }
    // for(auto it:row)
    // {
    //     cout<<it<<endl;
    // }
    // cout<<"col"<<endl;
    // for(auto it:col)
    // {
    //     debug(it);
    // }
    int flag=1;
    forf(i,0,n)
    {
        int c=row.find(i)!=row.end();
        forf(j,0,n)
        {
            int d=col.find(j)!=col.end();
            int req=temp[i][j]%2;
            int have=(c+d)%2;
            if(req!=have)
            {
                flag=0;
                break;
            }
        }
        if(flag==0) break;
    }
    if(flag)
    {
        yeah
        return;
    }
    k=-1;
    row.clear();
    col.clear();
    forf(j,0,n)
    {
        forf(i,0,n)
        {
            if(temp[i][j])
            {
                k=j;
                col.insert(j);
                break;
            }
        }
        if(k!=(-1))
        {
            forf(i,0,n)
            {
                if(temp[i][j]==0)
                {
                    row.insert(i);
                }
            }
            break;
        }
    }
    forf(i,0,n)
    {
        if(i==k) continue;
        forf(j,0,n)
        {
            if(row.find(j)!=row.end())
            {
                if(temp[j][i]==0)
                {
                    col.insert(j);
                    break;
                }
            }
            else
            {
                if(temp[j][i])
                {
                    col.insert(j);
                    break;
                }
            }
        }
    }
    flag=1;
    forf(i,0,n)
    {
        int c=row.find(i)!=row.end();
        forf(j,0,n)
        {
            int d=col.find(j)!=col.end();
            int req=temp[i][j]%2;
            int have=(c+d)%2;
            if(req!=have)
            {
                flag=0;
                break;
            }
        }
        if(flag==0) break;
    }
    if(flag) yeah
        else nope;

    
    return;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t=1;
    cin>>t;
    while(t--)
    {
        // cout<<t<<" ";
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree