Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Connected Components

Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network.

Input

In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$.

In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other).

In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character.

Output

For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise.

Constraints

$2 \leq n \leq 100,000$

$0 \leq m \leq 100,000$

$1 \leq q \leq 10,000$

Sample Input

10 9
0 1
0 2
3 4
5 7
5 6
6 7
6 8
7 8
8 9
3
0 1
5 9
1 3

Sample Output

yes
yes
no

Solution in C (judge verdict: Time Limit Exceeded)

#include<stdio.h>
#include<stdlib.h>
typedef struct Graph
{
  int numOfAdj;
  int *adj;
  int status;//0:not discovered, 1:discovered, 2:finished
} graph;
int n;//num of vertice
graph *p;//array of vertice
int *d;//array of degree
int *queue;
int head=0,tail=0;
void setAdj(int,int);
int breadthFirstSearch(int,int);
void enqueue(int);
int dequeue(void);
void queueReset(void);
void printGraph(void);//debugging
int main(void)
{
  int i,j;//counter
  int num;//num of relations / questions
  int start,end;
  int key,numOfAdj,adj;
  scanf("%d",&n);
  p=(graph*)malloc(sizeof(graph)*n);
  d=(int*)malloc(sizeof(int)*n);
  queue=(int*)malloc(sizeof(int)*n);
  for(i=0;i<n;i++) p[i].numOfAdj=0;
  scanf("%d",&num);
  for(i=0;i<num;i++)
    {
      scanf("%d%d",&start,&end);
      setAdj(start,end);
      setAdj(end,start);
    }
  //printGraph();
  scanf("%d",&num);
  for(i=0;i<num;i++)
    {
      scanf("%d%d",&start,&end);
      if(breadthFirstSearch(start,end)!=-1) printf("yes\n");
      else printf("no\n");
    }
  return 0;
}
void setAdj(int start,int end)
{
  int i;//counter
  int *temp=p[start].adj;
  p[start].numOfAdj++;
  p[start].adj=(int*)malloc(sizeof(int)*p[start].numOfAdj);
  for(i=0;i<p[start].numOfAdj-1;i++) p[start].adj[i]=temp[i];
  p[start].adj[i]=end;
}
int breadthFirstSearch(int start,int end)
{
  int i;//counter
  int u,v;
  //printf("[%d->%d]\n",start,end);
  for(i=0;i<n;i++)
    {
      p[i].status=0;
      d[i]=-1;
    }
  queueReset();
  d[start]=0;
  enqueue(start);
  while(head!=tail)
    {
      u=dequeue();
      for(i=0;i<p[u].numOfAdj;i++)
        {
          v=p[u].adj[i];
          if(p[v].status==0)
            {
              //printf(" %d->%d\n",u,v);
              p[v].status=1;
              d[v]=d[u]+1;
              enqueue(v);
            }
        }
      p[u].status=2;
    }
  return d[end];
}
void enqueue(int key)
{
  queue[tail]=key;
  tail++;
}
int dequeue(void)
{
  head++;
  return queue[head-1];
}
void queueReset(void)
{
  head=0;
  tail=0;
}
void printGraph(void)
{
  int i,j;//counter
  for(i=0;i<n;i++)
    {
      printf("[%d]",i);
      for(j=0;j<p[i].numOfAdj;j++) printf(", %d",p[i].adj[j]);
    }
}