問題一覧 > 通常問題

No.317 Adding Edges

レベル : / AI推定難易度 : 1725 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 75
作問者 : catupper
お気に入りにしたユーザー ProblemId : 896 / 自分の提出
問題文最終更新日: 2026-06-26 06:08:06
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
Advent Calendar Contest 2015 (順位表) の他の問題:

Problem Statement

You are given a graph $G$ with $N$ vertices and $M$ edges.
For every integer $i$ such that $1 ≦ i ≦ N$, answer the following question.

Question. When you add as many edges as you like ($0$ or more) to the graph $G$, can you make a connected component with $i$ vertices? If so, what is the minimum number of edges to add?

Input

$N$ $M$
$u_1$ $v_1$
$u_2$ $v_2$
$\quad\vdots$
$u_M$ $v_M$

Line $1$ contains the integer $N(1 ≦ N ≦ 10^5)$, the number of vertices of the graph, and the integer $M(1 ≦ M ≦ 2×10^5)$, the number of edges, separated by a space. Line $i$ $(1 ≦ i ≦ M)$ of the following $M$ lines contains the numbers $u_i, v_i(1 ≦ u_i, v_i ≦ N)$ of the $2$ vertices connected by the $i$-th edge, separated by a space.
The graph $G$ may contain multiple edges and self-loops.

Output

The output consists of $N$ lines.
On line $i$ $(1 ≦ i ≦ N)$, output the answer to the question for that $i$.
If the answer is "cannot be made", output the integer $-1$.

Sample

Sample 1
Input
6 3
2 3
4 5
5 6
Output
0
0
0
1
1
2

There are connected components with $3$ vertices, $2$ vertices, and $1$ vertex, with $1$ component of each size.
Connected components with $1,2,3$ vertices can be made without adding any edges.
By adding $1$ edge, connected components with $4,5$ vertices can be made.
By adding $2$ edges, all the connected components can be joined to make a connected component with $6$ vertices.

Sample 2
Input
9 9
2 3
4 5
5 6
6 4
7 8
8 9
9 1
1 1
3 2
Output
-1
0
0
0
1
1
1
-1
2

Note that there may be self-loops and multiple edges.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。