AT_awtf2026algo_a.Min Cut of Graph of Min Weight

入门

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There is a weighted tree TT with NN vertices numbered 11 to NN. The ii-th edge of TT connects vertices AiA_i and BiB_i with weight CiC_i.

We now construct a complete undirected graph GG with NN vertices numbered 11 to NN, based on TT. For each edge of GG, the capacity is defined as follows.

  • The capacity of edge (i,j)(i,j) of GG is the minimum weight of an edge contained in the path connecting vertices ii and jj on TT.

Let f(i,j)f(i,j) be the capacity of the minimum cut separating vertices ii and jj on GG.

Find ∑1≤i<j≤Nf(i,j)\sum_{1 \leq i < j \leq N} f(i,j), modulo 998244353998244353.

Solve SS cases for each input.

存在一棵带权树 TT,其包含 NN 个编号为 11 至 NN 的顶点。树 TT 的第 ii 条边连接顶点 AiA_i 和 BiB_i,权重为 CiC_i。

现在,我们基于 TT 构造一个包含 NN 个编号为 11 至 NN 的顶点的无向完全图 GG。对于 GG 中的每条边,其容量定义如下:

  • 图 GG 中边 (i,j)(i,j) 的容量等于树 TT 上连接顶点 ii 和 jj 的路径中所含边的最小权重。

令 f(i,j)f(i,j) 表示图 GG 中分离顶点 ii 和 jj 的最小割的容量。

求 ∑1≤i<j≤Nf(i,j)\sum_{1 \leq i < j \leq N} f(i,j) 对 998244353998244353 取模的结果。

对每组输入,需解决 SS 个测试用例。

输入格式

The input is given from Standard Input in the following format:

SS
case1case_1
case2case_2
⋮\vdots
caseScase_S

Each test case is given in the following format:

NN
A1A_1 B1B_1 C1C_1
A2A_2 B2B_2 C2C_2
⋮\vdots
AN−1A_{N-1} BN−1B_{N-1} CN−1C_{N-1}

输入从标准输入中按以下格式给出:

SS
case1case_1
case2case_2
⋮\vdots
caseScase_S

每个测试用例按以下格式给出:

NN
A1A_1 B1B_1 C1C_1
A2A_2 B2B_2 C2C_2
⋮\vdots
AN−1A_{N-1} BN−1B_{N-1} CN−1C_{N-1}

输出格式

For each test case, output the answer.

对于每个测试用例,输出答案。

输入输出样例

  • 输入#1

    4
    3
    1 2 1
    2 3 10
    4
    1 2 1
    2 3 10
    3 4 2
    13
    11 4 337329830
    13 1 72247
    4 1 1768959
    5 4 5399893
    2 8 1832265
    12 7 107755
    10 4 743
    5 12 95
    4 3 389684075
    2 6 1222
    11 8 253280162722
    9 4 21671
    15
    8 5 285187324995
    14 10 755031423304
    2 8 88860861719
    12 7 596982637940
    10 4 225447687713
    7 15 210989590191
    13 5 836365489027
    6 15 859904883890
    8 1 362117197524
    12 8 422952343663
    1 14 112179584332
    15 11 487330735107
    12 9 528451854379
    3 7 343910842803

    输出#1

    15
    32
    13620068
    909241492

说明/提示

Sample 1 Explanation:
In the first test case, the capacities of edges (1,2),(1,3),(2,3)(1,2),(1,3),(2,3) of GG are 1,1,101,1,10, respectively. The answer is f(1,2)+f(1,3)+f(2,3)=2+2+11=15f(1,2)+f(1,3)+f(2,3)=2+2+11=15.

Constraints

  • 1≤S≤1250001 \leq S \leq 125000
  • 2≤N≤2500002 \leq N \leq 250000
  • 1≤Ai,Bi≤N1 \leq A_i,B_i \leq N
  • 1≤Ci≤10121 \leq C_i \leq 10^{12}
  • The input graph is a tree.
  • The sum of NN over the SS cases is at most 250000250000.
  • All input values are integers.

样例 1 解释:
在第一个测试用例中,图 GG 中边 (1,2)(1,2)、(1,3)(1,3)、(2,3)(2,3) 的容量分别为 11、11、1010。答案为 f(1,2)+f(1,3)+f(2,3)=2+2+11=15f(1,2)+f(1,3)+f(2,3)=2+2+11=15。

约束条件

  • 1≤S≤1250001 \leq S \leq 125000
  • 2≤N≤2500002 \leq N \leq 250000
  • 1≤Ai,Bi≤N1 \leq A_i,B_i \leq N
  • 1≤Ci≤10121 \leq C_i \leq 10^{12}
  • 输入图是一棵树。
  • 所有 SS 个测试用例的 NN 值之和不超过 250000250000。
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页