#!perl -nla
($a,$x,$y)=map{$b=$_;$d[$_]=$b=$@while$@=$d[$b];$b}@F;$a*$y?print$x-$y?NO:YES:$x-$y?$d[$x]=$y:0
__END__
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Note that non-ascii characters in the above source code will be escaped (such as \x9f).