Pagini recente » Cod sursa (job #2702644) | Cod sursa (job #1293096) | Cod sursa (job #1291636) | Statistici Costea Lucian (Lucianx3) | Cod sursa (job #778569)
Cod sursa(job #778569)
// stramosi.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int *v, **x;
int **stramosi;
void read_input(int &n, int &m) {
fstream in_file;
in_file.open("stramosi.in");
in_file >> n >> m;
v = new int[n];
x = new int*[m];
for(int i = 0; i < n; i++)
in_file >> v[i];
for(int i = 0; i < m; i++) {
x[i] = new int[2];
in_file >> x[i][0] >> x[i][1];
}
in_file.close();
}
int depth(int n, int i) {
int pos, j;
pos = i;
j = 0;
while (v[pos]) {
j++;
pos = v[pos] - 1;
}
return j + 1;
}
int get_val(int start, int depth) {
int pos = start - 1;
while (v[pos] && depth--)
pos = v[pos] - 1;
return v[pos];
}
void calculate(int n) {
stramosi = new int*[n];
for(int i = 0; i < n; i++) {
int d = depth(n, i);
stramosi[i] = new int[d];
int j = 0, pos = i;
while (v[pos]) {
stramosi[i][j++] = v[pos];
pos = v[pos] - 1;
}
stramosi[i][j] = 0;
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < depth(n, i); j++)
cout << stramosi[i][j] << " ";
cout << endl;
}
getchar();
}
void write_solution(int m) {
ofstream out_file;
out_file.open("stramosi.out");
for(int i = 0; i < m; i++)
out_file << get_val(x[i][0], x[i][1] - 1) << endl; //stramosi[x[i][0] - 1][x[i][1] - 1] << endl;
out_file.close();
}
int main()
{
int n, m;
read_input(n, m);
//calculate(n);
write_solution(m);
//cout << n << " " << m << endl;
//for(int i = 0; i < n; i++)
// cout << v[i] << " ";
//cout << endl;
//for(int i = 0; i < m; i++)
// cout << x[i][0] << " " << x[i][1] << endl;
//getchar();
return 0;
}