Pagini recente »
Rating Alexandru Morus (alexandru.morus)
|
Atașamentele paginii Clasament 2020-11-19-clasa-5-tema-9
|
Monitorul de evaluare
|
Rating teodor (legotechnic)
|
Cod sursă (job #238732)
Cod sursă (job
#238732)
#include <fstream>
using namespace std;
ifstream fin("pointeri.in");
ofstream fout ("pointeri.out");
#define MAX 200001
struct pointer
{
int st, dr;
}a[MAX];
int n, rad, x[MAX], i;
void rez(int ind)
{
if (a[ind].st == -1 && a[ind].dr == -1)
{
x[++i]=ind;
return;
}
else if (a[ind].st == -1)
{
x[++i]=ind;
rez(a[ind].dr);
}
else if (a[ind].dr == -1)
{
rez(a[ind].st);
x[++i]=ind;
}
else
{
rez(a[ind].st);
x[++i]=ind;
rez(a[ind].dr);
}
}
int main()
{
//citire
fin >> n >> rad;
for (int j = 0; j<n; j++) fin >> a[j].st;
for (int j = 0; j<n; j++) fin >> a[j].dr;
rez(rad);
fout << x[1] << '\n';
x[0] = x[n+1] = -1;
for (int j = 1; j<=n ; j++)
{
a[x[j]].st = x[j-1];
a[x[j]].dr = x[j+1];
}
for (int j = 0; j<n ; j++) fout << a[j].st << ' ';
fout << '\n';
for (int j = 0; j<n; j++) fout << a[j].dr << ' ';
fout << '\n';
return 0;
}