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>
using namespace std;
#include<stdio.h>

typedef struct box { int val; box *prev, *next; } box_t;

box_t* init()
{
    box_t* head = new box_t;
    head->val = -1;
    head->next = NULL;
    head->prev = NULL;
    return head;
}

box_t* make_tail(box_t* head)
{
    box_t* tail = new box_t;
    tail->val = -1;
    head->next = tail;
    tail->next = NULL;
    tail->prev = head;
    return tail;
}

bool isEmpty(box_t* head, box_t* tail)
{
    return (head->next == tail ? true : false);
}

box_t* f_insert(box_t* head, int data)
{
    box_t* box = new box_t;
    box->val = data;
    box->next = head->next;
    box->prev = head;
    head->next->prev = box;
    head->next = box;
    return box;
}

int b_pop(box_t* tail)
{
    if(!(tail->prev->prev)) return -1;
    box_t* box = tail->prev;
    int val;
    val = tail->prev->val;
    tail->prev = box->prev;
    box->prev->next = tail;
    delete box;
    return val;
}

void clear(box_t* head, box_t* tail)
{
    box_t* box1 = head->next;
    box_t* box2;
    if(box1 == tail) return;
    while(box1 != tail){
        box2 = box1->next;
        delete box1;
        box1 = box2;
    };
    head->next = tail;
    tail->prev = head;
    return;
}

int main()
{
    int G[100000][100000];
    int dist[100000];

    box_t* head = init();
    box_t* tail = make_tail(head);

    int n, i, j, e, q, v1, v2;
    scanf("%d", &n);
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){ G[i][j] = 0; } }
    int m;
    scanf("%d", &m);
    for(int k = 0; k < m; k++){
        scanf("%d %d", &i, &j);
        G[i][0]++; G[j][0]++;
        G[i][G[i][0]] = j;
        G[j][G[j][0]] = i;
    }
    scanf("%d", &q);
    int self, friends;
    bool is_connected;

    for(int k = 0; k < q; k++){
        for(j = 0; j < n; j++) dist[j] = -1;

        scanf("%d %d", &self, &friends);
        f_insert(head, self);
        dist[self] = 0;
        is_connected = false;

        while(1){
            v1 = b_pop(tail);
            e = G[v1][0];
            for(i = 1; i <= e; i++){
                v2 = G[v1][i];
                if(dist[v2] < 0){
                    dist[v2] = dist[v1] + 1;
                    f_insert(head, v2);
                }
            }
            if(dist[friends] > 0){
                is_connected = true;
                break;
            }
            if(isEmpty(head, tail)) break;
        };
        if(is_connected){ printf("yes\n"); }else{ printf("no\n"); }
        if(!isEmpty(head, tail)) clear(head, tail);
    }

    return 0;
}

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

#define inf 1000000007

using namespace std;

vector<vector<int> > connect;
vector<int> visited;
int n;

void dfs(int start,int c){
	stack<int> st;
	st.push(start);
	while(!st.empty()){
		int k = st.top();
		visited[k] = c;
		st.pop();
		for(int i=0;i<connect[k].size();++i){
			int tmp = connect[k][i];
			if(visited[tmp]==-1){
				st.push(tmp);
			}
		}
	}
}

int main(){
	int m,q;
	cin >> n >> m;
	connect.resize(n);
	visited.resize(n);
	for(int i=0;i<m;++i){
		int s,t;
		cin >> s >> t;
		connect[s].push_back(t);
		connect[t].push_back(s);
	}
	int c = 0;
	for(int i=0;i<n;++i)visited[i] = -1;
	for(int i=0;i<n;++i){
		if(visited[i] == -1){
			dfs(i,c);
			c++;
		}
	}
	cin >> q;
	vector<bool> res(q);
	for(int i=0;i<q;++i){
		int from,to;
		cin >> from >> to;
		if(visited[from] == visited[to]){
			res[i] = true;
		}else{
			res[i] = false;
		}
	}
	for(int i=0;i<q;++i){
		if(res[i])cout << "yes" << "\n";
		else cout << "no" << "\n";
	}
}

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

