Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COA `Map<String, Integer>` lookup is cheap (`O(1)`); you shouldn't be bothered by it unless it's proven to be a problem. Premature optimization is the root of all evils. This is even more true in this case, because there's a _BIG_ optimization that you still haven't discovered. You understood me right: you don't have to create the adjacency list at all. You can solve this problem in `O(V)` space. You can process the input in `O(V+E)` (which is optimal, since that's the size of the input), and after you're done reading the input, you can produce the output rightaway in `O(1)`.
      singulars
    2. COAnother way to save space without using the `Map<String,Integer>` is to `.intern()` the strings. A lot of names will repeat in the input, and they are redundantly stored in the memory multiple times. If you only store the `.intern()` of those names, the redundant copies will be garbage-collected. And as a bonus, if you consistently `.intern()` the names, you can use `==` and `!=` instead of `equals()`. Read about how string interning works. This is small time optimization, though. The really important one is the one in above comment.
      singulars
    3. COI know it has to do with testing bipartiteness/graph colouring .. but I am still thinking in my head to get an answer in O(1) just after creating a Map(name,int) of all the nodes!! The int in Map(name,int) can change as per distance , all even goes in 1 group and odd in other .. Problem with all these approaches, It requires an adjacency list :( to ensure that graph is traced in BFS or DFS format. Else you end up with a new node not knowing which group the node belongs to.. so it becomes difficult to know what nodes the children would go to I am missing something, am I on the right track ?
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload