Count Connected Components

AtCoder
IDabc284_c
Time2000ms
Memory256MB
Difficulty
You are given a simple undirected graph with $N$ vertices numbered $1$ to $N$ and $M$ edges numbered $1$ to $M$. Edge $i$ connects vertex $u_i$ and vertex $v_i$. Find the number of connected components in this graph. ## Constraints * $1 \leq N \leq 100$ * $0 \leq M \leq \frac{N(N - 1)}{2}$ * $1 \leq u_i, v_i \leq N$ * The given graph is simple. * All values in the input are integers. ## Input The input is given from Standard Input in the following format: $N$ $M$ $u_1$ $v_1$ $u_2$ $v_2$ $\vdots$ $u_M$ $v_M$ [samples] ## Notes A **simple undirected graph** is a graph that is simple and has undirected edges. A graph is **simple** if and only if it has no self-loop or multi-edge. A **subgraph** of a graph is a graph formed from some of the vertices and edges of that graph. A graph is **connected** if and only if one can travel between every pair of vertices via edges. A **connected component** is a connected subgraph that is not part of any larger connected subgraph.
Samples
Input #1
5 3
1 2
1 3
4 5
Output #1
2

The given graph contains the following two connected components:

*   a subgraph formed from vertices $1$, $2$, $3$, and edges $1$, $2$;
*   a subgraph formed from vertices $4$, $5$, and edge $3$.

![image](https://img.atcoder.jp/ghi/abc284c_095531536446f41896b10cf15a3e4d158ce081ef1b6fe656788371718b39b3fb.jpg)
Input #2
5 0
Output #2
5
Input #3
4 6
1 2
1 3
1 4
2 3
2 4
3 4
Output #3
1
API Response (JSON)
{
  "problem": {
    "name": "Count Connected Components",
    "description": {
      "content": "You are given a simple undirected graph with $N$ vertices numbered $1$ to $N$ and $M$ edges numbered $1$ to $M$. Edge $i$ connects vertex $u_i$ and vertex $v_i$.   Find the number of connected compone",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc284_c"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a simple undirected graph with $N$ vertices numbered $1$ to $N$ and $M$ edges numbered $1$ to $M$. Edge $i$ connects vertex $u_i$ and vertex $v_i$.  \nFind the number of connected compone...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments