No.3751 Nonopoly
Problem Statement
There are \(N\) companies and \(M\) contracts between them. The companies are numbered \(1,2,\ldots,N\). Contract \(i\) is between company \(u_i\) and company \(v_i\), and its value is \(W_i\).
Alice and Bob play a game in which, taking turns starting with Alice, each player chooses one company that has not yet been acquired, acquires it, and adds it to their own group. The game continues until all companies have been acquired.
After the game ends, each player's profit is defined as the sum of the values of the contracts between companies that both belong to that player's group.
The difference between the two players' profits at the end of the game, \((\text{Alice's profit})-(\text{Bob's profit})\), is called the "final score" of this game.
Alice tries to maximize the final score, and Bob tries to minimize it. Find the final score when both players act optimally.
Constraints
- \(1 \le N \le 2 \times 10^5\)
- \(0 \le M \le \min(2 \times 10^5,\frac{N(N-1)}{2})\)
- \(1 \le u_i < v_i \le N\)
- All \((u_i,v_i)\) are distinct
- \(1 \le W_i \le 10^9\)
- All input values are integers
Input
The input is given from standard input in the following format.
$N$ $M$ $u_1$ $v_1$ $W_1$ $u_2$ $v_2$ $W_2$ $\vdots$ $u_M$ $v_M$ $W_M$
Output
Output in one line the final score when both players act optimally.
Sample
Sample 1
Input
5 4 1 3 10 1 4 2 2 4 6 2 5 6
Output
4
For example, suppose the companies are acquired in the order
\(2 \to 4 \to 5 \to 1 \to 3\).
Since Alice and Bob acquire companies alternately, Alice's group ends up as
\(\{2,3,5\}\) and Bob's group as \(\{1,4\}\).
The only contract added to Alice's profit is the one between company \(2\) and company \(5\), whose value is \(6\).
The only contract added to Bob's profit is the one between company \(1\) and company \(4\), whose value is \(2\).
Therefore, the final score is \(6-2=4\), which matches the final score when both players act optimally. (Note that these moves are not necessarily optimal.)
Sample 2
Input
3 1 1 2 7
Output
0
There may be companies that have no contracts with any company.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
siganai