問題一覧 > 通常問題

No.1553 Lovely City

レベル : / AI推定難易度 : 1550 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / スペシャルジャッジ問題 (複数の解が存在する可能性があります)
タグ : (AC するまで非表示) / 解いたユーザー数 68
作問者 : first_vil / テスター : 沙耶花
お気に入りにしたユーザー ProblemId : 6435 / 自分の提出
問題文最終更新日: 2021-09-20 14:41:39
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
yukicoder contest 300 (順位表) の他の問題:

Problem Statement

There is a graph with $N$ vertices and $0$ edges. You want to add some directed edges to this graph so that the following condition is satisfied.

  • For every integer $i\ (1 \le i \le M)$, there exists a directed path from vertex $U_i$ to vertex $V_i$.

Among the ways of adding directed edges that satisfy this condition, show one that minimizes the number of added edges.

Input

$N$ $M$
$U_1$ $V_1$
$U_2$ $V_2$
$\vdots$
$U_M$ $V_M$
  • All inputs are integers.
  • $2 \le N \le 2 \times 10^5$
  • $1 \le M \le 2 \times 10^5$
  • $1 \le U_i,V_i \le N$
  • $U_i \neq V_i$
  • If $i \neq j$, then $(U_i,V_i) \neq (U_j,V_j)$

Output

On line $1$, output the number of added edges $K$. On line $i+1\ (1 \le i \le K)$, output $A_i$ and $B_i$ separated by a space, where the $i$-th directed edge goes from vertex $A_i$ to vertex $B_i$. End the output with a newline.

If there are multiple ways of adding directed edges that minimize the number of added edges, you may output any of them. However, $1 \le A_i,B_i \le N$ must be satisfied.

$K$
$A_1$ $B_1$
$A_2$ $B_2$
$\vdots$
$A_K$ $B_K$

Sample

Sample 1
Input
3 2
1 2
1 3
Output
2
1 2
1 3

There exist directed paths from vertex $1$ to vertex $2$ and from vertex $1$ to vertex $3$, so this output satisfies the condition. The condition cannot be satisfied with $1$ or fewer directed edges, so the minimum number of added edges is $2$.

Sample 2
Input
8 10
4 5
3 4
6 8
5 7
2 4
1 6
3 6
1 8
2 8
1 2
Output
7
3 1
1 6
6 2
2 8
8 4
4 5
5 7

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