Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	for i := 0; i < n; i++ {
		group[i] = i
	}
	
	fmt.Scan(&m)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < m; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if group[s] < group[t] {
			group[t] = group[s]
		} else {
			group[s] = group[t]
		}
	}

	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if root(s, &group) == root(t, &group) {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

C++: #include <iostream>
#include <queue>
using namespace std;
struct Node{
    vector<int> fri;
    bool flag;
};
queue<Node> refresh(queue<Node> que,Node node[],int n){
    while(!que.empty())
        que.pop();
    
    for(int i=0; i<n; i++)
        node[i].flag=false;

    return que;
}

int main(){
    int n,m,s,t;
    Node node[100000];
    cin >> n >> m;

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

        node[s].fri.push_back(t);
        node[t].fri.push_back(s);
    }

    queue<Node> que;
    refresh(que,node,n);

    int q;
    cin >> q;

    bool answer[10000];
    for(int i=0; i<q; i++){
        answer[i]=false;
        cin >> s >> t;

        node[s].flag=true;
        que.push(node[s]);

        while(!que.empty() && !answer[i]){
         Node fro = que.front();

         for(int j=0; j<fro.fri.size(); j++){
            int x = fro.fri[j];
  
            if(x==t){
                answer[i]=true;
                break;
            }

            if(!node[x].flag){
                node[x].flag=true;
                que.push(node[x]);
            }
         }

          que.pop();
        }

        que=refresh(que,node,n);
    }

    for(int i=0; i<q; i++){
        if(answer[i])
            cout << "yes\n";
        else
            cout << "no\n";
    }
}

C++: #include<iostream>
#include<unordered_map>
#include<unordered_set>

using namespace std;

int main(){
	int n, m, s, t, q, i, j;
	int id, stc = 0;
	bool answer_yes = false;
	unordered_map<int, unordered_set<int> > Adj;
	unordered_map<int, int> Stack;
	unordered_set<int> Check;
	unordered_set<int>::iterator it, itend;

	cin >> n >> m;
	for (i = 0; i < m; i++) {
		cin >> s >> t;
		Adj[s].insert(t);
		Adj[t].insert(s);
	}

	/*for (auto it1 = Adj.begin(); it1 != Adj.end(); it1++) {
		cout << "\n" << it1->first << " : " << flush;
		for (auto it2 = it1->second.begin(); it2 != it1->second.end(); it2++) {
			cout << *it2 << " " << flush;
		}
	}
	cout << endl;*/

	cin >> q;
	for (i = 0; i < q; i++) {
		cin >> s >> t;
		
		// calculate
		Stack.insert(pair<int, int>(stc, s));
		Check.insert(s);
		while (!Stack.empty()) {
			id = Stack[stc];
			//cout << "\nid : " << id << " stc : " << stc << endl;
			itend = Adj[id].end();
			for (it = Adj[id].begin(); it != itend; it++) {
				//cout << *it << " " << flush;
				if (Check.find(*it) == Check.end()) {
					if (*it == t) {
						answer_yes = true;
						Stack.clear();
						break;
					}
					stc++;
					Stack.insert(pair<int, int>(stc, *it));
					Check.insert(*it);
					break;
				}
			}
			if (it == itend) {
				Stack.erase(stc);
				stc--;
			}
		}
		if (answer_yes) {
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
			Stack.clear();
		}
		answer_yes = false;
		Check.clear();
		stc = 0;
	}
	
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
using namespace std;
#define maxn 100000
int n,color[maxn];
vector<int> G[maxn];
void dfs(int g,int c){
    stack<int> s;
    s.push(g);
    color[g] = 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]){
                s.push(v);
                color[v] = c;
            }
        }
    }
}
int main(){
    int m,a,b,q;
    scanf("%d%d",&n,&m);
    while(m--){
        scanf("%d%d",&a,&b);
        G[a].push_back(b);
        G[b].push_back(a);
    }
    int j=1;
    for(int i=0;i<n;i++)
        if(!color[i])
            dfs(i,j++);
    scanf("%d",&q);
    while(q--){
        scanf("%d%d",&a,&b);
        if(color[a]==color[b])
            printf("yes\n");
        else
            printf("no\n");
    }
}

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

queue<int> que;

class Node
{
private:
	//int next_num;
	//int *next_node_list;
	list<int> next_node_list;
public:
	Node();
	void AddNextNode(int id);
	bool Search(int st, int ed);
	bool check; //!
	//bool *check;
	//bool *link;
};

Node::Node()
{
	//next_num = 0;
	check = false;
	/*check = new bool[n];
	for (int i = 0; i < n; i++) {
		check[i] = false;
	}*/
	/*link = new bool[n];
	for (int i = 0; i < n; i++) {
		link[i] = false;
	}*/
}

void Node::AddNextNode(int id)
{
	next_node_list.push_back(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;
	}*/
	list<int>::iterator itr;
	for(itr = next_node_list.begin(); itr != next_node_list.end(); itr++){
		if (*itr == ed) return true;
	}
	
	/*for (int j = 0; j < next_num; j++) {
		//if (Node_list[next_node_list[j]].check[ed] == 0) {
		if (Node_list[next_node_list[j]].check == 0) { //!
		//	Node_list[next_node_list[j]].check[ed] = 1;
			Node_list[next_node_list[j]].check = 1; //!
			que.push(next_node_list[j]);
		}
	}*/
	for(itr = next_node_list.begin(); itr != next_node_list.end(); itr++){
		if (Node_list[*itr].check == 0) {
			Node_list[*itr].check = 1;
			que.push(*itr);
		}
	}
	
	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;
		Node_list[s].check = 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();
		}
		for (int j = 0; j < n; j++) {
			//Node_list[j].check[t] = false;
			Node_list[j].check = false; //!
		}

		if (ret) {
			cout << "yes" << "\n";
		}
		else {
			cout << "no" << "\n";
		}
	}
	
	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[N];

void dfs(int r, int c) {
  stack<int> S;
  S.push(s);
  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);
      }
    }
  }
}

void assignColor() {
  int id = 1;
  for (int i = 0; i < n; i++) {
    color[i] = NIL;
  }
  for (int u = 0; u < n; u++) {
    if (color[u] == NIL) {
      dfs(u, id++);
    }
  }
}

int main() {
  int m, s, t;

  cin >> n >> m;

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

  assignColor();

  int q;
  cin >> q;

  for (int i =0; i < q; i++) {
    cin >> s >> t;
    if (color[s] == color[t]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}