#P15439. [蓝桥杯 2025 国 Python B] 连通块

    ID: 17459 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>2025广度优先搜索 BFS链表蓝桥杯国赛

[蓝桥杯 2025 国 Python B] 连通块

Problem Description

Given an undirected complete graph with nn nodes, delete mm edges from it. Find how many connected components the graph has.

Output the number of connected components, and output the details of each connected component.

Input Format

The first line of input contains two positive integers n,mn, m, separated by a space, representing the number of nodes and the number of deleted edges.

The next mm lines each contain two positive integers ui,viu_i, v_i, separated by a space, indicating that the edge between uiu_i and viv_i is deleted. It is guaranteed that the same edge will not be deleted twice.

Output Format

The first line of output contains an integer xx, representing the number of connected components.

The next xx lines each describe one connected component. The first number yy indicates the number of nodes in this connected component, followed by yy numbers giving the node indices in order, with a space between adjacent integers. Output the connected components in increasing order of their smallest node; within each connected component, output the nodes in increasing order.

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

Hint

Constraints and Conventions for Test Cases

For 40%40\% of the test cases, n1000n \le 1000.

For all test cases, 1n,m2×1051 \le n, m \le 2 \times 10^5, mn(n1)2m \le \dfrac{n(n-1)}{2}, and 1ui,vin1 \le u_i, v_i \le n.

Translated by ChatGPT 5