2982 - 2024_IDS_Spring_Lab5 Scoreboard

Time

2024/04/01 01:00:00 2024/04/08 23:59:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
14275 2024_IDS_Spring_Lab5_Graph Connected Component

14275 - 2024_IDS_Spring_Lab5_Graph Connected Component   

Description

We call an undirected graph connected if there exists a path (u, v) for any two vertices u, v in an undirected graph.

A connected component of an undirected graph is a connected subgraph that is not part of any larger connected subgraph.

Note: an isolated vertex which can not reached by any other vertices should be considered as one connected component.

Given an undirected graph G = (V, E), note that:
- G may not be connected.
- G doesn't have multiple edges and self-loops.

There are n vertices in V, denoted by 1, 2, ..., n.

Please answer the number of connected components in graph G.

Input

The first line of the input contains an integer — the number of testcases.

The first line of each testcase contains two integers and — the size of V and the size of E.

Then m lines follow, each line contains two integers ui and vi (1 <= ui, v<= n ; ui != vi), being an edge in E.

Restrictions

For test ID 1 (each 20 points):
- 1 <= t <= 5
- 2 <= n <= 103
- 1 <= m <= min( n (n - 1) / 2, 2 * 105)

For test ID 2-5 (each 20 points):
- 1 <= t <= 5
- 2 <= n <= 105
- 1 <= m <= min( n (n - 1) / 2, 2 * 105)

Output

For each testcase, output one line, which answer the number of connected components in graph G.

Remember there shouldn't be a ‘\n’ at the end of the last line of output.

Sample Input  Download

Sample Output  Download

Tags




Discuss