CF34D.Road Map

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There are n cities in Berland. Each city has its index — an integer number from 1 to n. The capital has index _r_1. All the roads in Berland are two-way. The road system is such that there is exactly one path from the capital to each city, i.e. the road map looks like a tree. In Berland's chronicles the road map is kept in the following way: for each city i, different from the capital, there is kept number p__i — index of the last city on the way from the capital to i.

Once the king of Berland Berl XXXIV decided to move the capital from city _r_1 to city _r_2. Naturally, after this the old representation of the road map in Berland's chronicles became incorrect. Please, help the king find out a new representation of the road map in the way described above.

贝兰国有 nn 座城市。每座城市都有一个编号——一个从 11 到 nn 的整数。首都的编号为 r1r_1。贝兰国的所有道路均为双向道路。道路系统满足:从首都到任意一座城市的路径恰好有一条,即道路地图构成一棵树。在贝兰国的编年史中,道路地图以如下方式保存:对每个不同于首都的城市 ii,记录一个数 pip_i —— 即从首都到城市 ii 的唯一路径上,ii 的前一个城市(即路径上 ii 的父节点)的编号。

某日,贝兰国国王贝尔 XXXIV 决定将首都从城市 r1r_1 迁至城市 r2r_2。显然,迁都后,编年史中原来保存的道路地图表示方式便不再正确。请帮助国王求出迁都后、按上述方式表示的新道路地图。

输入格式

The first line contains three space-separated integers n, _r_1, _r_2 (2 ≤ n ≤ 5·104, 1 ≤ _r_1 ≠ _r_2 ≤ n) — amount of cities in Berland, index of the old capital and index of the new one, correspondingly.

The following line contains n - 1 space-separated integers — the old representation of the road map. For each city, apart from _r_1, there is given integer p__i — index of the last city on the way from the capital to city i. All the cities are described in order of increasing indexes.

第一行包含三个以空格分隔的整数 nn、r1r_1、r2r_2(2 ≤ n ≤ 5⋅1042 \leq n \leq 5\cdot10^4,1 ≤ r1 ≠ r2 ≤ n1 \leq r_1 \ne r_2 \leq n),分别表示贝尔兰的城市数量、旧首都的编号以及新首都的编号。

接下来一行包含 n−1n-1 个以空格分隔的整数——即道路图的旧表示法。对于除 r1r_1 外的每个城市 ii,给出一个整数 pip_i,表示从首都到城市 ii 的路径上 ii 的前一个城市(即父节点)的编号。所有城市按编号升序依次描述。

输出格式

Output n - 1 numbers — new representation of the road map in the same format.

输出 n−1n-1 个数——以相同格式表示的道路地图的新表示。

输入输出样例

  • 输入#1

    3 2 3
    2 2

    输出#1

    2 3
  • 输入#2

    6 2 4
    6 1 2 4 2

    输出#2

    6 4 1 4 2

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

首页