Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

ADAMTR - Editorial

$
0
0

PROBLEM LINK:

Div1
Div2
Practice

Setter-Alei Reyes
Tester-Pranjal Jain
Editorialist-Abhishek Pandey

DIFFICULTY:

Easy-Medium

PRE-REQUISITES:

2-Sat, DFS, Graph Coloring

PROBLEM:

Given $2$ matrices $A$ and $B$. In an operation, you can swap a row $i$ with column $i$. Tell if its possible to change matrix $A$ to matrix $B$.

QUICK-EXPLANATION:

Key to AC- See that swapping both row $i$ and row $j$ lead to same effect, i.e. element at $A_{i,j}$ remains at its position. Hence, we can make a logical expression, one for each row (whether to swap it or not) and solve it with $2-sat$.

OR

Key to AC- Identify it as a graph coloring problem with edges telling if nodes have to be of same color or not.

An element $A_{i,j}$ is swapped only if one of row $i$ OR row $j$. Swapping both of them leads $A_{i,j}$ to remain at its place. Hence, construct a graph with nodes from $1$ to $N$. Add an edge from every row $i$ to every other row $j$ with label telling if their color must be same (both swapped or both not swapped) or different (only one of them swapped). Now do $DFS$ on this graph to see if it can be colored under such constraints. Answer does not exist if coloring is impossible.

EXPLANATION:

We will be primarily discussing tester's graph coloring solution. Dont worry $2-SAT$ lovers, I am very sure that @alei will discuss his $2-Sat$ here(/* end of shameless advertisement of setter's blog*/).

We will first see that how the graph is formed, and then how to color it.

1. How to form the graph-

With regards to swapping row $i$ and row $j$, we have a total of $4$ choices -

  • Swap neither $i$ nor $j$ - No change to $A_{i,j}$ and $A_{j,i}$.
  • Swap row $j$ with column $j$ - $A_{i,j} \implies A_{j,i}$, i.e. both get swapped.
  • Swap row $i$ with column $i$ - $A_{i,j} \implies A_{j,i}$, i.e. both get swapped.
  • Swap BOTH row $i$ and $j$ - No change to $A_{i,j}$ and $A_{j,i}$.

Now, for every element of $A$ do the following-

  • If its not possible to make $A_{i,j}$ and $A_{j,i}$ same as corresponding elements in $B$ by given operations, print No
  • If $A_{i,j}==B_{i,j}$ and $A_{j,i}==B_{j,i}$, add an edge between Node $i$ and Node $j$ signifying that color of these $2$ nodes should be same.
  • If $A_{i,j} \neq B_{i,j}$ and $A_{j,i} \neq B_{j,i}$, we need exactly one swap. Add an edge from Node $i$ to Node $j$ signifying that these nodes must be of different color.

However, there is one case we failed to capture in above. If $A_{i,j}==A_{j,i}==B_{i,j}==B_{j,i}$ then this means we don't care if they are of same or different color. For such cases, we don't put any edge as edges are put to constraint the color of node. If there is no constraint, we do not add the edges.

A brief code to do this is below, in case you need help in implementation-

View Content

2. How to Color the Graph-

Its nothing but simple DFS for all the connected components in our graph (recall that we are not adding edges in some cases - the graph can be disconnected!!).

One of the ways to do it is, start DFS from any node of the connected component. Assign it some color, say $1$. If we travel an edge, flip color to $0$ OR keep it $1$ depending on what type of edge it is. If at any point of time, we arrive at an already colored node, and the color we need to assign to it is different from its current color, the answer would be $NO$.

An implementation of the DFS function is given below if anyone needs help-

View Content

SOLUTION

Setter - Using $2-SAT$

View Content

Tester - Using Graph Coloring.

View Content

$Time$ $Complexity=O(N^2)$
$Space$ $Complexity=O(N^2)$

CHEF VIJJU'S CORNER :D

1. Setter's Notes-

View Content

2. Tester's Notes-

View Content

3. You are given a general graph of $N$ nodes and $M$ edges. Can you give an algorithm to tell how many colors the graph needs to be colored - such that no $2$ adjacent nodes are of same color? What info do you need? (HINT: Degree of nodes...)

4. Given a tree of $N$ nodes, where $N \leq 10^{18}$, how many colors are needed for coloring it if adjacent nodes cannot have same color? How many colors does a $N*M$ bipartite graph needs? Can we draw some interesting observation from it?

Ans-

View Content

4. Related Problems-


Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>