Cod sursa(job #542540)

Utilizator blastoiseZ.Z.Daniel blastoise Data 26 februarie 2011 14:38:57
Problema Gossips Scor 0
Compilator cpp Status done
Runda Romanian Master in Mathematics and Sciences 2011, Ziua 2 Marime 0.8 kb
#include <stdio.h>
#include <string.h>
#include <set>

using namespace std;

set <int> v[100100];

int x,y,z,N,M,Q,i,s,ok;
int T[100100];

set <int>::iterator it;

int main()
{
	freopen("gossips.in","r",stdin);
	freopen("gossips.out","w",stdout);

	scanf("%d%d%d",&N,&M,&Q);

	memset(T,0,sizeof(T));

	for(i=1;i<=M;i++)
	{
		scanf("%d%d",&x,&y);
		T[y]=x;
	}

	for(i=1;i<=Q;i++)
	{
		scanf("%d%d%d",&s,&x,&y);

		if(s==2)
		{
			while(x!=0)
			{
				z=y;
				while(z!=0)
				{
					v[x].insert(z);
					z=T[z];
				}
				x=T[x];
			}
		}
		else
		{
			ok=0;
			while(x!=0)
			{
				it=v[x].find(y);
				if(it!=v[x].end())
				{
					ok=1;
					break;
				}

				x=T[x];
			}

			if(ok==0) printf("NO\n");
			else printf("YES\n");
		}
	}

	return 0;
}