FILLMTR - Editorial

PROBLEM LINK:

Practice

Contest

Author: Praveen Dhinwa

Tester: Jingbo Shang

Editorialist: Hanlin Ren

DIFFICULTY:

Easy-Medium

PREREQUISITES:

disjoint set union, bipartite graph, spanning forest

PROBLEM:

A matrix B_{N\times N} is good if there is an array A such that B_{i,j}=B_{j,i}=|A_i-A_j| for all 1\le i,j\le N. Given a matrix where exactly Q entries are filled, and every filled entry is 0 or 1, determine if we can fill the rest entries(by any integer, possibly not 0,1) such that the resulting matrix is good.

QUICK EXPLANATION:

The answer is “yes” if and only if you can assign every number in [1,n] even or odd, such that:

  • When B_{i,j}=0, i and j have the same label;
  • When B_{i,j}=1, i and j have different label.

To check this, we can either:

  • Link a bidirectional edge (i,j) for any known B_{i,j}, find a spanning forest and assign labels along the tree greedily, and check if the assignment is valid;
  • or, use a Disjoint Set Union structure that maintains parity information, and add edges into the structure.

EXPLANATION:

An observation

The problem actually asks if there exists an array A that satisfies Q conditions of the form “|A_i-A_j|=val”. Consider a graph with n nodes, for every such condition, we link an edge with weight val between node i and node j.

Let’s say a node i is even if A_i\equiv 0\pmod 2, otherwise say node i is odd. Then,

  • if two nodes are connected by an edge of weight 0, then they’re both even or both odd;
  • if two nodes are connected by an edge of weight 1, then one of them is even and the other is odd.

Thus, if the array A exists, there must be an assignment that maps every node to either even or odd, and satisfies the above requirements. But can the existence of A be guaranteed by the existence of that assignment? The answer turns out to be yes! For a valid assignment, we simply put A_i=1 if node i is odd, and put A_i=0 if it’s even. Why does this work? For every edge (i,j) in the graph,

  • if its weight is 0, then either i,j are both odd, or they are both even. If they are both odd, then A_i=A_j=1; if they are both even, then A_i=A_j=0. In both cases we have A_i=A_j;
  • if its weight is 1, then one of A_i,A_j is 1 and the other is 0, therefore |A_i-A_j|=1.

We see that all edges, no matter its weight is 0 or 1, is satisfied by our array A.

That is to say, the answer is “Yes” if and only if there exists a valid assignment. How to check this? There are many different solutions:

Author’s Solution

Let’s first find a spanning forest of the graph. We can maintain a data structure called disjoint set union(“DSU”), and do something similar to Kruskal’s Algorithm.

As in Kruskal’s Algorithm, initially no edge is added to the graph, and every vertex itself forms a set. Then let’s add edges one by one, in an arbitrary order(since we only need “a spanning forest”, not a “minimum” one or something, so any order is acceptable). Every time we add an edge (u,v), if u and v are in different sets, we combine these sets into one set, and add this edge to the spanning forest; otherwise we do nothing.

Pseudocode:

for i in nodes
	makeset(i) //i itself is a set
for (u,v) in edges
	if find(u) != find(v)
		add edge (u,v) to the edge list of spanning forest
		Union(u,v)//merge two sets

After we have a spanning forest, we can solve the problem. First let’s consider the case that all edges are in the spanning forest. In this case the answer is always “Yes”, and we can find a valid assignment in linear time. For every connected component, we pick an arbitrary node v and suppose v is even. For any node u in this component, if the distance between u and v is odd(i.e., there are odd edges that has weight 1 in the path from u to v), then u is odd; otherwise u is even. This assignment can be computed in one dfs:

dfs(u) : //procedure dfs()
	for v is u's child :
		parity[v] = parity[u] xor (the weight between (u, v))
		dfs(v)
//main
for i in nodes :
	if i's component is not visited :
		dfs(i)

What if there are other edges? Note that no “other edge” can cross two components. If for every edge (u,v), (the parity of u) xor (the parity of v) xor (the weight of (u,v)) is 0(“parity constraint”), then the assignment is already valid. Otherwise, say edge (u,v) violates that constraint, then there is a cycle of odd weight(that passes through u and v). A cycle of odd weight means that a number’s parity is different from itself’s, which is impossible. So we only need to check if every other edge satisfies the parity constraint.

