Codeforces 34 D. Road Map

本文介绍了一道关于树形结构的数据结构问题,任务是在给定的城市道路图中,将首都从一个城市移动到另一个城市,并重新计算除新首都外所有城市的父节点。通过反转新旧首都间的路径来实现更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://codeforces.com/contest/34/problem/D

There are n cities in Berland. Each city has its index — an integer number from 1 to n. The capital has index r1. 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 pi — 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 r1 to city r2. 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.

Input

The first line contains three space-separated integers nr1r2 (2 ≤ n ≤ 5·104, 1 ≤ r1 ≠ r2 ≤ 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 r1, there is given integer pi — index of the last city on the way from the capital to city i. All the cities are described in order of increasing indexes.

Output

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

Examples
input
Copy
3 2 3
2 2
output
Copy
2 3 
input
Copy
6 2 4
6 1 2 4 2
output
Copy
6 4 1 4 2 

题目大意:

一个城市要改变其首都,该城市首都保证每个城市到首都都只有一条路,即一个树结构,首都是根节点,其余每个节点只有一个父节点。给定n个点,原首都和新首都,接下来一行给定除了首都的所有节点的父节点,要求输出改变新首都后的除了首都的所有节点的父节点(不添加任何新路径)。

题目思路:

画图看一下就可知,把新首都到原首都的该条路径反过来就好,其余路径不变。

代码:


#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const double DINF = 0xffffffffffff;
const int mod = 1e9+7;
const int N = 1e5+5;

int a[N];
int b[N];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    int n,r1,r2,x;
    cin>>n>>r1>>r2;
    for(int i=1;i<=n;i++)
    {
        if(i==r1)
        {
            a[r1]=r1;
            b[r1]=r1;
        }
        else
        {
            cin>>x;
            a[i]=x;
            b[i]=x;
        }
    }
    int now=r2;
    while(1)
    {
        if(b[now]==r1)
        {
            a[r1]=now;
            break;
        }
        else
        {
            a[b[now]]=now;
            now=b[now];
        }
    }
    bool flag=true;
    for(int i=1;i<=n;i++)
    {
        if(i!=r2)
        {
            if(flag)
                flag=false;
            else
                cout<<" ";
            cout<<a[i];
        }
    }
    cout<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值