Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

C++: #include <stdio.h>
#include <iostream>
#include <utility>
#include <algorithm>
#include <math.h>
#include <vector>
#include <list>
#define N 100000
using namespace std;
int n;
list<int> adj[N];
bool f[N] = {};
int g[N] = {};


void setGraphNum(int v, int i)
{
    g[v] = i;
    f[v] = true;
    list<int>::iterator itr = adj[v].begin();
    while (itr != adj[v].end())
    {
        if (!f[*itr])
            setGraphNum(*itr, i);
        itr++;
    }
}
void CalcGraphNum()
{
    int k = 1;
    for (int i = 0; i < n; i++)
    {
        if (g[i] == 0)
            setGraphNum(i, k++);
    }
}

int main()
{
    int m, u, v, q;
    cin >> n >> m;

    for (int i = 0; i < m; i++)
    {
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    CalcGraphNum();
    cin >> q;
    for (int i = 0; i < q; i++)
    {
        cin >> u >> v;
        if (g[u] == g[v])
            printf("yes\n");
        else
            printf("no\n");
    }

    return 0;
}

C++: #include <iostream>
#include <vector>

using namespace std;

bool* visit;

bool dfs(int s, int t, vector<vector<int>> sns) {
  if (s == t) {
    return true;
  }
  if (visit[s]) {
    return false;
  }

  visit[s] = true;

  int len = sns[s].size();
  for (int i = 0; i < len; i++) {
    int next = sns[s][i];
    if (dfs(next, t, sns)) {
      return true;
    }
  }
  return false;
}

int main() {
  int n,m,q,s,t;
  cin >> n >> m;

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

    sns[s].push_back(t);
    sns[t].push_back(s);
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    visit = new bool[n];
    if (dfs(s, t, sns)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}

C++: #include <stdio.h>

int m[100000] = {0};

void init() {
	int i;
	for ( i = 0; i < 100000; ++i) {
		m[i] = i;
	}
}

int find(int n) {
	if ( n == m[n]) {
		return n;
	}
	return m[n] = find(m[n]);
}

void friend (int x, int y) {
	if ( find(x) == find(y)) {
		return 1;
	} else {
		return 0;
	}
}

void unit (int x, int y) {
	x = find(x);
	y = find(y);
	
	if ( x == y ) {
		return ;
	}
	m[y] = x;
}

int main() {
	int n, s, t, u, v, i;
	init();
	scanf("%d", &n);
	scanf("%d", &s);
	
	for ( i = 0; i < s; ++i) {
		scanf("%d", &u);
		scanf("%d", &v);
		unit( u, v);
	}
	
	scanf("%d", &t);
	
	for ( i = 0; i < s; i++) {
		scanf("%d", &u);
		scanf("%d", &v);
		if ( friend( u, v) == 1) {
			printf("yes\n");
		} else {
			printf("no\n");
		}
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
#define MAX_N 100000
int g[MAX_N][MAX_N];
int n;

bool can_connect(int s,int t) {
  bool visited[n];
  queue<int> que;
  visited[s] = true;
  que.push(s);
  while(!que.empty()) {
    int v = que.front(); que.pop();
    for(int i=0;i<n;i++) {
      if(g[v][i] != 0 && !visited[i]) {
        if(i == t) return true;
        visited[i] = true;
        que.push(i);
      }
    }
  }
  return false;
}

int main() {
  int m,s,t,q;
  scanf("%d %d\n",&n,&m);
  for(int i=0;i<m;i++) {
    scanf("%d %d",&s,&t);
    g[s][t] = 1;
    g[t][s] = 1;
  }
  scanf("%d",&q);
  for(int i=0;i<q;i++) {
    scanf("%d %d",&s,&t);
    if(can_connect(s,t)){
      printf("yes\n");
    }
    else {
      printf("no\n");
    }
  }
}

C++: #include <iostream>
#include <vector>
#define eol '\n'
using namespace std;

vector<int> visit;

bool bfs(int s, int t, vector< vector<int> > list) {
  if (s == t) {
    return true;
  }
  
  // visit flag
  visit[s] = 1;
  
  for (int i = 0; i < list[s].size(); i++) {
    int next = list[s][i];
    if (visit[next] == 0) {
      if (bfs(next, t, list)) {
        return true;
      }
    }
  }
  return false;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n,m,s,t,q;
  cin >> n >> m;

  visit.resize(n,0);
  vector< vector<int> > list(n, vector<int>());
  
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    list[s].push_back(t);
    list[t].push_back(s);
  }

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

    if (bfs(s,t,list)) {
      cout << "yes" << eol;
    } else {
      cout << "no" << eol;
    }
  }
  return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <queue>
using namespace std;

#define NODE_NUM 100000

queue<int> que;

class Node
{
private:
	int next_num;
	int next_node_list[NODE_NUM];
public:
	Node();
	void AddNextNode(int id);
	bool Search(int st, int ed);
	bool check[NODE_NUM];
	bool link[NODE_NUM];
};

Node::Node()
{
	next_num = 0;
	for (int i = 0; i < NODE_NUM; i++) {
		check[i] = false;
	}
	for (int i = 0; i < NODE_NUM; i++) {
		link[i] = false;
	}
}

void Node::AddNextNode(int id)
{
	next_node_list[next_num] = id;
	next_num++;
}

Node *Node_list;

bool Node::Search(int st, int ed)
{
	if (link[ed]) return true;

	for (int k = 0; k < next_num; k++) {
		Node_list[st].link[next_node_list[k]] = true;
		Node_list[next_node_list[k]].link[st] = true;
	}

	for (int i = 0; i < next_num; i++) {
		if (next_node_list[i] == ed) return true;
	}
	
	for (int j = 0; j < next_num; j++) {
		if (Node_list[next_node_list[j]].check[ed] == 0) {
			Node_list[next_node_list[j]].check[ed] = 1;
			que.push(next_node_list[j]);
		}
	}
	
	return false;
}

int main(void)
{
	//input
	int n;
	cin >> n;
	Node_list = new Node[n]();
	int m;
	cin >> m;
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		Node_list[s].AddNextNode(t);
		Node_list[t].AddNextNode(s);
	}
	
	//output
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t;
		Node_list[s].check[t] = true;
		bool ret = Node_list[s].Search(s, t);
		while (!ret && !que.empty()) {
			ret = Node_list[que.front()].Search(s, t);
			que.pop();
		};
		while (!que.empty()) {
			que.pop();
		}

		if (ret) {
			cout << "yes" << "\n";
		}
		else {
			cout << "no" << "\n";
		}
	}
	
	return 0;
}