int main()
{
    int kase,F,T,d[10009],u,i;
    sieve(10002);
    scanf("%d",&kase);
    while(kase--)
    {
      scanf("%d %d",&F,&T);
      bool vis[10009]={0};
      q.push(F);
      d[F]=0;
      vis[F]=1;
      while(!q.empty())
      {
        u=q.front();
        q.pop();
        for(i=0;i<c;i++)
        {
         if(!vis[a[i]])
          if(check(u,a[i]))
          d[a[i]]=d[u]+1,q.push(a[i]),vis[a[i]]=1;
        }
      }
      if(vis[T])
       printf("%d\n",d[T]);
      else
       printf("Impossible\n");
    }
    return 0;
}
Exemplo n.º 2
0
bool solve(qi &l)
{
   int t;
   si r;

   int i=1;
   bool found=false;
   while(!l.empty())
   {
      t=l.front();
      if(t==i) { found=true; break; }
      r.push(t);
      l.pop();
   }
   if(!found) return false;
   l.pop();
   i+=1;

   while(!l.empty() && !r.empty())
   {
      if(l.front()==i)
	 l.pop();
      else if(r.top()==i)
	 r.pop();
      else
      {  
         found=false;
	 while(!l.empty())
	 {
	    t=l.front();
	    if(t==i) { found=true; break; }
	    r.push(t);
	    l.pop();
	 }
         if(!found) return false;
	 l.pop();
      }
      i+=1;
   }
   return true;
}
void bfs(long start)
{
	long i,next,u;
	d[start]=0;
	vis[start]=1;
	q.push(start);
	while(!q.empty())
	{
		u=q.front();
		q.pop();
		for(i=0;i<gr[u].size();i++)
		{
			next=gr[u][i];
			if(!vis[next])
			{
				q.push(next);
				vis[next]=1;
				d[next]=d[u]+1;
			}
		}
	}
}
long bfs(long start)
{
    long i,u,next,count=0;
    vis[start]=true;
    q.push(start);
    while(!q.empty())
    {
		count++;
		u=q.front();
        q.pop();
        for(i=0; i<gr[u].size(); i++)
        {
            next=gr[u][i];
            if(vis[next]==false)
            {
				
                q.push(next);
                vis[next]=true;
            }    
        }
    }
	
    return count;
}