Cod sursa(job #1643741)

Utilizator avsi_bvAvram Simona avsi_bv Data 9 martie 2016 20:02:56
Problema Stramosi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <iostream>
#include <fstream>
using namespace std;
int n,m,v[100];
int stram(int x,int y)
{
    int a,ok;
    ok=1;
    if(v[x]!=0)
    {
        a=v[x];
        y=y-1;
        while(y>0&&ok==1)
        {
            if(v[a]==0)ok=0;
            else
            {
                a=v[a];
                y=y-1;
            }
        }
        if(ok==0)return 0;
        else return a;
    }
    else return 0;
}
void caf()
{
    int i,x,y;
    ifstream f("stramosi.in");
    ofstream g("stramosi.out");
    f>>n>>m;
    for(i=1;i<=n;i++)f>>v[i];
    for(i=1;i<=m;i++)
    {
        f>>x>>y;
        g<<stram(x,y)<<endl;
    }
    f.close();
    g.close();
}
int main()
{
    caf();
    return 0;
}