PROBLEM LINK:
Setter-Denis Anishchenko
Tester-Hasan Jaddouh
Editorialist- Click here to find out.
DIFFICULTY:
MEDIUM
(This problem is MEDIUM w.r.t. thinking and wit required, while implementation is EASY and simple)
PRE-REQUISITES:
Graph Theory, DFS. Knowledge of mathematics in Graph theory will help you understand the editorial to its fullest :)
PROBLEM:
You are given $2$ problems out of which you have to solve only one of them. The problems are-
- Partition the graph into set of 2 vertices, such that number of edges from one set to another is $< K$
- Find a cycle of length at least $K$
- If both of above are unsolvable, print "NO ANSWER"
QUICK EXPLANATION:
Key to Success- The interrelation between the 2 problems was the factor which drew line between AC and "giving up"/WA.
At first glance, it looks a very complicated problem where we might have to "work for" both the problems if one of them has no answer. However, on analyzing the first problem of "CUT" in a little more detail, we see that it says about size of cut, but has no constraints on size of sets! (Hint: What if we put only 1 node in set $A$?). Hence, we do a check to find a node with degree $< K$. We put this node in set $A$ and all other nodes in set $B$, solving the first problem.
For second problem, we require a little more coding - we need to do a DFS. Note that, if we cannot solve first problem, we know for sure that each node has degree $\ge K$. Hence, we keep traversing until we find a node with all of its neighbors visited. Now, on finding such a node, we "close" the cycle by putting an edge between that node and the neighbor which was visited the earliest to get the longest possible cycle which visits at least that node, and all of its neighbors. Since number of neighbors is $\ge K$, the cycle is at least of length $K$.
EXPLANATION:
I think a lot of fun part is over since you guys read the Quick Explanation Section :( . Nevertheless, lets have as much fun as we can :). I know many of you would be having questions like "How? How to prove that? Why is it correct?" (especially for second problem), and all of those will be answered here.
This editorial is divided into 3 parts. The first part deals with solving problem of CUT, the second part deals with solving the CYCLE problem. The third part is a general conclusion, compilation and inference of our assumptions which arent done in other two parts.
Please note, that the way I will explain the solution, is more related to the given problem (i.e. when we have to solve either CUT or CYCLE) than when we have to solve both the problems individually. Also, the answer to hand exercises, i.e. all the "(Why?)" is in Chef Vijju's Corner.
1. Problem CUT-
(Refer to the Quick explanation if you havent.)
We open up a problem statement, with a fancy name resembling "Solve whatever you want" and are quite happy that FINALLY we have a choice in solving problems. And the very next thing done by setter is to dump 2 NP hard problems on our face. To top it off, he says that "If one of the problem has no solution, solve the other.". Not really a "Solve whatever you want," eh? XD
But as it turns out, solving both these problems when they are together (with the given choice of "Either this or that") is much, much easier than solving them both individually! The interrelation between these $2$ problems was not evident at first sight, which is the reason why so easy and simple problems had very less successful submissions during the contest.
Lets focus on this problem $CUT$ first. Think like a lazy-man. "What is the cheapest, and easiest way by which I can solve this problem reasonably well?" It turns out that there is a very basic method after all!
Note that the problem only stresses on the size of the cut, not the size of the sets $A$ and $B$!! So, this means, sets of size $1$ are very much allowed :) . What we do is, after taking the input and storing the graph, we check for the degree of nodes. If we find any node with degree $< K$,the voila! We solved the problem!
Say we found a node with degree $< K$, then that means there are $< K$ edges from that node to other nodes of the graph. Lets call this node $X$. We put this node in set $A$ , and all other nodes in set $B$. Note that size of cut is nothing but number of edges between nodes in set A and nodes in set B. In other words, size of cut is nothing but number of edges from $X$ to other nodes (i.e.its immediate neighbors). Size of cut, is hence, nothing but degree of $X$, which is $< K$.
If we fail to find any node with degree $< K$, then there isn't any "cheap" solution to this problem :( . We, like lazy-men, will cry that this problem is "too hard" and try moving on to the second problem.
2. Problem CYCLE-
(Pssst!!)
(Psssssst!!)
(Before reading anything, check out Question 5 b) here. DONT TELL HASAN OR HE'LL KILL ME.)
(PS- For those who don't understand, $\delta (G) \ge K$ means "degree of all nodes is $\ge K$.")
I will not explain the above proof in detail here. I will however, give hints and links for reference so that you can derive the result yourself. This argument and method of proof is not uncommon in questions of competitive coding, and they often appear in harder ones like this question. I will, however, give an alternate intuition/informal-proof to the claim.
By above, we (at least now) know that, if we have a graph where degree of each node is at least $K$, then there will be a cycle of length at least $K$ (or $K+1$ to be more accurate). Let me highlight the interrelation of problems here. If we start by attempting to solve problem CUT, then there are two outcomes-
- We may find a node with degree $< K$ and solve it. No need to worry about problem CYCLE then.
- We may not be able to find a node with degree $< K$. This implies that, all the nodes have degree $\ge K$ !!. Hence, the cycle problem is trivially solvable under these assumptions.
First I will describe how to solve CYCLE under the assumption that all nodes have degree $\ge K$ (or that we first tried solving CUT but failed to find an appropriate node).
What we will do is, a simple DFS of the graph. We use an array, say $time[]$, to mark which node we visited first, which node we visited second &etc. More precisely, if $time[a]=b$, then that means "Starting from the chosen root, Node $a$ is the $b'th$ node we visited." As simple as a cake.
Now, we keep on traversing till we find a node whose all neighbors are already visited. Note that we are bound to find such a node (Why? Q-1). Once we find such a node, we "close" the cycle by going back to the neighbor which was visited earliest (Why? Q-2). All that is left to be done, is to retrace the path backwards until we reach to the same node again. Implementation wise, we can easily do that using a stack (or vector or any suitable data structure). You can refer to editorialist's commented solution to see how the idea is implemented.
Now, why is this correct? Why does our claim hold, that a cycle of length $K+1$ will definitely exist if all nodes have a degree $\ge K$? Formal proof, is left to readers as an exercise. I will provide you with sufficient hints (well, I have provided you with the solution itself xD) so that you guys can derive it on your own- this is so that the concept can be used by you for solving future hard problems. But its obvious that not many go for pen and paper and derive using countless pages the claim above. There is something called a "killer-instinct" or intuition. Lets try to prove this intuition-wise, with an informal proof.
Lets say, we are doing DFS of a graph, and node $X$ is the last node left which we haven't visited. On visiting $X$, we see all the neighbors are visited as well, and that $X$ has edges going to them which means a cycle definitely exists. If we chose the earliest visited nodes, we can get a cycle which contains, at least, all the neighbors of $X$ (i.e. the largest possible cycle). Since number of neighbors of $X$ is $\ge K$, the cycle must have a length of at least $K+1$. We can very easily generalize this to any node of graph whose all neighbors are visited, instead of "last node" as I took.
3.Conclusion
Not much to say. During contest, I think $\approx 21$ people solved the question during the contest (counting from both divisions combined). The reason for this is not hard to see. At first, lot of people simply got scared on seeing $2$ $NP-Hard$ problems. Then, the interrelation of the $2$ problems was clearly not obvious from first sight. However, lets appreciate the sheer beauty and concept of the problem, and how the setter interrelated $2$ $NP-Hard$ problems into such a beautiful one. And well, in the end, it didnt quite turn out to be "Solve whatever you want" exactly, unless you have the ability solve $NP-Hard$ problems in linear time. (Pssst, if you have that, just teach me that so I can claim a few million dollars XD).
SOLUTIONS:
The solutions are also copied in the tabs, so that you guys dont have to wait for @admin to upload them etc. and can immediately access them. Copy the solutions from here and paste anywhere you are comfortable reading them.
CHEF VIJJU'S CORNER:
1.Answer of Q-1-
"Now, we keep on traversing till we find a node whose all neighbors are already visited. Note that we are bound to find such a node (Why? Q-1)."
2. Answer for Q-2-
"Once we find such a node, we "close" the cycle by going back to the neighbor which was visited earliest"
3. Setter's Notes-
4. Tester's Notes-
5. The time array, which I mentioned is frequently used in defining time of entry, and exit in a subtree ,assuming visiting a node takes unit (or $W_i$, depending on the question) time. You can try learning about Euler Path and Tour, and then Heavy Light Decomposition.
6. Test Case Bank-
7. Common Errors-
8. The crux of the question was how deeply you know the theorems in Graph Theory. For instance, if you already knew or induced that "If graph has all nodes of degree at least $K$, then problem CYCLE is easily solvable". Remember that, under certain assumptions and given conditions, the $NP-Hard$ problems do have an easy solution. They are $NP-Hard$ for a general scenario where none of the assumptions may hold. If you knew the above claim for CYCLE problem, you can work out the rest for CUT problem to get an easy AC.
9. Links and Hints for formal proof mentioned in problem CYCLE-