Cod sursa(job #834664)

Utilizator edmond_ciorbaCiorba Edmond edmond_ciorba Data 14 decembrie 2012 21:52:27
Problema Stramosi Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
    string be,ki;
    be="stramosi.in";
    ki="stramosi.out";
    ifstream f;
    ofstream g;
    f.open(be.c_str());
    g.open(ki.c_str());

    int n,m,i,a,b,p,db;
    vector<int> os;
    f >> n >> m;
    os.push_back(0);
    for(i=1; i<=n; ++i)
    {
        f >> a;
        os.push_back(a);
    }
    for(i=1; i<=m; ++i)
    {
        f >> a >> b;
        db=0;
        p=a;
        while( (db!=b) )
        {
            p=os[a];
            a=p;
            if(p==0)
            {
                db=b;
            }
            else
            {
                db++;
            }
        }
        g << p << endl;
    }

    f.close();
    g.close();
    return 0;
}