CF920E.Connected Components?

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an undirected graph consisting of n vertices and edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x, y) such that there is no edge between x and y, and if some pair of vertices is not listed in the input, then there is an edge between these vertices.

You have to find the number of connected components in the graph and the size of each component. A connected component is a set of vertices X such that for every two vertices from this set there exists at least one path in the graph connecting these vertices, but adding any other vertex to X violates this rule.

给你一个包含 nn 个顶点和 n(n−1)2−m\frac{n(n-1)}{2} - m 条边的无向图。我们并不直接给出图中存在的边,而是给出 mm 个无序对 (x, y)(x,\,y),表示顶点 xx 与 yy 之间不存在边;而若某对顶点未在输入中列出,则它们之间存在一条边。

你需要求出该图中连通分量的个数,以及每个连通分量的大小。连通分量是指一个顶点集合 XX,满足:对其中任意两个顶点,图中均存在一条路径将它们连接;但若向 XX 中添加任一其他顶点,则上述性质不再成立。

输入格式

The first line contains two integers n and m (1 ≤ n ≤ 200000, ).

Then m lines follow, each containing a pair of integers x and y (1 ≤ x, y ≤ n, x ≠ y) denoting that there is no edge between x and y. Each pair is listed at most once; (x, y) and (y, x) are considered the same (so they are never listed in the same test). If some pair of vertices is not listed in the input, then there exists an edge between those vertices.

第一行包含两个整数 nn 和 mm(1≤n≤2000001 \leq n \leq 200000,)。

接下来是 mm 行,每行包含一对整数 xx 和 yy(1≤x,y≤n1 \leq x, y \leq n,x≠yx \neq y),表示顶点 xx 与 yy 之间不存在边。每对顶点至多出现一次;(x,y)(x, y) 与 (y,x)(y, x) 被视为相同(因此在同一个测试用例中不会同时出现)。若某对顶点未在输入中列出,则它们之间存在一条边。

输出格式

Firstly print k — the number of connected components in this graph.

Then print k integers — the sizes of components. You should output these integers in non-descending order.

首先输出 k —— 该图中连通分量的个数。

然后输出 k 个整数 —— 各连通分量的大小。你需要按非降序输出这些整数。

输入输出样例

  • 输入#1

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

    输出#1

    2
    1 4

输入解题思路,AI测评打分。不知道怎么写?

首页