Cod sursa(job #778558)

Utilizator usainandrei popescu usain Data 14 august 2012 23:48:22
Problema Stramosi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 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;

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

void calculate(int n) {
	stramosi = new int*[n];
	for(int i = 0; i < n; i++) {
		stramosi[i] = new int[n];
		int j = 0, pos = i;
		while (v[pos]) {
			stramosi[i][j++] = v[pos];
			pos = v[pos] - 1;
		}
		while (j < n)
			stramosi[i][j++] = 0;
	}

	//for(int i = 0; i < n; i++) {
	//	for(int j = 0; j < n; j++)
	//		cout << stramosi[i][j] << " ";
	//	cout << endl;
	//}
}

void write_solution(int m) {
	ofstream out_file;
	out_file.open("stramosi.out");

	for(int i = 0; i < m; i++)
		out_file << 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;
}