#abc284c. C - Count Connected Components

C - Count Connected Components

Score : 300300 points

问题描述

你得到一个简单的无向图,其中包含 NN 个顶点,编号从 11NN,以及 MM 条边,编号从 11MM。第 ii 条边连接顶点 uiu_i 和顶点 viv_i

请找出此图中连通分量的数量。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a simple undirected graph with NN vertices numbered 11 to NN and MM edges numbered 11 to MM. Edge ii connects vertex uiu_i and vertex viv_i.
Find the number of connected components in this graph.

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.

Constraints

  • 1N1001 \leq N \leq 100
  • 0MN(N1)20 \leq M \leq \frac{N(N - 1)}{2}
  • 1ui,viN1 \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:

NN MM

u1u_1 v1v_1

u2u_2 v2v_2

\vdots

uMu_M vMv_M

Output

Print the answer.

Sample Input 1

5 3
1 2
1 3
4 5

Sample Output 1

2

The given graph contains the following two connected components:

  • a subgraph formed from vertices 11, 22, 33, and edges 11, 22;
  • a subgraph formed from vertices 44, 55, and edge 33.

image

Sample Input 2

5 0

Sample Output 2

5

Sample Input 3

4 6
1 2
1 3
1 4
2 3
2 4
3 4

Sample Output 3

1

update @ 2024/3/10 11:53:42