Problems > Friend Circles

Friend Circles

Difficulty: 1200 4000 ms 128 MB

graphs

N people are connected by M friendship links. Friends of friends belong to the same circle. How many circles are there (isolated people count as their own circle)?

Input: the first line has N and M; each of the next M lines has an edge u v (1-based).

Output: the number of connected components.

Constraints:

  • 1 ≤ N ≤ 104
  • 0 ≤ M ≤ 2×104
  • multiple edges and self-loops may appear

Sample Tests

Input 1
5 3
1 2
2 3
4 5
Output 1
2
Input 2
3 0
Output 2
3

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.