To demonstrate the idea, let’s consider the last test case in the sample. A spanning forest(tree) is shown below. Now we are adding an edge (1,3) which has weight 1. However parity of 1 and 3 are both 0(even), and this edge has weight 1, thus violates the parity constraint. Since there is an edge that violates the constraint, there should be an odd cycle — it’s 1\to 2\to 3\to 1 in this case, and this cycle means that parity(1)\ne parity(1), so there must be no solution.

sample

Pseudocode:

for (u, v) in edges :
	if parity[u] xor parity[v] xor weight[u,v] == 1 :
		//an odd cycle found
		print "No" and exit
print "Yes"//All edges satisfy the parity constraint

The time complexity is O(M\alpha(N)), since we used DSU.

Tester’s solution

We can modify the DSU structure so that it records parity information. Recall that the DSU structure is a set of rooted trees, where a tree stands for a set. Let father[x] be x's parent.

The basic procedure for DSU is finding root. Let find(x) be x's root. We’ll use a technique that’s called path compression: for every node that lies between x ans find(x), we set its father to be the root. The following is a demonstration of path compression.

path compression

Pseudocode:

find(x) :
	if x == father[x] :   //x is root
		return x
	father[x] = find(father[x])   //path compression
	return father[x]

When merging two sets, we set the father of one’s root the other root:

Union(x, y) :
	father[find(x)] = find(y)

Now comes the modification: for every node x in the DSU, we maintain diff[x], which is defined as parity(x) xor parity(father[x]). For example, consider the DSU shown in picture below. Black/white nodes are odd/even nodes respectively, and the value of diff[x] is written on the edge x\to father[x]. Note that find() changes several diff’s(red ones).

diff

We can easily rewrite our find() function. Let parity(x) be x’s parity(0 if x is even and 1 if x is odd). In the following code, t is x's original father, and after find(t), diff[t] becomes parity(root) xor parity(t). Now the old diff[x] is equal to parity(x) xor parity(t), and we want the new diff[x] become parity(root) xor parity(x). It’s easy to see diff[x](new) should become diff[x](old) xor diff[t].

find(x) :
	if x == father[x] : return x
	t = father[x] //the original father of x
	father[x] = find(t)
	diff[x] = diff[x] xor diff[t]//compute new diff[x].
	return father[x]

When merging two sets, we not only set the father of root_x be root_y, but also computes the new diff[root_x]. In the following pseudocode, w is the weight of inserted edge so parity(x) xor parity(y)=w; We also have parity(x) xor parity(root_x)=diff[x] and parity(y) xor parity(root_y)=diff[y]. Thus diff[root_x], which should be equal to parity(root_x) xor parity(root_y), can be computed by w xor diff[x] xor diff[y].

Union(x, y, w) :
	//w is the weight of the edge (x, y)
	root_x = find(x)
	root_y = find(y)
	father[root_x] = root_y
	diff[root_x] = w xor diff[x] xor diff[y]//compute diff[root_x].

OK, with the modified DSU structure, we can solve the problem more conveniently. Just like Kruskal’s algorithm, we insert edges one by one. For an edge (u,v) with weight w, if u and v are in different sets, we just merge them; if they are in the same set, we use diff to check if this edge doesn’t make an odd-weighted cycle. The indicator for an odd-weighted cycle is an edge (x,y) with weight w such that parity(u) xor parity(v) xor w=1. Since u and v are connected, they have the same root, say r. Then diff[u]=parity(r) xor parity(u) and diff[v]=parity(r) xor parity(v), so what we need to check is diff[u] xor diff[v] xor w=1.

for (u,v) in edges:
	w = weight of (u,v)
	if find(u) != find(v) :
		Union(u, v, w)
	else :
		if diff[u] xor diff[v] xor w == 1 :
			return "No"
		//otherwise do nothing

So we just scan all edges and judge every edge that’s not in the forest one by one. This solution also has complexity O(M\alpha(N)).

Editorialist’s solution

We can find a spanning forest by simple dfs.

Let’s iterate over all nodes. When we meet an unvisited node, we mark it as visited, search its component, and return the dfs tree. Of course, every node in its component is also marked visited. When we meet a visited node, we just do nothing. The procedure is better described by pseudocode:

dfs(x) :
	visited[x] = true
	for y adjacent to x
		if not visited[y] then
			add (x,y) to the spanning forest
			dfs(y)
construct_spanning_forest() :
	for i in all nodes
		if not visited[i] then
			dfs(i)

This gives the spanning forest in linear time. The rest is the same as Setter’s solution.

A note on DSU’s complexity

