Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	nConns := scanInt(sc)

	graph := make([][]int, nNodes)
	for i := 0; i < nConns; i++ {
		a, b := scanInt(sc), scanInt(sc)
		graph[a] = append(graph[a], b)
		graph[b] = append(graph[b], a)
	}

	groups := make([]int, nNodes)

	var setGroups func(int, int)
	setGroups = func(u int, groupId int) {
		if groups[u] > 0 {
			return
		}
		groups[u] = groupId

		for _, v := range graph[u] {
			setGroups(v, groupId)
		}
	}
	for i := 0; i < nNodes; i++ {
		setGroups(i, i+1)
	}

	nQuestions := scanInt(sc)

	w := bufio.NewWriter(os.Stdout)
	var ans string
	for i := 0; i < nQuestions; i++ {
		a, b := scanInt(sc), scanInt(sc)
		if groups[a] == groups[b] {
			ans = "yes"
		} else {
			ans = "no"
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: /**
 */

/*
 * test case 1
 2 1 1 2 1 1 2  
 test case 2
 4 4 1 2 1 3 2 4 3 4 1 1 2   
 */


#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector< vector<int> > rel;
vector<bool> visited;
int n, m, q;
int dest = 0;


bool fre1(int j) 
{
	for (int i=0;i<rel[j].size();i++)
	{
		int kk = rel[j][i];
		if (kk==dest) return true;
		if (visited[kk])continue;
		visited[kk]=true;
		if(fre1(kk))return true;
	}
	return false;
}

/**
 * preserve a priority que of nodes with the most connections. 
 * if there is a node with only 2 connections go look for the other one.
 */

priority_queue< pair<int, int> > pq;


bool fre3(int start) 
{
	for (int i=0;i<rel[start].size();i++)
	{
		int cand = rel[start][i];
		if (cand==dest) return true;
		if (visited[cand])continue;
		visited[cand]=true;
		int point = rel[cand].size();
		if (point == 1) continue;
		if (point == 2) {
			if (fre3(cand)) return true;
		} else {
			pq.push(pair<int, int>(point, cand));
		}
	}
	return false;
}


bool fre2(int start) 
{
	pq.push(pair<int,int>(0, start));
	while (pq.size())
	{
		pair<int, int> p = pq.top();
		pq.pop();
		start = p.second;
		if (fre3(start)) return true;
	}
	return false;
}


int main()
{
	cin >> n;
	rel.resize(n);
	cin >> m;
	for (int i=1;i<=m;i++){
		int j, k;
		cin >> j >> k;
		rel[j].push_back(k);
		rel[k].push_back(j);
	}

	cin >> q;
	for (int i=1;i<=q;i++){
		int start;
		cin >> start >> dest;
		visited.clear();
		visited.resize(n, false);
		cout << (fre2(start) ? "yes" : "no") << endl;
		while (pq.size()) pq.pop(); // forgot this.
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <math.h>
#include<cstring>
#include <cstdio>
using namespace std;
typedef long long int ll;
#define rep(i, a, n) for (ll i = a; i < (ll)(n); i++)
long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; }
long long LCM(long long a, long long b)  {return a/GCD(a,b)*b;}
int n,m,q;
int st,en;
vector<int> G[100000];
int color[100000];
int cnt=0;
void dfs(int st,int cnt)
{
    color[st]=cnt;
    for(int i=0;i<G[st].size();i++)
    {
        if(color[G[st][i]]==-1)
        {
            dfs(G[st][i],cnt);
        }
    }
}
void makeColor()
{
    for(int i=0;i<n;i++)
    {
        if(color[i]==-1)
        {
            dfs(i,++cnt);
        }
    }
}
int main(){
    memset(color,-1,sizeof(color));
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        int a,b;
        cin>>a>>b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    makeColor();
    cin>>q;
    
    for(int i=0;i<q;i++)
    {
        cin>>st>>en;
        if(color[st]==color[en])
            cout<<"yes"<<endl;
        else
            cout<<"no"<<endl;
    }
   return 0;
}

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

map<int, vector<int>> A;
int color[100001];

void assignColor(int u, int currentColor) {
    queue<int> Q;
    Q.push(u);
    color[u] = currentColor;
    int current;
    while(!Q.empty()) {
        current = Q.front();
        Q.pop();
        vector<int>::iterator it = A[current].begin();
        while(it != A[current].end()) {
            if (color[*it] == 0) {
                Q.push(*it);
                color[*it] = currentColor;
            }
            it++;
        }
    }
}

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

    int u1, u2;
    for (int i = 0; i < m; i++) {
        cin >> u1 >> u2;
        A[u1].push_back(u2);
        A[u2].push_back(u1);
    }

    for (int i = 0; i < n; i++) {
        color[i] = 0;
    }

    int c = 1;
    for (int i = 0; i < n; i++) {
        if (color[i] == 0) {
            assignColor(i, c);
            c++;
        }
    }

    int q, start, target;
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> start >> target;

        if (color[start] == color[target]) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
}

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

#define N 100000
#define WHITE 0  // 未訪問
#define BLACK 1  // 訪問ずみ

// 連結成分の番号を振る関数
void dfs(int n, int comp[], vector<int> G[]) {
  int i, j, vertex, neighbor, color[N];
  stack<int> S;
  // 初期状態
  for (i = 0; i < n; i++) color[i] = WHITE;

  for (i = 0; i < n; i++) {
    if (color[i] == WHITE) {
      // dfs
      S.push(i);
      while (!S.empty()) {
        vertex = S.top();
        S.pop();        
        color[vertex] = BLACK;  // 訪問済みにする
        comp[vertex] = i;  // 連結成分の番号を振る          
        for (j = 0; j < (int) G[vertex].size(); j++) {
          neighbor = G[vertex][j]; // 隣接している頂点：友人
          if (color[neighbor] == WHITE) S.push(neighbor);
        }
      }
    }
  }
}

int main() {
  int i, n, m, q, s, t;
  int comp[N];  // 連結成分の番号
  vector<int> G[N];  // グラフを表現する隣接リスト

  cin >> n >> m;
  // 隣接リストを作成
  for (i = 0; i < m; i++) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  // グラフを連結成分に分ける
  dfs(n, comp, G);

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

C++: #include<iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
using P = pair<int,int>;

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

const int MAX_N = 100100;
const int NO = -1;
const int WHITE = 0, GRAY = 1, BLACK = 2;

int n, m;
vector<int> adl[MAX_N]; // 隣接リスト
int color[MAX_N], group[MAX_N];
queue<int> Q;

void init(){
    rep(i,MAX_N){
        color[i] = WHITE;
        group[i] = NO;
    }
}

void bfs(int u, int c){
    color[u] = GRAY;
    group[u] = c;
    Q.push(u);

    while(!Q.empty()){
        u = Q.front(); Q.pop();
        for(auto v: adl[u]){
            if(color[v] == WHITE){
                color[v] = GRAY;
                group[v] = c;
                Q.push(v);
            }
        }
        color[u] = BLACK;
    }
}

void bfs_visit(){
    rep(i,n){
        if(group[i] == NO) bfs(i, i);
    }
}

int main()
{
    cin >> n >> m;
    init();
    rep(i,m){
        int s, t; cin >> s >> t;
        adl[s].push_back(t);
        adl[t].push_back(s);
    }
    bfs_visit();
    int q; cin >> q;
    rep(_,q){
        int s, t; cin >> s >> t;
        if(group[s] == group[t]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    //rep(i,n) cout << group[i] << endl;
}