Can anyone explain me how binary search will lead to answer.

# | User | Rating |
---|---|---|

1 | Radewoosh | 3759 |

2 | orzdevinwang | 3697 |

3 | jiangly | 3662 |

4 | Benq | 3644 |

5 | -0.5 | 3545 |

6 | ecnerwala | 3505 |

7 | tourist | 3486 |

8 | inaFSTream | 3478 |

9 | maroonrk | 3454 |

10 | Rebelz | 3415 |

# | User | Contrib. |
---|---|---|

1 | adamant | 172 |

2 | awoo | 166 |

3 | nor | 165 |

4 | SecondThread | 163 |

5 | maroonrk | 162 |

6 | BledDest | 161 |

6 | Um_nik | 161 |

8 | -is-this-fft- | 149 |

9 | Geothermal | 146 |

10 | platelet | 142 |

Can anyone explain me how binary search will lead to answer.

↑

↓

Codeforces (c) Copyright 2010-2023 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Dec/09/2023 08:22:09 (g1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

i think for each k we are checking whether there is a loop with any edge greater than k . if no then we shift high else if yes then we shif low. please correct if something is missing! what to do after that. how to make top. sort work on cyclic graph?

Just to be clear, in the binary search you have to check if a graph with only the edges that are higher than mid is cyclic or not, if it is cyclic, you do the binary search from mid+1 to right, else, left to mid, because you can decrease the value of k in this case.

After the binary search, knowing the lowest k possible, you do a topological sort

with only the edges higher than k, that way the graph isacyclic.With the topological order you go through all the edges smaller or equal to k (the ones you can invert), if it creates a cycle (edge is (u, v) but in the topological sort the v goes before u) you put in the list of edges to invert, if not, ignore it.

One way to implement this last part is having an array or map indexing each vertex to the respective position on the topological order, that way if vert[v] < vert[u], you know that v goes before u.

An article about it in geeksforgeeeks: https://www.geeksforgeeks.org/assign-directions-to-edges-so-that-the-directed-graph-remains-acyclic/

My implementation: https://codeforces.com/contest/1100/submission/48365428

lclpsoz means you are saying that once we find our best k , then its guarantee that any edge value > k will not be reversed otherwise its a contradiction!

so when we build a new graph with edges > k , we will get an acyclic graph ..

upto this much i understand ...

now what i understand after this is : (Ps : please correct me if wrong )

now since we made out new graph with all edges > k , the graph is acyclic . (the first part what we wanted we get) . we then find the topological ordering of the graph !

now the source u mentioned of G4G , i am thinking that you are trying to say , the edges with weights <=k , we should make them undirected , now we should assign their directions in such a way that the graph remains acyclic , this can be done by the above mentioned link.

And then finally print the answer . please tell if something is missing ! !

Yes, it's correct.

Just be careful to only print the edges (<= k) that the assigned direction is inverted compared with the original direction.

lclpsoz , but i have a doubt , while making a graph with edges > k , it is possible that every node may be not in our topolozical sort list , some nodes may get eliminated .

like in the 2nd example edges with weights >3(k)

are 2--->1 and 4--->3 but they are in different connected components , and 5 is not in our list . what to do in this case .

This is why you need to iterate over all nodes in the topological sort, so that no component of your graph get ignored.

Remember that in a lot of cases (like when the graph have multiple components) there are multiple valid topological orders.

Here are some valid topological orders for your example:

Notice that only two "orders" are defined, 2

have tocomebefore1 and 4have tocomebefore3, because of the two edges (2, 1) and (4, 3).To be clear, the topological order is a permutation of the vertices respecting the order of the existing edges, in this case, 2 -> 1 and 4 -> 3, so if the graph had no edges and had N vertices, it would have N! valid topological orders.

I

reallyrecommend that you read this article (translated from e-maxx) about topological sort, understand it completely and solve some of the proposed problems before trying to implement 1100E, the logic behind topological sort is very useful in a lot of problems.The way I see it is that in general you can check if a directed graph contains cycles by removing all nodes that only have incoming edges or only outgoing edges. Then do that procedure over and over again. If you end up with no nodes left then it didn't contain any cycles.

For this problem we can use binary search to figure out which edges we are freely allowed to switch the direction of. Note that in the procedure above these edges basically becomes wildcards so they can mostly be ignored, you just need to note down what direction you want them to be.