There are two tricks that used in DSU to make its complexity O(\alpha(n)) per operation.

The first one is path compression, which we mentioned in Tester’s solution part. The second one is union by rank, which means: when you merge two sets rooted at r_1 and r_2, you should let the final root be the one whose rank is larger; and the rank is something similar to depth. You can find detailed explanation here.

If you only use path compression, or you only use union by rank, the complexity should be O(\log n) per operation. The implementation in this editorial only uses path compression, so its complexity is actually O(M\log N).

However, in practice, people usually only implements path compression. The reason is that for most cases, path compression performs already quite fast. In fact, as pointed by the Author, the complexity of path compression with randomized union is O(\alpha(n)) per operation, and many data can be seen as random data for DSU. See this paper for more details.

ALTERNATIVE SOLUTION:

As you can see, there are many approaches to tackle the problem.

So if your solution is different with ours, feel free to leave a comment :smiley:

AUTHOR’S AND TESTER’S SOLUTIONS:

Author’s solution can be found here.(NOTE: This gets WA)
Tester’s solution can be found here.
Editorialist’s solution can be found here.

RELATED PROBLEMS:

I have used graph node coloring to solve this problem.

Firstly i checked for self loops and then for edge from u to v when an edge from v to u is already given, thus adding a bidirectional edge in a graph only once.

In case of self loop, if val == 1, ans is no.
in case of edges where edge between same vertices but opposite direction is already given, if val of both edges is different, ans is no.

after that, run a dfs on all unvisited vertices, coloring them with same color as their parent if val ==0 and different color if val==1. if found any contradiction during color assignment, ans is no.

If given graph pass all these tests, ans is yes…

PS: i found editorial solution a way too complex, so i posted mine.

Feel free to ask anything. Please upvote if you found this helpful.

Here’s a link to my


[1]


  [1]: https://www.codechef.com/viewsolution/15226381
7 Likes

This problem can be converted to 2-SAT, just add (i XOR j) if B[i][j]=1 or, (i XNOR j) if B[i][j]=0, to the implication graph.

1 Like

I used BFS.
Observations: Check whether the given entries of matrix do not conflict with each other?
How can we check that?
Assume some values and just explore all the values that are affected by that assumed value,and if any conflict is there return NO otherwise YES
So What we do?
We are given some graphs ,in which we may have several components in each component assume one node’s value and derive the values of other nodes in the component ,if any conflict is there return NO otherwise YES .
Code:


[1]


  [1]: https://www.codechef.com/viewsolution/15236840

There can be a really easy greedy solution the problem.We can initialise the array A by -1.There is crucial observation that the array A can be constructed with only two values 0 and 1.Sort the index value pairs.Now if both the given index are unassigned,assign them according to the value given.Like 1 3 1 would mean 1 and 3 would be different .So initialise a[1] with 0 and a[3]=a[1]XOR val.Keep doing this and if at some point when both the indices are intialised with values!= -1 ,then check if they satisfy the given value,if not break and print -1.


[1]


  [1]: http://www.codechef.com/viewsolution/15278044
2 Likes

The testcases for this problem were very weak.
[My solution][1] got an AC even though it is incorrect. It is easy to see that this fails in cases where 2 previously visited sets of weight 0 have an edge of weight 1 in between.
An example where my solution should fail is :
1
5 4
1 2 0
1 5 0
3 4 0
4 5 1
I reported the issue in the comments section of the problem link but to no avail. I wonder how many people with a similar solution got away with an AC.
[1]: CodeChef: Practical coding for everyone

1 Like

Test cases are weak My solution is getting 100 marks even though i get wrong answer for the test cases i made.

What I thought is we can see for the conditions when the array is not possible.
There would be 3 cases for this:

  1. For an index (i, j) of the matrix a, if (i==j) but a(i, j) =1.

  2. For two different indices (i1, j1) and (i2, j2) if(i1=j2) and (i2=j1) but a(i1, j1)! =a(i2, j2).

  3. For three different indices if any two of them have one common index (i, j) then the number of 1’s at these three indices should not be 1 or 3. Eg. If three indices are (1,2), (1,4), (2,4) here for any two indices 1 index(i or j) is common. Then for this 1 should not be at all the three positions or there should not be only one 1 at any of these 3 indices.

Can you tell me if there is any problem with this logic.

Hi everyone!
This editorial is great, but I made a video editorial on this problem anyways :slight_smile:
It talks about using disjoint sets to solve this:

