CF2206C.Upside Down Dijkstra

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

Your little sibling has a connected undirected graph of nn vertices and mm edges. The vertices are numbered from 11 to nn, and the edges are numbered from 11 to mm. Edge jj connects vertices uju_j and vjv_j with a positive integer weight of wjw_j.

Your little sibling coded Dijkstra's algorithm to find the shortest distances from vertex 11 to all other vertices, as shown in the pseudocode below. The array SS records the order in which each vertex is popped from the heap for the first time. Note that even though tuples for each vertex may be pushed multiple times into the heap, each vertex is added to SS exactly once.

However, your little sibling made a big mistake. In your sibling's code, the heap always pops the maximum tuple instead of the minimum tuple. Here, the heap orders tuples lexicographically by (dist,u)(\mathit{dist}, u), with larger dist\mathit{dist} considered larger; ties are broken by larger uu.

Your little sibling shares the graph structure with you, that is, all pairs of uju_j and vjv_j (1≤j≤m1 \le j \le m). However, your sibling does not share the edge weights wjw_j. Instead, your sibling shares an array S=(s1,s2,…,sn)S = (s_1, s_2, \ldots, s_n) with you and asks you to reconstruct the edge weights. Your task is to find integer edge weights w1,w2,…,wmw_1, w_2, \ldots, w_m (1≤wj≤1091 \leq w_j \leq 10^9 for all jj) such that running your sibling's incorrect code yields the given array SS.

Find any such assignment of edge weights. If no such assignment exists, report that it is impossible.

你的弟弟(或妹妹)有一个包含 nn 个顶点和 mm 条边的连通无向图。顶点编号为 11 到 nn,边编号为 11 到 mm。第 jj 条边连接顶点 uju_j 和 vjv_j,其权重为正整数 wjw_j。

你的弟弟(或妹妹)编写了 Dijkstra 算法来计算从顶点 11 到其余所有顶点的最短距离,伪代码如下所示。数组 SS 记录了每个顶点首次从堆中被弹出的顺序。注意:尽管每个顶点可能被多次压入堆中(以不同距离值),但每个顶点在 SS 中恰好只出现一次。

然而,你的弟弟(或妹妹)犯了一个严重错误:在其代码中,堆总是弹出最大元组,而非最小元组。此处,堆按字典序对元组 (dist,u)(\mathit{dist}, u) 进行排序,其中 dist\mathit{dist} 越大则元组越大;若 dist\mathit{dist} 相同,则 uu 越大者元组越大。

你的弟弟(或妹妹)将图的结构分享给了你,即所有边对应的顶点对 (uj,vj)(u_j, v_j)(1≤j≤m1 \le j \le m)。但并未提供边权 wjw_j。取而代之的是,他/她给你一个数组 S=(s1,s2,…,sn)S = (s_1, s_2, \ldots, s_n),并请你重构出满足条件的边权。你的任务是找出一组整数边权 w1,w2,…,wmw_1, w_2, \ldots, w_m(对所有 jj 满足 1≤wj≤1091 \leq w_j \leq 10^9),使得运行你弟弟(或妹妹)的错误版本代码后,恰好得到给定的数组 SS。

请输出任意一组满足条件的边权赋值。若不存在这样的赋值,请报告“不可能”。

输入格式

The first line of input contains two integers nn and mm (2≤n≤100 0002 \leq n \leq 100\,000; n−1≤m≤200 000n-1 \leq m \leq 200\,000).

The jj-th of the next mm lines contains two integers uju_j and vjv_j (1≤uj<vj≤n1 \leq u_j \lt v_j \leq n; (uj,vj)≠(uk,vk)(u_j, v_j) \neq (u_k, v_k) for all j≠kj \neq k). The input guarantees that the graph is connected.

The next line contains nn integers s1,s2,…,sns_1, s_2, \ldots, s_n (1≤si≤n1 \leq s_i \leq n; si≠sℓs_i \neq s_\ell for all i≠ℓi \neq \ell).

输入的第一行包含两个整数 nn 和 mm(2≤n≤100 0002 \leq n \leq 100\,000;n−1≤m≤200 000n-1 \leq m \leq 200\,000)。

接下来的 mm 行中,第 jj 行包含两个整数 uju_j 和 vjv_j(1≤uj<vj≤n1 \leq u_j \lt v_j \leq n;对所有 j≠kj \neq k,均有 (uj,vj)≠(uk,vk)(u_j, v_j) \neq (u_k, v_k))。输入保证该图是连通的。

