Codeforces 34 D. Road Map

题目链接: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;
}

猜你喜欢

转载自blog.csdn.net/baodream/article/details/80376462