AtCoder Grand Contest 045
E
Fragile Balls
AtCoder
未评级
原题
← DF →
翻译中…

Problem Statement

翻译中…

We have $N$ boxes numbered $1$ to $N$, and $M$ balls numbered $1$ to $M$. Currently, Ball $i$ is in Box $A_i$.

翻译中…

You can do the following operation:

翻译中…
  • Choose a box containing two or more balls, pick up one of the balls from that box, and put it into another box.
翻译中…

Since the balls are very easy to break, you cannot move Ball $i$ more than $C_i$ times in total. Within this limit, you can do the operation any number of times.

翻译中…

Your objective is to have Ball $i$ in Box $B_i$ for every $i$ ($1 \leq i \leq M$). Determine whether this objective is achievable. If it is, also find the minimum number of operations required to achieve it.

翻译中…

Constraints

翻译中…
  • $1 \leq N \leq 10^5$
  • $1 \leq M \leq 10^5$
  • $1 \leq A_i,B_i \leq N$
  • $1 \leq C_i \leq 10^5$
  • In the situation where the objective is achieved, every box contains one or more balls. That is, for every $i$ ($1 \leq i \leq N$), there exists $j$ such that $B_j=i$.
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

N M
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M

翻译中…

Output

翻译中…

If the objective is unachievable, print $-1$; if it is achievable, print the minimum number of operations required to achieve it.

翻译中…

样例

Input #1
3 3
1 2 1
2 1 1
1 3 2
Output #1
3
We can achieve the objective in three operations, as follows: - Pick up Ball $1$ from Box $1$ and put it into Box $2$. - Pick up Ball $2$ from Box $2$ and put it into Box $1$. - Pick up Ball $3$ from Box $1$ and put it into Box $3$.
Input #2
2 2
1 2 1
2 1 1
Output #2
-1
Input #3
5 5
1 2 1
2 1 1
1 3 2
4 5 1
5 4 1
Output #3
6
Input #4
1 1
1 1 1
Output #4
0
题目信息
题号
agc045_e
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。