LINK TO THE PROBLEM ( http://codeforces.com/contest/216/problem/B )
I am not getting the logic …I also tried to learn form others code but …again m not getting their logic .here is the the link of a solution( http://codeforces.com/contest/216/submission/16612019 ) but i am not able to understand why the root() function is designed like this and how it works…

Read this complete article and I am sure You will get the idea of Root() function.

I will solve this question after some time, then I will suggest you the approach.But I recommend you to read this article first.This will surely help you to solve many others problems too.

I guess you have to print number of close loop in given graph,where number of students are node and given pair are edges.

So basically root() function here tells you about close loop formed in given graph.This is concept is basically used in Kruskal algo to do the check for closed loop.