Cod sursa(job #699032)

Utilizator dsfm_danielaasd mghd dsfm_daniel Data 29 februarie 2012 17:14:08
Problema Arbore Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <stdio.h>
#include <iostream>
using namespace std;

#define dim 100000

int v[dim];
int s[dim];



int main (void)
{
	int n,m,x,y,i,j,ok,type;
	FILE *f1,*f2;
	f1=fopen("arbore.in","r");
	f2=fopen("arbore.out","w");
	fscanf(f1,"%d %d\n",&n,&m);
	for (i=1;i<n;i++)
	{
		fscanf(f1,"%d %d\n",&x,&y);
		v[y]=x;
		s[i]=0;
	}
	s[n]=0;
	for (j=1;j<=m;j++)
	{
		fscanf(f1,"%d ",&type);
		if (type==1)
		{
			fscanf(f1,"%d %d\n",&x,&y);
			s[x]=s[x]+y;
			for (i=1;i<=n;i++)
				if (v[i]==x)
					s[i]=s[i]+y;
		}
		else if (type==2)
		{
			fscanf(f1,"%d\n",&x);
			ok=0;
			for (i=1;i<=n;i++)
				if (s[i]==x)
				{
					fprintf(f2,"%d\n",i);
					ok=1;
					i=n;
				}
			if (ok==0)
				fprintf(f2,"-1\n");
		}
	}
	fclose(f1);
	fclose(f2);
	return 0;
}