CF1764F.Doremy's Experimental Tree

省选/NOI-

通过率:0%

时间限制:2.50s

内存限制:256MB

AC君温馨提醒

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

题目描述

Doremy has an edge-weighted tree with nn vertices whose weights are integers between 11 and 10910^9. She does n(n+1)2\frac{n(n+1)}{2} experiments on it.

In each experiment, Doremy chooses vertices ii and jj such that j≤ij \leq i and connects them directly with an edge with weight 11. Then, there is exactly one cycle (or self-loop when i=ji=j) in the graph. Doremy defines f(i,j)f(i,j) as the sum of lengths of shortest paths from every vertex to the cycle.

Formally, let disi,j(x,y)\mathrm{dis}_{i,j}(x,y) be the length of the shortest path between vertex xx and yy when the edge (i,j)(i,j) of weight 11 is added, and Si,jS_{i,j} be the set of vertices that are on the cycle when edge (i,j)(i,j) is added. Then,

f(i,j)=sum_x=1nleft(min_yinS_i,jmathrmdis_i,j(x,y)right).f(i,j)=\\sum\_{x=1}^{n}\\left(\\min\_{y\\in S\_{i,j}}\\mathrm{dis}\_{i,j}(x,y)\\right).

Doremy writes down all values of f(i,j)f(i,j) such that 1≤j≤i≤n1 \leq j \leq i \leq n, then goes to sleep. However, after waking up, she finds that the tree has gone missing. Fortunately, the values of f(i,j)f(i,j) are still in her notebook, and she knows which ii and jj they belong to. Given the values of f(i,j)f(i,j), can you help her restore the tree?

It is guaranteed that at least one suitable tree exists.

Doremy 有一棵含 nn 个顶点的边权树,各边权值为 11 到 10910^9 之间的整数。她在该树上进行了 n(n+1)2\frac{n(n+1)}{2} 次实验。

每次实验中,Doremy 选择两个顶点 ii 和 jj(满足 j≤ij \leq i),并用一条权值为 11 的边直接连接它们。此时图中恰好形成一个环(当 i=ji = j 时为自环)。Doremy 将 f(i,j)f(i,j) 定义为:所有顶点到该环的最短路径长度之和。

形式化地,设 disi,j(x,y)\mathrm{dis}_{i,j}(x,y) 表示在添加权值为 11 的边 (i,j)(i,j) 后,顶点 xx 与 yy 之间的最短路径长度;设 Si,jS_{i,j} 为添加边 (i,j)(i,j) 后构成的环上的顶点集合。则:

f(i,j)=∑x=1n(min⁡y∈Si,jdisi,j(x,y)).f(i,j)=\sum_{x=1}^{n}\left(\min_{y\in S_{i,j}}\mathrm{dis}_{i,j}(x,y)\right).

Doremy 将所有满足 1≤j≤i≤n1 \leq j \leq i \leq n 的 f(i,j)f(i,j) 值记录下来,然后入睡。然而醒来后,她发现原树已丢失。幸运的是,她的笔记本中仍保留着所有 f(i,j)f(i,j) 的值,并且她清楚每个值对应的 ii 和 jj。已知这些 f(i,j)f(i,j) 的值,你能帮她恢复出原来的树吗?

题目保证至少存在一棵满足条件的树。

输入格式

The first line of input contains a single integer nn (2≤n≤20002 \le n \le 2000) — the number of vertices in the tree.

The following nn lines contain a lower-triangular matrix with ii integers on the ii-th line; the jj-th integer on the ii-th line is f(i,j)f(i,j) (0≤f(i,j)≤2⋅10150 \le f(i,j) \le 2\cdot 10^{15}).

It is guaranteed that there exists a tree whose weights are integers between 11 and 10910^9 such that the values of f(i,j)f(i,j) of the tree match those given in the input.

输入的第一行包含一个整数 nn(2≤n≤20002 \le n \le 2000),表示树中顶点的数量。

接下来的 nn 行包含一个下三角矩阵,其中第 ii 行有 ii 个整数;第 ii 行第 jj 个整数为 f(i,j)f(i,j)(0≤f(i,j)≤2⋅10150 \le f(i,j) \le 2\cdot 10^{15})。

保证存在一棵树,其边权均为 11 到 10910^9 之间的整数,且该树对应的 f(i,j)f(i,j) 值与输入中给出的值完全一致。

输出格式

Print n−1n-1 lines describing the tree. In the ii-th line of the output, output three integers uiu_i, viv_i, wiw_i (1≤ui,vi≤n1 \le u_i,v_i \le n, 1≤wi≤1091 \le w_i \le 10^9), representing an edge (ui,vi)(u_i,v_i) whose weight is wiw_i.

If there are multiple answers, you may output any.

All edges must form a tree and all values of f(i,j)f(i,j) must match those given in the input.

输出 n−1n-1 行,描述该树。在输出的第 ii 行中,输出三个整数 uiu_i、viv_i、wiw_i(其中 1≤ui,vi≤n1 \le u_i,v_i \le n,1≤wi≤1091 \le w_i \le 10^9),表示一条权重为 wiw_i 的边 (ui,vi)(u_i,v_i)。

若存在多个合法答案,输出任意一个即可。

所有边必须构成一棵树,且所有 f(i,j)f(i,j) 的值必须与输入中给出的值完全一致。

输入输出样例

  • 输入#1

    3
    7
    3 5
    0 2 8

    输出#1

    2 3 3
    1 2 2
  • 输入#2

    9
    8081910646
    8081902766 8081903751
    8081902555 8081903540 8081905228
    3090681001 3090681986 3090681775 7083659398
    7083657913 7083658898 7083658687 2092437133 15069617722
    1748216295 1748217280 1748217069 5741194692 749972427 10439821163
    2377558289 2377559274 2377559063 6370536686 1379314421 5028071980 8866466178
    1746983932 1746984917 1746984706 5739962329 748740064 10438588800 5026839617 10448447704
    2341942133 2341943118 2341942907 6334920530 1343698265 4992455824 8830850022 4991223461 9115779270

    输出#2

    1 2 985
    2 3 211
    2 4 998244353
    2 5 998244853
    4 6 671232353
    6 8 1232363
    4 7 356561356
    7 9 35616156

说明/提示

In the first test case, the picture below, from left to right, from top to bottom, shows the graph when pairs (1,1)(1,1), (1,2)(1,2), (1,3)(1,3), (2,2)(2,2), (2,3)(2,3), (3,3)(3,3) are connected with an edge, respectively. The nodes colored yellow are on the cycle.

在第一个测试用例中,下图从左到右、从上到下依次展示了分别连接边 (1,1)(1,1)、(1,2)(1,2)、(1,3)(1,3)、(2,2)(2,2)、(2,3)(2,3)、(3,3)(3,3) 时对应的图。图中黄色标记的节点位于环上。

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

首页