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<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n) ; i++)
constexpr auto INF = 2147483647;
typedef long long ll;
constexpr int MAX = 100000;

vector<int> Group_list[MAX];
const int NIL = -1;
int Check_list[MAX];
int n;

void Checking(){
  //init
  rep(i,n) Check_list[i] = NIL;
  stack<int> stk;
  int check_cnt = 0;
  //solve
  rep(i,n){
    if(Check_list[i] == NIL) {
      stk.push(i);
      check_cnt++;
      Check_list[i] = check_cnt;
    }
    int now, next;
    while(!stk.empty()){
        now = stk.top();
        stk.pop();
        rep(j, Group_list[now].size()){
          next = Group_list[now][j];
          if(Check_list[next] == NIL) {
            Check_list[next] = check_cnt;
            stk.push(next);
          }
        }
    }
  }
}

int main(){
  int m,q;
  int from, to;
  cin >> n >> m;
  rep(i,m){
    cin >> from >> to;
    Group_list[from].push_back(to);
    Group_list[to].push_back(from);
  }

  Checking();

  cin >> q;
  rep(i,q){
    cin >> from >> to;
    if(Check_list[from] == Check_list[to] && Check_list[from] != NIL){
      cout << "yes" << endl;;
    }else{
      cout << "no" << endl;
    }
  }
  

  return 0;
}

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

void set_color(const vector<vector<int> >& u, int p, vector<int>& color, int c) {
    if (color[p] != -1) return;
    color[p] = c;
    for (int i = 0; i < u[p].size(); i++) {
        set_color(u, u[p][i], color, c);
    }
}

bool is_friend(const vector<vector<int> >& u, int s, int t){
    static vector<int> color(u.size(), -1);
    static int c = 0;
    set_color(u, s, color, c++);
    return color[s] == color[t];
}

int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<int> > u(n);
    for (int i = 0; i < m; i++) {
        int s, t;
        cin >> s >> t;
        u[s].push_back(t);
        u[t].push_back(s);
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int s, t;
        cin >> s >> t;
        if (is_friend(u, s, t)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
using namespace std;
int count = 0;
class edge;

class vertex{
    public:
        int member;
        vector<edge*> edges;
        int label;
        vertex(){
            member = -1;
        }
        void add_member(int n){
            member = n;
        }
};

class edge{
    public:
        vertex *to,*from;
        edge(vertex *_to){
            to = _to;
        }
};

void check_member_dfs(vertex *v){
    v->member = count;
    for(int i = 0; i < v->edges.size(); i++){
        vertex *to = v->edges[i]->to;
        if (to->member >= 0){
            continue;
        }else{
            check_member_dfs(to);
        }
    }
    return;
}

int main(){
    int n,m,temp1,temp2,q;
    cin >> n >> m;
    vertex *V[n];
    for(int i = 0; i < n ; i++) V[i] = new vertex();
    for(int i = 0; i < m; i++){
        cin >> temp1 >> temp2;
        V[temp1]->edges.push_back(new edge(V[temp2]));
        V[temp2]->edges.push_back(new edge(V[temp1]));
    }
    for(int i = 0; i < n; i++){
        if(V[i]->member >= 0){
            continue;
        }else{
            check_member_dfs(V[i]);
            count++;
        }
    }
    cin >> q;
    string result[q];
    for(int i = 0; i < q; i++){
        cin >> temp1 >> temp2;
        if(V[temp1]->member == V[temp2]->member){
            result[i] = "yes";
        }else{
            result[i] = "no";
        }
    }
    for(int i = 0; i < q; i++) cout << result[i] << endl;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main(){
        int N,M;
        cin >> N >> M;
        vector<int>groups(N,-1);
        vector<vector<int> >friends(N);
        for(int i=0 ; i<M ; i++ ){
                int s,t;
                cin >> s >> t;
                friends[s].push_back(t);
                friends[t].push_back(s);
        }
        int group=0;
        for(int i=0 ; i<N ; i++ ){
                if(groups[i]!=-1)
                        continue;
                queue <int> cycle;
                cycle.push(i);
                while(cycle.size()!=0){
                        int now=cycle.front();cycle.pop();
                        vector <int>::iterator itr;
                        for(itr=friends[now].begin() ; itr!=friends[now].end() ; ++itr ){
                                if(groups[*itr]==-1)
                                        cycle.push(*itr);
                        }
                        groups[now]=group;
                }
                group++;
        }
        cin >> N;
        for(int i=0 ; i<N ; i++ ){
                int s,t;
                cin >> s>> t;
                if(groups[s]==groups[t])
                        cout<<"yes"<<endl;
                else
                        cout<<"no"<<endl;
        }
        return 0;
}

C++: #include<stack>
#include<queue>
//#include<conio.h>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
queue<int> Q;
vector<int> G[100000];
bool flag = false;
int s, t;
int color[100000];

void a(){
	while (!Q.empty()){
		Q.pop();
		for (int i = 0; i < G[s].size(); i++){
			//_getch();
			//cout << G[s][i] << endl;
			if (G[s][i] == t){
				flag = true;
				while (!Q.empty()) Q.pop();
				return;
			}
			else if (color[i] == 0){
				Q.push(G[s][i]);
				color[i] = 1;
			}
		}
		if (!Q.empty())s = Q.front();
	}
}

int main(){
	int n, 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);
	}
	cin >> q; fill(color, color + 100000, 0);
	for (int i = 0; i < q; i++){
		cin >> s >> t;
		Q.push(s);
		a();
		if (flag) cout << "yes" << endl;
		else cout << "no" << endl;
		flag = false;
		fill(color, color + 100000, 0);
	}

	return 0;
}