Can you please explain your logic? I have forgotten JAVA. Also, I think this is solved using dfs, you don’t seem to have used it, it’s good unless it’s wrong. Please do comment a short explanation for your logic.
Actually logic on which you solved this problem is wrong just check for the case when
N=6 and S=5 and relations=
only 0th person was infected but your code is giving output that all 6 were infected .
So correct your logic or in case you require any help behind logic just drop down a comment.