下一行包含 nn 个整数 s1,s2,…,sns_1, s_2, \ldots, s_n(1≤si≤n1 \leq s_i \leq n;对所有 i≠ℓi \neq \ell,均有 si≠sℓs_i \neq s_\ell)。

输出格式

If no assignment of edge weights yields the given array SS, output impossible.

Otherwise, output a single line containing mm integers w1,w2,…,wmw_1, w_2, \ldots, w_m (1≤wj≤1091 \leq w_j \leq 10^9) for the weights of the edges such that your sibling's incorrect code yields the given array SS.

If there are multiple outputs, any one of them will be accepted.

It can be shown that if there exists any assignment of positive integer weights that yields SS, then there also exists one with 1≤wj≤1091 \le w_j \le 10^9 for all jj.

如果不存在任何边权赋值方案能够得到给定数组 SS,则输出 impossible。

否则,输出一行,包含 mm 个整数 w1,w2,…,wmw_1, w_2, \ldots, w_m(满足 1≤wj≤1091 \leq w_j \leq 10^9),表示各条边的权重,使得你兄弟/姐妹的错误代码恰好生成给定数组 SS。

若存在多种可行输出,输出任意一种即可。

可以证明:若存在某种正整数权重赋值方案能生成 SS,则必存在一种满足对所有 jj 均有 1≤wj≤1091 \le w_j \le 10^9 的方案。

输入输出样例

  • 输入#1

    5 7
    3 4
    2 3
    1 2
    3 5
    1 4
    1 5
    4 5
    1 4 3 5 2

    输出#1

    6 1 3 1 3 2 2
  • 输入#2

    4 4
    1 2
    2 3
    1 3
    2 4
    1 3 4 2

    输出#2

    impossible

说明/提示

Explanation for the sample input/output #1

Figure C.1 illustrates the structure of the graph and the assignment of edge weights in the sample output.

Figure C.1: Graph with assigned edge weights.

With this assignment, your sibling's incorrect code runs as follows:

  1. Initially, the heap contains (0,1){(0, 1)}.
  2. From (0,1)(0, 1), vertex 11 is popped from the heap for the first time. Now the heap contains (3,4),(3,2),(2,5){(3, 4), (3, 2), (2, 5)}.
  3. From (3,4)(3, 4), vertex 44 is popped. Now the heap contains (9,3),(6,1),(5,5),(3,2),(2,5){(9, 3), (6, 1), (5, 5), (3, 2), (2, 5)}.
  4. From (9,3)(9, 3), vertex 33 is popped. Now the heap contains (15,4),(10,5),(10,2),(6,1),(5,5),(3,2),(2,5){(15, 4), (10, 5), (10, 2), (6, 1), (5, 5), (3, 2), (2, 5)}.
  5. From (10,5)(10, 5), vertex 55 is popped. Now the heap contains (12,4),(12,1),(11,3),(10,2),(6,1),(5,5),(3,2),(2,5){(12, 4), (12, 1), (11, 3), (10, 2), (6, 1), (5, 5), (3, 2), (2, 5)}.
  6. From (10,2)(10, 2), vertex 22 is popped.

This results in S=(1,4,3,5,2)S = (1, 4, 3, 5, 2).

样例输入/输出 #1 的说明

图 C.1 展示了样例输出中图的结构以及边权的分配方式。

图 C.1:已分配边权的图。

在此边权分配下,你兄弟/姐妹的错误代码执行过程如下:

  1. 初始时,堆中包含 (0,1){(0, 1)}。
  2. 从 (0,1)(0, 1) 中弹出顶点 11(首次弹出)。此时堆中包含 (3,4),(3,2),(2,5){(3, 4), (3, 2), (2, 5)}。
  3. 从 (3,4)(3, 4) 中弹出顶点 44。此时堆中包含 (9,3),(6,1),(5,5),(3,2),(2,5){(9, 3), (6, 1), (5, 5), (3, 2), (2, 5)}。
  4. 从 (9,3)(9, 3) 中弹出顶点 33。此时堆中包含 (15,4),(10,5),(10,2),(6,1),(5,5),(3,2),(2,5){(15, 4), (10, 5), (10, 2), (6, 1), (5, 5), (3, 2), (2, 5)}。
  5. 从 (10,5)(10, 5) 中弹出顶点 55。此时堆中包含 (12,4),(12,1),(11,3),(10,2),(6,1),(5,5),(3,2),(2,5){(12, 4), (12, 1), (11, 3), (10, 2), (6, 1), (5, 5), (3, 2), (2, 5)}。
  6. 从 (10,2)(10, 2) 中弹出顶点 22。

最终得到 S=(1,4,3,5,2)S = (1, 4, 3, 5, 2)。

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

首页