PROBLEM LINK:Author: Praveen Dhinwa Tester: Jingbo Shang Editorialist: Hanlin Ren DIFFICULTY:EasyMedium 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_iA_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:
To check this, we can either:
EXPLANATION:An observationThe problem actually asks if there exists an array $A$ that satisfies $Q$ conditions of the form "$A_iA_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,
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,
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 SolutionLet'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:
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:
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. Pseudocode:
The time complexity is $O(M\alpha(N))$, since we used DSU. Tester's solutionWe 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 The basic procedure for DSU is finding root. Let Pseudocode:
When merging two sets, we set the father of one's root the other root:
Now comes the modification: for every node $x$ in the DSU, we maintain We can easily rewrite our
When merging two sets, we not only set the father of
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
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 solutionWe 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:
This gives the spanning forest in linear time. The rest is the same as Setter's solution. A note on DSU's complexityThere 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 :D AUTHOR'S AND TESTER'S SOLUTIONS:Author's solution can be found here.(NOTE: This gets WA) RELATED PROBLEMS:
This question is marked "community wiki".
asked 24 Aug, 17:34

Hi everyone! This editorial is great, but I made a video editorial on this problem anyways :) It talks about using disjoint sets to solve this: Codechef SEPT17 FIll the Matrix Cheers! answered 12 Sep, 03:14

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 Code answered 11 Sep, 16:09
Isn't this the same as editorialist's solution?
(11 Sep, 16:14)
4
Facepalm... This problem has so many solutions and Editorialists should describe all them in details...
(11 Sep, 16:25)
Editorialist's solution is what I majorly followed :p . But I must say, I learnt a lot of terminologies from editorial. Reminds me that I still have a long way to go...:)
(11 Sep, 16:31)
1
@r_64 just my opinion :P
(11 Sep, 16:47)
1
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.
(11 Sep, 18:14)

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

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 a1 with 0 and a[3]=a1XOR 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. Code answered 11 Sep, 19:18

The testcases for this problem were very weak. My solution 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. answered 11 Sep, 19:39

The editorial seems actually very long. But its good as it is detailed. You can refer this for complete list of video tutorials for SEP17 including WEASTELX. answered 12 Sep, 18:41

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: CODE answered 11 Sep, 16:50

Test cases are weak My solution is getting 100 marks even though i get wrong answer for the test cases i made. answered 11 Sep, 22:15

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 answered 12 Sep, 18:41
I have used the same approach Explanation https://discuss.codechef.com/questions/109357/fillmtreditorial/110997 A link to code is given alongwith.. Feel free to ask anything... Upvote...
(13 Sep, 14:52)

@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 ? answered 13 Sep, 17:32

` cananyone tell me why my logic isnt 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 answered 13 Sep, 21:54

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 .... answered 14 Sep, 00:51
Well, You ought to go for vertex 2coloring instead of bipartite graph testing... You might like to have a look at my approach with explanation https://discuss.codechef.com/questions/109357/fillmtreditorial/110997 Feel free to ask anything... Please upvote
(14 Sep, 23:13)

can anyone give me test case where my code fails ? here is the link  https://www.codechef.com/viewsolution/15423175 answered 14 Sep, 19:04
Well, i dont code in C language, so i can't understand your code... But, You might like to have a look at my approach with explanation https://discuss.codechef.com/questions/109357/fillmtreditorial/110997 Feel free to ask anything... Please upvote...
(14 Sep, 23:14)

can anyone tell me what is wrong in this logic
answered 17 Sep, 18:21

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. answered 26 Sep, 18:03

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. answered 03 Oct, 13:11

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.
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=6qWg7O4Fmg
You can refer this for complete list of video tutorials for SEP17 including WEASTELX.