Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = []int{}
		group[i] = -1
	}
	
	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())
		
		g[s] = append(g[s], t)
		g[t] = append(g[t], s)
	}
	for i := 0; i < n; i++ {
		if group[i] != -1 {
			continue
		}
		q := []int{i}
		for len(q) > 0 {
			pop := q[0]
			if len(q) > 1 {
				q = q[1:]
			} else {
				q = []int{}
			}
			if group[pop] == -1 {
				q = append(q, g[pop]...)
				group[pop] = i
			}
		}
	}
	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 group[s] == group[t] {
			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<cstring>

using namespace std;

int n,m,q,v[100005],color[100005];

struct edge{
    int index;
    edge *next;
};

struct head{
    int index;
    edge *first;
}heads[100005];

void dfs(int pre,int num,int c){
    v[num]=1;
    color[num]=c;
    edge *e=heads[num].first;
    while(e!=NULL){
        if(e->index!=pre&&v[e->index]==0)
            dfs(num,e->index,c);
        e=e->next;
    }
}

int main(){
    memset(v,0,sizeof(v));
    memset(color,0,sizeof(color));
    memset(heads,0,sizeof(heads));
    cin>>n>>m;
    int i,j,c=0;
    for(i=0;i<n;i++){
        heads[i].index=i;
    }
    for(i=0;i<m;i++){
        int a,b;
        cin>>a>>b;
        edge *e1=new edge();
        edge *e2=new edge();
        e1->index=b;
        e2->index=a;
        e1->next=heads[a].first;
        e2->next=heads[b].first;
        heads[a].first=e1;
        heads[b].first=e2;
    }
    for(i=0;i<n;i++){
        if(v[i]==0)dfs(-1,i,++c);
    }
    cin>>q;
    for(i=0;i<q;i++){
        int a,b;
        cin>>a>>b;
        if(color[a]==color[b])cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
#define MAX 100000
#define 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);
            }
        }
    }
}

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

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

    assignColor();

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

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

// ---------------------
// repetition
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

// debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
typedef long long ll;
vector<int> par;

void init(int n) {
  par = vector<int>(n);
  REP(i, n) {
    par[i] = i;
  }
}
int root(int key) {
  if (par[key] == key) return key;
  return root(par[key]);
}
int same(int a, int b) {
  return root(a) == root(b);
}
void unite(int a, int b) {
  int x = root(a);
  int y = root(b);

  if (x == y) return;

  par[x] = y;
}

int main() {
  int n, m;
  cin >> n >> m;

  init(n);

  REP(_, m) {
    int a, b;
    // cin >> a >> b;
    scanf("%d %d", &a, &b);
    unite(a, b);
  }
  int q;
  cin >> q;
  REP(_, q) {
    int a, b;
    // cin >> a >> b;
    scanf("%d %d", &a, &b);
    if (same(a, b)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define NIL -1

typedef vector<int> Adj_list;
typedef vector<Adj_list> Adj_lists;
typedef vector<int> Group_ids;

void set_group_ids(const Adj_lists& adj_lists, Group_ids& group_ids){

  int id = -1;
  for(int i=0; i<adj_lists.size(); i++){
    if(group_ids.at(i) != NIL) continue;
    else id++;

    queue<int> q;
    q.push(i);

    while(!q.empty()){
      // visit top of queue
      const int& visit = q.front();
      const Adj_list& adj_list = adj_lists.at(visit);

      // push adjecent node which is not visited yet
      for(const int& adj : adj_list){
        if(group_ids.at(adj) == NIL){
          q.push(adj);
        }
      }

      group_ids.at(visit) = id;
      q.pop();
    }
  }
}

int main(){

  int n, m;
  scanf("%d %d", &n, &m);

  Adj_lists adj_lists(n);
  for(int i=0; i<m; i++){
    int a, b;
    scanf("%d %d", &a, &b);
    adj_lists.at(a).push_back(b);
    adj_lists.at(b).push_back(a);
  }

  Group_ids group_ids(n+1, NIL);
  set_group_ids(adj_lists, group_ids);

  int q;
  scanf("%d", &q);
  for(int i=0; i<q; i++){
    int a, b;
    scanf("%d %d", &a, &b);
    if(group_ids.at(a) == group_ids.at(b)) printf("yes\n");
    else printf("no\n");
  }

  return 0;
}

C++: #include <bits/stdc++.h>

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int n, m;
struct node {
    int parent;
    vector<int> child;
};

int main() {
    cin >> n >> m;
    node person[n];
    REP(i, n)
        person[i].parent = -1;
    REP(i, m){
        int s, t;
        cin >> s >> t;
        if(person[s].parent == -1 && person[t].parent == -1){
            person[s].parent = s;
            person[t].parent = s;
            person[s].child.push_back(t);
        } else if(person[s].parent == -1){
            person[s].parent = person[t].parent;
            person[person[t].parent].child.push_back(s);
        } else if(person[t].parent == -1){
            person[t].parent = person[s].parent;
            person[person[s].parent].child.push_back(t);
        } else {
            REP(j, person[person[t].parent].child.size())
                person[person[person[t].parent].child[j]].parent = person[s].parent;
            int sz = person[person[t].parent].child.size();
            REP(j, sz)
                person[person[s].parent].child.push_back(person[person[t].parent].child[j]);
            person[person[t].parent].child.clear();
        }
    }
    int q;
    cin >> q;
    REP(i, q){
        int s, t;
        cin >> s >> t;
        if(person[s].parent != -1 && person[t].parent != -1  && person[s].parent == person[t].parent)
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    return 0;
}