CF1472G.Moving to the Capital

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There are nn cities in Berland. The city numbered 11 is the capital. Some pairs of cities are connected by a one-way road of length 1.

Before the trip, Polycarp for each city found out the value of did_i — the shortest distance from the capital (the 11-st city) to the ii-th city.

Polycarp begins his journey in the city with number ss and, being in the ii-th city, chooses one of the following actions:

  1. Travel from the ii-th city to the jj-th city if there is a road from the ii-th city to the jj-th and di<djd_i \lt d_j;
  2. Travel from the ii-th city to the jj-th city if there is a road from the ii-th city to the jj-th and di≥djd_i \geq d_j;
  3. Stop traveling.

Since the government of Berland does not want all people to come to the capital, so Polycarp no more than once can take the second action from the list. in other words, he can perform the second action 00 or 11 time during his journey. Polycarp, on the other hand, wants to be as close to the capital as possible.

For example, if n=6n = 6 and the cities are connected, as in the picture above, then Polycarp could have made the following travels (not all possible options):

  • 2→5→1→2→52 \rightarrow 5 \rightarrow 1 \rightarrow 2 \rightarrow 5;
  • 3→6→23 \rightarrow 6 \rightarrow 2;
  • 1→3→6→2→51 \rightarrow 3 \rightarrow 6 \rightarrow 2 \rightarrow 5.

Polycarp wants for each starting city ii to find out how close he can get to the capital. More formally: he wants to find the minimal value of djd_j that Polycarp can get from the city ii to the city jj according to the rules described above.

Berland 有 nn 座城市,编号为 11 的城市是首都。某些城市对之间由一条长度为 11 的单向道路连接。

旅行前,Polycarp 对每座城市 ii 计算出了 did_i —— 从首都(即第 11 号城市)到第 ii 号城市的最短距离。

Polycarp 从编号为 ss 的城市出发,在位于第 ii 号城市时,他可选择以下动作之一:

  1. 若存在一条从第 ii 号城市指向第 jj 号城市的道路,且满足 di<djd_i \lt d_j,则从第 ii 号城市前往第 jj 号城市;
  2. 若存在一条从第 ii 号城市指向第 jj 号城市的道路,且满足 di≥djd_i \geq d_j,则从第 ii 号城市前往第 jj 号城市;
  3. 停止旅行。

由于 Berland 政府不希望所有人都涌向首都,因此 Polycarp 至多只能执行一次 上述第 2 类动作;换言之,他在整个旅途中执行第 2 类动作的次数为 00 或 11 次。而 Polycarp 自身则希望尽可能靠近首都。

例如,若 n=6n = 6,且各城市之间的连接关系如上图所示,则 Polycarp 可能进行如下行程(并非全部可能方案):

  • 2→5→1→2→52 \rightarrow 5 \rightarrow 1 \rightarrow 2 \rightarrow 5;
  • 3→6→23 \rightarrow 6 \rightarrow 2;
  • 1→3→6→2→51 \rightarrow 3 \rightarrow 6 \rightarrow 2 \rightarrow 5。

Polycarp 希望对每个起始城市 ii,求出他所能达到的、离首都最近的距离。更准确地说:他希望对每个起始城市 ii,找出在上述规则下,从城市 ii 出发所能到达的城市 jj 中,djd_j 的最小可能值。

输入格式

The first line contains one integer tt (1≤t≤1041 \leq t \leq 10^4) — the number of test cases. Then tt test cases follow.

Each test case is preceded by an empty line.

The first line of each test case contains two integers nn (2≤n≤2⋅1052 \leq n \leq 2 \cdot 10^5) and mm (1≤m≤2⋅1051 \leq m \leq 2 \cdot 10^5) — number of cities and roads, respectively.

This is followed by mm lines describing the roads. Each road is characterized by two integers uu and vv (1≤u,v≤n,u≠v1 \leq u, v \leq n, u \ne v) — the numbers of cities connected by a one-way road.

It is guaranteed that the sums of nn and mm over all test cases do not exceed 2⋅1052 \cdot 10^5.

It is guaranteed that for each pair of different cities (u,v)(u, v) there is at most one road from uu to vv (but a pair of roads from uu to vv and from vv to uu — is valid).

It is guaranteed that there is a path from the capital to all cities.

第一行包含一个整数 tt(1≤t≤1041 \leq t \leq 10^4)—— 测试用例的数量。随后是 tt 个测试用例。

每个测试用例之前有一空行。

每个测试用例的第一行包含两个整数 nn(2≤n≤2⋅1052 \leq n \leq 2 \cdot 10^5)和 mm(1≤m≤2⋅1051 \leq m \leq 2 \cdot 10^5)—— 分别表示城市的数量和道路的数量。

接下来是 mm 行,每行描述一条道路。每条道路由两个整数 uu 和 vv(1≤u,v≤n, u≠v1 \leq u, v \leq n,\ u \ne v)表征,表示存在一条从城市 uu 到城市 vv 的单向道路。

保证所有测试用例的 nn 之和与 mm 之和均不超过 2⋅1052 \cdot 10^5。

保证对于任意两个不同的城市 (u,v)(u, v),至多存在一条从 uu 到 vv 的道路(但同时存在从 uu 到 vv 和从 vv 到 uu 的两条道路是允许的)。

保证从首都到所有城市均存在路径。

输出格式

For each test case, on a separate line output nn numbers, the ii-th of which is equal to the minimum possible distance from the capital to the city where Polycarp ended his journey.

对于每个测试用例,在单独一行中输出 nn 个数字,其中第 ii 个数字等于 Polycarp 旅程结束时所在城市到首都的最小可能距离。

输入输出样例

  • 输入#1

    3
    
    6 7
    1 2
    1 3
    2 5
    2 4
    5 1
    3 6
    6 2
    
    2 2
    1 2
    2 1
    
    6 8
    1 2
    1 5
    2 6
    6 1
    2 3
    3 4
    4 2
    5 4

    输出#1

    0 0 1 2 0 1 
    0 0 
    0 0 2 1 1 0

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

首页