No.1194 Replace
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もしくは右上の雲マークをクリックしてアカウントを作成してください。