Cod sursa(job #190603)

Utilizator AndreyPAndrei Poenaru AndreyP Data 23 mai 2008 17:04:21
Problema Stramosi Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include<stdio.h>
int n,m,v[250010],p,q;
char c[1750010],k[100];
void citire()
{
	fgets(c,1750010,stdin);
	int i,aux=0,n1=0;
	for(i=0; c[i]!='\0'; i++)
	{
		if(c[i]>='0')
			aux=aux*10+c[i]-'0';
		else
		{
			v[++n1]=aux;
			aux=0;
		}
	}
}
void citeste()
{
	fgets(c,100,stdin);
	p=q=0;
	int i,j;
	for(i=0; c[i]>='0'; i++)
		q=q*10+c[i]-'0';
	for(j=i+1; c[j]>='0'; j++)
		p=p*10+c[j]-'0';
}
void scrie()
{
	if(q==0)
	{
		printf("0\n");
		return;
	}
	int y=0,i;
	while(q)
	{
		k[++y]=q%10+'0';
		q/=10;
	}
	for(i=y; i; i--)
		printf("%c",k[i]);
	printf("\n");
}
void dfs()
{
	if((!p)||(!q))
	{
		//printf("%d\n",q);
		scrie();
		return;
	}
	q=v[q];
	p--;
	dfs();
}
int main()
{
	freopen("stramosi.in","r",stdin);
	freopen("stramosi.out","w",stdout);
	scanf("%d%d\n",&n,&m);
	citire();
	int i;
	for(i=0; i<m; i++)
	{
		citeste();
		dfs();
	}
	return 0;
}