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

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

1 | tourist | 3851 |

2 | jiangly | 3634 |

3 | Um_nik | 3539 |

4 | slime | 3498 |

5 | ksun48 | 3493 |

6 | djq_cpp | 3486 |

7 | maroonrk | 3471 |

8 | MiracleFaFa | 3466 |

9 | Radewoosh | 3442 |

10 | Petr | 3426 |

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

1 | -is-this-fft- | 183 |

2 | awoo | 181 |

3 | YouKn0wWho | 177 |

4 | Um_nik | 175 |

5 | dario2994 | 172 |

6 | Monogon | 170 |

6 | adamant | 170 |

8 | maroonrk | 169 |

9 | errorgorn | 166 |

10 | antontrygubO_o | 165 |

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

↑

↓

Codeforces (c) Copyright 2010-2022 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Aug/18/2022 16:25:13 (h3).

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.Thanks , now I understood . Also , Thanks for the link .

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.

Thanks !

most common solutions are , we can find best k , make a graph with edges > k , then top. sort the graph

assign undirected edges to nodes having edge wt. <=k ,

the according to the top. sort list , we can assign directions to the undirected edges