codeDominus's blog

By codeDominus, history, 7 years ago, In English

LINK for the problem I am learning DSU and came up with this question but could not apply DSU. It will be of great help if someone tell the logic for solving this problem.

UPD 1 :solved the question LINK for answer

Tags dsu
  • Vote: I like it
  • +4
  • Vote: I do not like it

| Write comment?
»
7 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Good day to you!

Firstly, set all colors to "UNDEF" [for example -1] {uncolored} and build graph (for example with vectors [I assume you can do this, if not, ask more!]).

Now for each query of type 1, set color to "y" and iterate over all "neighbors" and check whether their color is same — in case of "yes", connect them with DSU.

The second query just ask if their DSU-component is same.

Since "he colors each node at most once" there is no problem with "erasing connections" (even though it would be solvable too, just much harder).

Good Luck!

  • »
    »
    7 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    Thanks

    solved the question.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    Hi ! Sorry if I'm late, but can you tell me how do you solve it without that condition "he colors each node at most once" ?

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Hmm I got some doubts about it, because it is true that graph of "star-shape" with queries to the star (which means simple iteration over neighbors would time out) will "kill" it, but if it will be "reasonable" graph, then Link-Cut-Tree shall do it (instead of DSU).