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 <iostream>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
#define N 100
#define MAX 100000
#define NIL -1
#define rep(i, n) for(int i = 0; i < (n); ++i)

int n;
vector<int> G[MAX];
int color[MAX];

void dfs_visits(int r, int id){
    stack<int> S;
    S.push(r);
    color[r] = id;
    while(!S.empty()){
        int u = S.top(); S.pop();
        rep(j, G[u].size()){
            int v = G[u][j];
            if(color[v] == NIL) {
                color[v] = id;
                S.push(v);
            }
        }
    }
}

void dfs(){
    // initialization
    rep(i, n) color[i] = NIL;
    int id = 1;
    rep(u, n){
        if (color[u] == NIL) dfs_visits(u, id++);
    }
}

int main()
{
    int s, t, m, q;
    cin >> n >> m;
    rep(i, m){
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    dfs();
    
    cin >> q;
    vector<string> ans(q);

    rep(i, q){
        cin >> s >> t;
        if(color[s] == color[t]) ans[i] = "yes";
        else ans[i] = "no";
    }

    rep(i, q){
        cout << ans[i] << endl;
    }

    return 0;
}

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

static const int MAX = 100000;
static const int 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 = 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 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;
    }
  }
};

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <sstream>
#include <string>
#include <utility>
#include <map>
#include <memory>
#include <set>
#include <vector>
#include <deque>
#include <list>
#include <stack>
#include <queue>

using namespace std;

class Friend
{
public:
  vector<int> v;
  int solve();
  bool isFriend( vector<vector<int>>& adj, vector<int>& visited, int u, int v );
};

bool Friend::isFriend( vector<vector<int>>& adj, vector<int>& visited, int u, int v )
{
  if ( u == v ) return true;

  if ( visited[u] ) return false;

  visited[u] = 1;

  int n = adj[u].size();
  
  for ( auto i = 0; i < n; ++i )
    {
      if ( isFriend( adj, visited, adj[u][i], v ) ) return true;
    }

  return false;
}

int Friend::solve()
{
  int n, m;
  cin >> n >> m;
  
  vector<vector<int>> adj( n );

  for ( auto i = 0; i < m; ++i )
    {
      int u, v;
      cin >> u >> v;
      adj[u].push_back( v );
      adj[v].push_back( u );
    }

  int q;
  cin >> q;

  for ( auto i = 0; i < q; ++i )
    {
      int u, v;
      cin >> u >> v;
      vector<int> visited( n, 0 );
      
      if ( isFriend( adj, visited, u, v ) )
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  
  return 0;
}

int main()
{
  Friend f;

  f.solve();

  return 0;
}

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

int *P;

int root(int a)
{
    if(P[a] == a) return a;
    P[a] = root(P[a]);
    return P[a]; 
}

void join(int a, int b){ int x = root(a), y = root(b); P[x] = y; }

bool same(int a, int b){ return (root(a) == root(b) ? true : false); }

int main()
{
    int i, n, m, a, b, q;
    scanf("%d %d", &n, &m);
    P = new int [n];
    for(i = 0; i < n; i++) P[i] = i;

    for(i = 0; i < m; i++){ scanf("%d %d", &a, &b); join(a, b); }

    scanf("%d", &q);
    for(i = 0; i < q; i++){
        scanf("%d %d", &a, &b);
        if(same(a, b)){ printf("yes\n"); }else{ printf("no\n"); }
    }
    delete [] P;
    return 0; 
}

C++: #include <bits/stdc++.h>
#define MAX 1000001
//#define EZPZ_LEMON_SQUEEZY
using namespace std;
vector<int> M[MAX];
int n, e, q;
bool vis[MAX];
int color[MAX];
void dfs(int index, int cor)
{
	vis[index] = 1;
	color[index] = cor;
	for (int i = 0; i < M[index].size(); i++) {
		if (!vis[M[index][i]]) {
			dfs(M[index][i], cor);
		}
	}
}

int main()
{
#ifdef EZPZ_LEMON_SQUEEZY
	freopen("D:\\out.txt", "w", stdout);
#endif
	ios::sync_with_stdio(0);
	int s, t;
	cin >> n >> e;
	for (int i = 0; i < e; i++) {
		cin >> s >> t;
		M[s].push_back(t);
		M[t].push_back(s);
	}
	memset(vis, 0, sizeof(vis));
	memset(color, 0, sizeof(color));
	int c = 1;
	for (int i = 0; i < n; i++) {
		if (!vis[i]) {
			dfs(i, c);
			c++;
		}
	}

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

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

static const int MAX = 100000;
static const int 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 = 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 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;
}