Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
	"bytes"
)

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

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;
}