Pagini recente » Cod sursa (job #1528383) | Cod sursa (job #2220945) | Cod sursa (job #2303835) | Cod sursa (job #630967) | Cod sursa (job #129403)
Cod sursa(job #129403)
#include <stdio.h>
FILE*f=fopen("stramosi.in.cpp","r");
FILE*g=fopen("stramosi.out","w");
#define nmax 300
struct pelem{long info; pelem *next;};
long n,m,i,v,ct,vl;
short marc[nmax];
pelem *nod[nmax],*list[nmax],*a[nmax];
long s[nmax],father[nmax],dist[nmax],idist[nmax];
long stramos[nmax],varf[nmax];
void qin(pelem *&first, long vl)
{
pelem *p;
p=new pelem;
p->info=vl;
p->next=first;
first=p;
}
void qout(pelem *&first, long &vl)
{
pelem *p;
vl=first->info;
p=first;
first=first->next;
delete(p);
}
void df(long nodulet)
{
long rez,vl,valo;
s[nodulet]=1;
while (list[nodulet]!=NULL)
{
qout(list[nodulet],vl);
if (s[vl]==0)
{
s[vl]=1;
dist[vl]=dist[nodulet]+1;
idist[dist[vl]]=vl;
if (marc[vl]==1)
while (nod[vl]!=NULL)
{
qout(nod[vl],valo);
rez=dist[vl]-valo;
if (rez>0) qin(a[vl],idist[rez]);
else qin(a[vl],0);
}
df(vl);
}
}
}
int main()
{
fscanf(f,"%ld%ld",&n,&m);
ct=0;
for (i=1; i<=n; i++)
{
fscanf(f,"%ld",&v);
if (v==0) father[++ct]=i;
else
{
qin(list[i],v);
qin(list[v],i);
}
}
for (i=1; i<=m; i++)
{
fscanf(f,"%ld%ld",&varf[i],&stramos[i]);
marc[varf[i]]=1;
qin(nod[varf[i]],stramos[i]);
}
for (i=1; i<=ct; i++)
{
dist[father[i]]=1;
while (nod[father[i]]!=NULL)
{
qout(nod[father[i]],vl);
qin(a[father[i]],0);
}
idist[1]=father[i];
df(father[i]);
}
for (i=1; i<=m; i++)
if (a[varf[i]]!=NULL)
{
qout(a[varf[i]],vl);
fprintf(g,"%ld\n\",vl);
}
return 0;
}