Count Friends

0

0 votes
Disjoint set, Disjoint data structures
Problem

There are N students and M relationships of the form u v, which means that student u and student v are friends. If two students are not friends directly but they have a mutual friend, then they too become friends. Your task is to count the number of friends of the ith student where 1iN.

Input:
The first line consists of two integers N and M denoting the number of students and the number of relationships respectively.
The next M lines consists of two integers u and v denoting that student u and student v are friends. u and v can never be equal and relationships are not repeated.

Output:
Print N space separated integers which tells us the number of friends of the ith student.

Constraints:
1N105
1M105
1u,vN

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the sample test case -
Student 1 has no friends.
Student 2 is friends with student 3 and 4.
Student 3 is friends with student 2 and 4.
Student 4 is friends with student 2 and 3.

Editor Image

?