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:

This paper proves that you can, in fact, use randomized union rather than union by rank, and will still get same amortized time complexity.

@dpraveen Thanks! Added to the editorial.

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
3 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

Isn’t this the same as editorialist’s solution?
Although I agree that the editorial makes the problem seem way more complex that it is.

Facepalm… This problem has so many solutions and Editorialists should describe all them in details…

5 Likes

Editorialist’s solution is what I majorly followed :stuck_out_tongue: . But I must say, I learnt a lot of terminologies from editorial. Reminds me that I still have a long way to go…:slight_smile:

@r_64 just my opinion :stuck_out_tongue:
Although you do explain things in considerable detail, which I’m sure I will appreciate when going through the WEASELSC editorial

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

Well, to say the truth, i didn’t even read the solution line to line but only after seeing the PREREQUISITES, it was apparent to me that the solution discussed here would appear far more complex than it actually is!!!
Which, i believe, is never appreciable.

@meooow my solution resembles the editorial one considerably, but not in finer details as well as complexity as i think.

1 Like

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
1 Like

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]: https://www.codechef.com/viewsolution/15338636

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.

Can you tell some test cases where it may fail.

It didn’t actually work :-(.

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!

6 Likes

You only considered cycles of length <= 3. Try this:

1

5 5

1 2 1

2 3 0

3 4 1

4 5 0

5 1 1

1 Like

PLEASE ELABORATE. thanks

//