void bfs(int s);

int n_user,m,*deg;
vector<int> *G; 

int main(void){
	cin>>n_user>>m;
	deg = new int[n_user];
	G = new vector<int>[n_user];
	for(int i=0;i<n_user;i++)
		deg[i] = -1;

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

	for(int i=0;i<n_user;i++){
		cout<<i<<" : ";
		for(int j=0;j<G[i].size();j++){
			cout<<G[i][j]<<" ";
		}
		cout<<endl;
	}

	for(int i=0;i<n_user;i++) bfs(i);

	int question,s,t;
	cin>>question;
	for(int i=0;i<question;i++){
		cin>>s>>t;
		if(deg[s]!=-1){
			if(deg[s]==deg[t])
				cout<<"yes"<<endl;
			else
				cout<<"no"<<endl;
		}
	}

	delete [] G; delete [] deg;
}

void bfs(int s){
	queue<int> q;
	q.push(s);
	if(deg[s]==-1) deg[s] = s;
	else return ;

	int u;
	while(!q.empty()){
		u = q.front();
		q.pop();
		for(int v=0;v<G[u].size();v++){
			if(deg[G[u][v]]==-1){
				deg[G[u][v]] = deg[u];
				q.push(G[u][v]);
			}
		}
	}
}

C++: #include <iostream>
#include <algorithm>
#include <climits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
vector<int> g[100000];
vector<int> c(100000);

void dfs(int v, int col) {
    c[v] = col;
    for (int i = 0; i < g[v].size(); ++i) {
        if(c[g[v][i]] == 0) dfs(g[v][i], col);
    }
}

int main() {
    int n, m, q;
    cin >> n>> m;
    for (int i = 0; i < m; ++i) {
        int v, w;
        cin >> v >> w;
        g[w].push_back(v);
        g[v].push_back(w);
    }

    for (int k = 0; k < n; ++k) {
        c[k] = 0;
    }

    int col = 1;
    for (int j = 0; j < n; ++j) {
        if(!c[j]) {
            dfs(j, col);
            col++;
        }
    }

    cin >> q;
    for (int l = 0; l < q; ++l) {
        int v,w;
        cin >> v >> w;
        if(c[v] == c[w]) cout << "yes";
        else cout << "no";
        cout << endl;

    }


    return 0;
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

const int SIZE = 100001;
int group[SIZE];

void assignGroup(int u, int g, std::vector<int> *a) {
  if (group[u] != 0) return;

  group[u] = g;
  // for (int i = 0; i < a[u].size(); ++i) {
  //   if (a[u][i] && group[i] == 0) assignGroup(i, g, a);
  // }
  for (auto v : a[u]) {
    // printf("u: %d, v: %d, group[u]: %d, group[v]: %d\n", u, v, group[u],
    //        group[v]);
    if (group[v] == 0) assignGroup(v, g, a);
  }
}

bool isConnected(int s, int t) { return group[s] == group[t]; }

int main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<int> a[n];

  rep(i, m) {
    int s, t;
    std::cin >> s >> t;
    a[s].push_back(t);
    a[t].push_back(s);
  }

  // std::cout << "/* message */" << '\n';
  // rep(i, n) {
  //   for (auto v : a[i]) std::cout << v << ' ';
  //   std::cout << '\n';
  // }
  // std::cout << "/* message */" << '\n';

  int g = 0;
  rep(i, n) {
    if (group[i] == 0) { assignGroup(i, ++g, a); }
  }

  // rep(i, n) std::cout << group[i] << ' ';

  int q;
  std::cin >> q;
  rep(i, q) {
    int s, t;
    std::cin >> s >> t;
    if (isConnected(s, t))
      std::cout << "yes" << '\n';
    else
      std::cout << "no" << '\n';
  }

  return 0;
}