Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N, M := sc.nextInt(), sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < M; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	group := make([]int, N)
	for i := 0; i < N; i++ {
		group[i] = -1
	}
	var dfs func(cur int)
	dfs = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if group[next] == -1 {
				group[next] = group[cur]
				dfs(next)
			}
		}
	}
	num := 0
	for i := 0; i < N; i++ {
		if group[i] == -1 {
			group[i] = num
			dfs(i)
			num++
		}
	}

	Q := sc.nextInt()
	for i := 0; i < Q; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		if group[s] == group[t] {
			fmt.Fprintln(wtr, "yes")
		} else {
			fmt.Fprintln(wtr, "no")
		}
	}
	wtr.Flush()
}

C++: #include<iostream>
#include<iomanip>
#include<queue>
#include<string>
#include<stack>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cassert>
#include<ctime>
#include<algorithm>
#include<utility>
#include<map>
#include<set>
#include<vector>
#include<functional>

using namespace std;

#ifdef DEBUG
#define dprintf      printf
#define dout(x)      cout<<#x" = "<<(x)<<endl
#define darray(x,n)    {int i;rep(i,n)cout<<#x"["<<i<<"] = "<<*((x)+i)<<endl;}
#define dloop(i,s,g) for((i)=(s);(i)<(g);(i)++)
#define drep(i,n)    for((i)=0;(i)<(n);(i)++)
#define dstop()      scanf("%*c")
#else
#define dprintf      (1)?0:printf
#define dout(x)
#define darray(x,n)
#define dloop(i,s,g) if(1){}else
#define drep(i,n)    if(1){}else
#define dstop()      if(1){}else
#endif

#define F    first
#define S    second
#define pb   push_back
#define mp   make_pair

#define loop(i,s,g) for((i)=(s);(i)<(g);(i)++)
#define rep(i,n)    for((i)=0;(i)<(n);(i)++)
#define all(x)      (x.begin(),x.end())
#define in(T,...) T __VA_ARGS__; impl(__VA_ARGS__);
#define array(T,id,n) T id[n]; rep(i,n)cin>>id[i];

//#define int long long;
#ifndef int
#define INF  (0x7fffffff)
#else
#define LINF (0x7fffffffffffffff)
#endif

typedef  long long           ll;
typedef  unsigned            ui;
typedef  unsigned long long  ull;
typedef  pair<int,int>       i_i;
typedef  pair<ll,int>        ll_i;
typedef  pair<ll,ll>         ll_ll;
typedef  pair<double,int>    d_i;
typedef  pair<double,double> d_d;

void impl(){};
template <typename T,typename... TS >
void impl(T &head,TS &... tail)
{
  cin>>head;
  impl(tail ...);
}

int e[112345][112345]={};
int sumi[112345]={};
int n,m;

int dfs(int v,int g)
{
  int i;
  int ret=0;
  dout(v);
  if(v==g)
    return 1;
  rep(i,n)
    if(e[v][i]==1 && sumi[i]==0)
      {
	sumi[i]=1;
	ret=dfs(i,g);
	if(ret==1)
	  break;
      }
  return ret;
}

signed main(void)
{
  int i;
  int a,b;
  int q;
  int s,t;
  cin>>n>>m;
  rep(i,m)
    {
      cin>>a>>b;
      e[a][b]=1;
      e[b][a]=1;
    }
  cin>>q;
  rep(i,q)
    {
      cin>>s>>t;
      if(dfs(s,t))
	puts("yes");
      else
	puts("no");
    }  
  
  return 0;
}

C++: /**
 */

/*
 * test case 1
 2 1 1 2 1 1 2  
 test case 2
 4 4 1 2 1 3 2 4 3 4 1 1 2   
 */


#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector< vector<int> > rel;
vector<bool> visited;
int n, m, q;
int dest = 0;


bool fre1(int j) 
{
	for (int i=0;i<rel[j].size();i++)
	{
		int kk = rel[j][i];
		if (kk==dest) return true;
		if (visited[kk])continue;
		visited[kk]=true;
		if(fre1(kk))return true;
	}
	return false;
}

/**
 * preserve a priority que of nodes with the most connections. 
 * if there is a node with only 2 connections go look for the other one.
 */

priority_queue< pair<int, int> > pq;


bool fre3(int start) 
{
	for (int i=0;i<rel[start].size();i++)
	{
		int cand = rel[start][i];
		if (cand==dest) return true;
		if (visited[cand])continue;
		visited[cand]=true;
		int point = rel[cand].size();
		if (point == 1) continue;
		if (point == 2) {
			if (fre3(cand)) return true;
		} else {
			pq.push(pair<int, int>(point, cand));
		}
	}
	return false;
}


bool fre2(int start) 
{
	pq.push(pair<int,int>(0, start));
	while (pq.size())
	{
		pair<int, int> p = pq.top();
		pq.pop();
		start = p.second;
		if (fre3(start)) return true;
	}
	return false;
}


int main()
{
	cin >> n;
	rel.resize(n);
	cin >> m;
	for (int i=1;i<=m;i++){
		int j, k;
		cin >> j >> k;
		rel[j].push_back(k);
		rel[k].push_back(j);
	}

	cin >> q;
	for (int i=1;i<=q;i++){
		int start;
		cin >> start >> dest;
		visited.clear();
		visited.resize(n, false);
		cout << (fre2(start) ? "yes" : "no") << endl;
		while (pq.size()) pq.pop(); // forgot this.
	}

	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <queue>

#define MAX 100000


using namespace std;


int main(void)
{
	int n;
	int m;
	int s;
	int t;
	int q;
	int type = 1;

	int gcol[MAX];

	cin >> n >> m;

	for (int i = 0; i < m; i++) {
		cin >> s >> t;

		if (gcol[s] != 0) {
			gcol[t] = gcol[s];
		}
		else if (gcol[t] != 0) {
			gcol[s] = gcol[t];
		}
		else {
			gcol[s] = gcol[t] = type;
			type++;
		}
	}

	cin >> q;

	for (int i = 0; i < q; i++) {
		cin >> s >> t;

		if (gcol[s] == gcol[t] && gcol[s] != 0) {
			cout << "yes";
		}
		else {
			cout << "no";
		}

		cout << endl;

	}

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;

static const int MAX = 100000;
static const int NIL = -1;

int n;
vector<int>G[MAX];
int color[MAX];

void dfs(int r, int c){
  stack<int>S;
  S.push(r);
  color[r] = c;
  while ( !S.empty() ) {
    int u = S.top();
    S.pop();
    for ( int i = 0; i < G[u].size() ; ++i) {
      int v = G[u][i];
      if ( color[v] == NIL ) {
	color[v] = c;
	S.push(v);
      }
    }
  }
}

int main(){
  int s, t, m, q;
  cin >> n >> m;
  
  for ( int i = 0; i < m ; ++i) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  int id = 1;
  
  for ( int i = 0; i < n; ++i) {
    color[i] = NIL; //preset
  }
  
  for ( int u = 0; u < n; ++u) {
    if ( color[u] == NIL ) {
      dfs(u,id);
      id++;
    }
  }
  
  scanf("%d", &q);
  for ( int i = 0; i < q; ++i) {
    scanf("%d %d", &s, &t);
    if ( color[s] == color[t] ) {
      printf("yes\n");
    } else {
      printf("no\n");
    }
  }

  return (0);
}