2.Social Network Community
Spoj
You need to build an ADD friend feature. if 'x' sends a friend request to 'y', he may accept it or deny it.
SOCNET has a special feature called 'community'.When two people 'x' and 'y' becomes friends,the communities of two are merged together.(If 'x' has no friends, it's community consist of only himself)
Since, your friend is low on funds, the data center he uses has a restriction-the MAXIMUM size of any community cannot exceed 'm'.
You need to work on following three types of queries-
A x y - x sends a friend request to y
E x y - check whether x and y are present in same community(print 'Yes' or 'No')
S x - prints the size of the community 'x' belongs to.
NOTE- A friend requested is accepted only if the merger of the two communities results in a community not greater than 'm'.
Solution : (Union using rank , path compression)
Time Complexity: O(n)
Last updated