Problem Link:PREREQUISITES:Graph theory, Basic graph algorithms Difficulty: Easymedium PROBLEM:Given an undirected(connected) graph with N vertices and M edges, direct the edges(make a directed graph consisting of the same edges directed in either forward or backward direction) in such a manner that indegree of all the vertices is even or print that it is not possible. EXPLANATION:Given N, we know that there are N vertices. So we take N disjoint sets, and while taking the edges as input, we check using unionfind algorithm whether inclusion of this particular edge causes the formation of cycle. If yes, we'll not include this edge in our tree for now. We'll store every such edge in a different list, say L. After the above step, we now have one spanning tree of the given graph. Now, perform Depth First Search on any node(since it is a connected graph) and orient the edges in the direction of DFS. This operation gives us a Directed Acyclic Graph(DAG). Now, insert the extra edges from list L into the graph obtained after the above step in such a manner that it remains a DAG after inclusion of these extra edges. For that, we need to topologically sort the vertices. Thus, if we have an extra edge between u and v, and in topological ordering, u comes before v, then we direct the edge from u to v, otherwise from v to u. In this way, we have now included all the edges while retaining the properties of the DAG. Now, traverse each node from the adjacency list and store its parents in a set. [vector<set<ll>> parents(n)]. Also, store the indegree of every vertex in an array, say indegree[]. Now, begin from the node(s) which has no children. If its indegree is not even, reverse the direction of one incoming edge to this node, without caring about its parent. Once its indegree is even, traverse all of its parents and repeat the same procedure for every node. In this way, we reach the root node of our DAG which has no parents. Here, if its indegree is not even, we can be sure that the given graph cannot be converted to a directed graph in which every vertex has an even indegree. If the indegree of the root is even, then the graph has been converted to the desired graph. Hence we compare its edges with the given graph's edges and print the desired output. This gives us a linear time complexity to reach the desired output!
EDIT: Nima came up with a point which I would like to bring into everyone's notice: If number of edges is odd, the solution is impossible. Else, solution is always possible! Thanks Nima! \/ Time Complexity:O(V+E)=O(N+M) SOLUTION:asked 18 Dec '18, 01:39

Using Simple DFS:
Simple Idea: "Flip all edges between 2 odd indegree vertices so they both would have even indegrees."
DFS
This DFS is simple enough to code under 10 Lines neatly. Working: Initialize Invert as 0. Indegree(var) of a vertex is 0 if it's indegree is even, else Indegree(var) is 1. XOR with 1 inverts, else it remains the same. So, if indegree of some vertex is 1, Invert will flip. If Invert is 1 it means we need to flip the edges on our way. If Invert is 0 we don't need to flip. Example: There is one graph whose only 4 vertices has odd indegree. Let's name them 1 to 4. Also, there are two branches from vertex 2, one has vertex 1 and other has vertices 3 and 4. If we start DFS from vertex 1, all edges from 1 to 2 would be flipped and all edges from 3 to 4 would be flipped. Now Let's try DFS from vertex 2. Edges from vertex 2 to vertex 3 would be flipped. Now when it goes to vertex 4, it won't be able to find its pair so until it reaches vertex 1, all the edges would be flipped in the path. So we can see that edge from vertex 2 to vertex 3 gets flipped twice, resulting in flipping only from vertex 1 to vertex 2 and vertex 3 to vertex 4. answered 20 Dec '18, 13:33
1
@black_truce I went through your solution. Time complexity is pretty much similar to that of my solution. But my solution is more complex indeed! Good work!!
(21 Dec '18, 12:05)

@pandey_ji in your code while adding the extra edge after the topological sort you have the below code if(pehlekaun[pehla]>pehlekaun[dusra]) adj[dusra].push_back(pehla); else adj[pehla].push_back(pehla); Shouldn't the else condition be adj[dusra].push_back(pehla); Anyhow I ran the code with both the conditions and both seem to give correct answer. Am I missing something or are the test cases week ? answered 21 Dec '18, 00:39
1
@vikram987 thanks for pointing out the error! That was kind of a severe error! I can't believe how this solution got accepted with such error. The code actually got accepted at once. Maybe the test cases were weak enough because I'm adding self loop there(by mistake though) and it's still passing! Thanks anyway for the correction!! <3
(21 Dec '18, 12:01)

Access Denied on the Solution answered 18 Dec '18, 02:06
The solutions haven't been made public yet by the host! They won't take much time hopefully.
(18 Dec '18, 02:11)
can u post using ideone
(18 Dec '18, 02:16)
Link to my solution on ideone with sample test cases: https://ideone.com/PvaHdF
(18 Dec '18, 02:22)

Interesting approach. I did it a bit differently:
You can prove (2) with induction and use it to also direct the edges. each step of induction is:
note*: connectivity is in terms of edges. we don't want edges of the graph to be split into two separate components but it is ok to leave vertex "b" disconnected from the graph after removing the two edges. So, we start with a connected graph with even edges. Each step removes two edges and keeps the graph connected. (also note that removing those edges doesn't have any impact on the remaining graph because we don't care about outgoing degrees) So the induction invariant is maintained (connected graph & even edges). The base case is Now, the main challenge in implementing this is to remove pairs of edges that share a vertex and don't disconnect the graph. To do that, I picked a random vertex as seed, did BFS from that vertex and labeled vertices with their BFS level (imagine turning the graph into an onion with layers). Now, for each vertex in the outermost layer, as long as their degree is greater than 1, remove pairs of adjacent edges, prioritizing edges that have the other end in a farther layer. if you end up with a remaining degree of 1, just leave it. later when you process the other end of the edge (from an inner layer, you'll remove that edge first). my implementation: https://www.codechef.com/viewsolution/21869407 answered 18 Dec '18, 03:52
@nima101 your solution is a better and smarter approach! _/_
(18 Dec '18, 22:57)

@pandey_ji can u explain the intuition behind your approach , I mean why this works!! answered 18 Dec '18, 16:49
@sudhanshu6324 first I'm converting the graph to a DAG so that there won't be any cycle which would make it tough to update the indegree of a vertex belonging to that cycle. Then, I'm simply updating the indegree of every vertex starting from leaves in bottomup fashion till the root! That's it!
(18 Dec '18, 23:00)

Performing a DFS will treat it like a tree isn't it ?
Then why don't we do a solution using only DFS :D...
One of my friend did... I think it's a correct approach...
i did using simple bfs :)
I too tried bfs method @shivam_g1470 , but my code wasn't working on the sample cases. Maybe I didn't cover all the cases. However, I wasn't convinced with that solution, hence I came up with this intuition and it worked well! ;))
@l_returns yes we're treating the given graph like a tree. And yes, I went through the solutions of several people and found out that we can do it using DFS only too. Peace!
Okay great :)