All Tracks Data Structures Disjoint Data Structures Basics of Disjoint Data Structures Problem

Count Friends
Tag(s):

Disjoint Set, Easy, approved

Problem
Editorial
Analytics

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 $$i^{th}$$ student where $$1 \leq i \leq N$$.

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 $$i^{th}$$ student.

Constraints:
$$1 \leq N \leq 10^5$$
$$1 \leq M \leq 10^5$$
$$1 \leq u, v \leq N$$

SAMPLE INPUT
4 3
4 3
2 4
2 3
SAMPLE OUTPUT
0 2 2 2 
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 $$.

Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Visual Basic, Kotlin

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

This Problem was Asked in

HackerEarth

Challenge Name

Code Monk (Disjoint Set Union (Union Find))

OTHER PROBLEMS OF THIS CHALLENGE
Notifications
View All Notifications