Connected components

http://opencup.ru/files/oce/gp6/problems-e.pdf

Any idea what should I do for problem D?

//