Go: package main

import (
	"bufio"
	"container/list"
	"os"
	"strconv"
	"fmt"
)

const (
	MAX = 100000
	NIL = -1
)

var (
	n     int
	G     [MAX][]int
	color [MAX]int
)

func DFS(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			DFS(i, id)
		}
	}
}

func main() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}

	assignColor()
	scanner.Scan()
	q, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < q; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

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