Cod sursa(job #778570)

Utilizator usainandrei popescu usain Data 15 august 2012 00:32:07
Problema Stramosi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.78 kb
// 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;

int get_val(int start, int depth) {
	int pos = start - 1;

	while (v[pos] && depth--)
		pos = v[pos] - 1;

	return v[pos];
}

void read_input(int &n, int &m) {
	fstream in_file;
	ofstream out_file;
	int a, b;

	in_file.open("stramosi.in");
	in_file >> n >> m;
	v = new int[n];

	for(int i = 0; i < n; i++)
		in_file >> v[i];

	
	out_file.open("stramosi.out");
	for(int i = 0; i < m; i++) {
		in_file >> a >> b;
		out_file << get_val(a, b - 1) << endl;
	}

	in_file.close();
	out_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;
}

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;
}