問題一覧 > 通常問題

No.3751 Nonopoly

レベル : / AI推定難易度 : 1400 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 1024 MB / 標準ジャッジ問題
タグ : (解説公開後に AC するまで非表示) / 解いたユーザー数 41
作問者 : marc2825 / テスター : siganai
お気に入りにしたユーザー ProblemId : 13470 / 自分の提出
問題文最終更新日: 2026-10-02 19:23:20
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 516 (順位表) の他の問題:

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