問題一覧 > 通常問題

No.1194 Replace

レベル : / AI推定難易度 : 1400 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 81
作問者 : define / テスター : Thistle
お気に入りにしたユーザー ProblemId : 4900 / 自分の提出
問題文最終更新日: 2020-08-22 11:48:02
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
技術室奥プログラミングコンテスト#5 Day1 (順位表) の他の問題:

Problem Statement

There is an integer sequence $A$ of length $N$. Initially, $A_i\ = i $ $(1 \leq i \leq N)$.

There are $M$ candidate operations for this sequence.

The $i$-th operation:
For every $j$ such that $A_j\ =\ B_i$, replace it with $A_j\ =\ C_i$

There is no limit on the number of times each operation may be performed, and some operations may never be performed.
Also, the operations may be performed in any order, and the series of operations may be ended at any time.

After finishing the series of operations, what is the maximum possible value of the sum of the elements of $A$?

Input

$N\ M$
$B_1\ C_1$
$\cdots$
$B_M\ C_M$

$2 \leq N \leq 10^9$
$1 \leq M \leq 2 \times 10^5$
$1 \leq B_i,C_i \leq N$
$B_i \neq C_i$

All inputs are integers.

Output

Print on one line the maximum possible sum of the elements of $A$ after the operations.

Sample

Sample 1
Input
5 5
2 3
1 2
4 5
3 4
5 4
Output
25

Before the operations: ${1,2,3,4,5}$
Operation 2: ${2,2,3,4,5}$
Operation 1: ${3,3,3,4,5}$
Operation 4: ${4,4,4,4,5}$
Operation 3: ${5,5,5,5,5}$

In this way, all elements can be made 5.

Sample 2
Input
4 3
4 3
3 2
2 1
Output
10

You do not have to perform any operations at all.

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

You may perform the same operation any number of times.

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