Codechef SEPT17- FIll the Matrix

Cheers!

7 Likes

The problem is not yet added to the practice portal

I tried to solve it using bicoloring(dfs) but getting TLE on subtask 2 of task 2. Can someone help me ? Here is my code:
https://www.codechef.com/viewsolution/15367330

The editorial seems actually very long. But its good as it is detailed.
I have summarized the tutorial in my short ~10 min video here : https://www.youtube.com/watch?v=6qWg7-O4Fmg

You can refer this for complete list of video tutorials for SEP17 including WEASTELX.

1 Like

I used bipartite graph for 1 difference edges and disjoint sets for 0 difference edges. I am getting WA. Please, take a look at the code :


[1]


  [1]: https://www.codechef.com/viewsolution/15414051

@taran_1407
Sorry I don’t know how to reply to your comment.
I used the same approach. Can you find out why my code is getting TLE in the 2nd task of the 2nd subtask ?

[My Code][1]
[1]: CodeChef: Practical coding for everyone

` cananyone tell me why my logic isnt working
#include
#include
using namespace std;

        /*
         * 
         */
        const int MAX=2e6+9;
        int main() {
            int i  ,t ,n ,q;
            cin>>t;
            while(t--){
                cin>>n>>q;
                int v[MAX]={0} ,x ,y ,d , a[MAX]={0};
                for(i=1;i<=n;i++){
                    a[i]=0;
                    v[i]=0;
                }
               int f=0;
                while(q--){
                    cin>>x>>y>>d;
                    if(x==y){
                        if(d!=0){
                            f=1;
                        }
                        v[x]=1;
                        a[x]=0;
                        
                    }
                    else  if(v[x]==0&&v[y]==0){
                  
                        a[x]=0;
                        a[y]=d+a[x];
                        v[x]=1;
                        v[y]=1;
                    }
                    else  if(v[x]==0&&v[y]==1){
                        a[x]=a[y]+d;
                        v[x]=1;
                    }
                    else  if(v[x]==1&&v[y]==0){
                        a[y]=a[x]+d;
                        v[y]=1;
                    }
                    else  if(v[x]==1&&v[y]==1){
                        if(abs(a[x]-a[y])!=d){
                            f=1;
                        }
                    }
                }
                if(f==1){
                    cout<<"no"<<"\n";
                }
                
                else{
                    cout<<"yes"<<"\n";
                }
                
            }
            
        }
         
           `can anyone tell me why my logic is not working 

first i declared two array a[] and other array v[] array v will keep the record of all the integers which
are visited now during each query 4 condition arises

  1. both 1st and 2nd are unvisited
  2. 1st is visited and 2nd is unvisited
    3 1st is unvisited and 2nd is visited
    4 both are visited
    now
    for the 1st condition
    i marked both the nodes as visited
    and assigned a[i]=d;
    a[j]=a[i]+d;
    for the 2nd condition
    marked 2nd node as visited
    a[j]=a[i]+d;
    for 3rd condition
    marked 1st node as visited
    a[i]=a[j]+d;
    for 4 th condition
    since both nodes are visited and are assigned some values
    therefore
    if(abs(a[i]-a[j])!=d)
    ans=no and exit
    else
    ans=yes;

Will anyone please tell me whats the problem with my solution …
Thnks in advance…

i tried checking if graph is bipartite or not…
https://www.ideone.com/OGDY8I

PLease do check …

can anyone give me test case where my code fails ?
here is the link - https://www.codechef.com/viewsolution/15423175

can anyone tell me what is wrong in this logic

  1. Initially all nodes are uncolored.
  2. As I get values of i,j and val, if both a[i] and a[j] are uncolored then if val is 0 then give them same color or if val is 1 then give different colors.
  3. If anyone of a[j] or a[i] is already colored then color other one accordingly if val is 1 then different color or same color if val is 0.
  4. If both a[i] and a[j] are colored then if val is 0 then they should have same color if not then matrix is wrong similarly if val is 1 then they should have different color if not the matrix is wrong.
  5. In all other cases matrix will be correct.

Hello! can anyone pls tell me why the solution on going according to author’s logic is getting WA? I have written code according to the logic explained in the “Author’s Solution” section of the editorial which seems to be absolutely correct. Any help is much appreciated. Thanks!! U can find my code here.

Simple solution can found only by sorting the initial m commands and then trying to assign the values to the array. This takes O( m*log(m) ) time, which is the time for sorting. U can see the solution here.