Cod sursa(job #1273534)

Utilizator Corina1997Todoran Ana-Corina Corina1997 Data 22 noiembrie 2014 17:00:10
Problema Stramosi Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream is("stramosi.in");
ofstream os("stramosi.out");

void Push( int i, int j );

int n, m, aux, x, a ,b;
vector<vector<int> > G;

int main()
{
    is >> n >> m;
    G.resize(n+1);
    for ( int i = 1; i <= n; i++ )
    {
        is >> x;
        G[i].push_back(0);
        G[i].push_back(x);
    }

    for ( int i = 1; i <= n; i++ )
    {
        if ( G[i][1] != 0 )
            Push(i, G[i][1] );
    }

    for ( int i = 1; i <= m; i++ )
    {
        is >> a >> b;
        if ( b <= G[a][0] )
            os << G[a][b] << '\n';
        else
            os << "0\n";
    }
    is.close();
    os.close();
    return 0;
}

void Push( int i, int j )
{
    aux = 0;
    while ( j != 0 )
    {
        aux = G[j][1];
        G[i].push_back(aux);
        j = aux;
        G[i][0]++;
    }

}