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<iostream>
#include<climits>
#include<vector>
#include<list>
#include<functional>
#include<algorithm>
#include<string>
#include<cmath>
#include<complex>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<tuple>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF (int)1e9
#define INFL (ll)1e11
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

string dfs(int s, int t, vector<vector<int>> v, vector<int> &used) {
	if (s == t) return "yes\n";
	used[s] = 1;
	int n = v.size();
	int k = v[s].size();
	REP(l, k) {
		int j = v[s][l];
		if (!used[j]) {
			return dfs(j, t, v, used);
		}
	}
	return "no\n";
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n, m;
	cin >> n >> m;
	vector<vector<int>> v(n);
	REP(i, m) {
		int s, t;
		cin >> s >> t;
		v[s].push_back(t);
		v[t].push_back(s);
	}
	int q;
	cin >> q;
	REP(i, q) {
		int s, t;
		cin >> s >> t;
		vector<int> used(n, 0);
		cout << dfs(s, t, v, used);
	}
	

	return 0;
}

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

bool is_friend(const vector<vector<int> >& u, int s, int t, vector<int>& searched){
    if (s == t) return true;
    for (int i = 0; i < u[s].size(); i++) {
        if (!searched[u[s][i]]) {
            searched[u[s][i]] = 1;
            if (is_friend(u, u[s][i], t, searched)) return true;
        }
    }
    return false;
}
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;
        vector<int> searched(n, 0);
        searched[s] = 1;
        if (is_friend(u, s, t, searched)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
static const int MAX=100000;
static const int NIL=-1;

int n;
std::vector<int> G[MAX];
int color[MAX];
void DFS(int,int);
void assignColor(void);

int main(){
  int s, t, m, q;
  std::cin >> n >> m;
  for(int i=0; i<m; i++){
    std::cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }
  assignColor();
  std::cin >> q;
  for(int i=0; i<q; i++){
    std::cin >> s >> t;
    if(color[s] == color[t]) std::cout << "yes\n";
    else std::cout << "no\n";
  }
  return 0;
}
void assignColor(void){
  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++);
}
void DFS(int r, int c){
  std::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);
      }
    }
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define NIL -1

int c = 0,n,m;
int color[100000];
vector<int> li[100000];

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 < li[u].size();i++) {
      if(color[li[u][i]] == NIL) {
        S.push(li[u].at(i));
        color[li[u][i]] = c;
      }
    }
  }
}

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



int main() {

  
  cin >> n >> m;


  for(int i = 0;i < m;i++) {
    int a,b;
    cin >> a >> b;
    li[a].push_back(b);
    li[b].push_back(a);
  }

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

  int q;
  cin >> q;

  assignColor();

  for(int i = 0;i < q;i++) {
    int a,b;
    cin >> a >> b;
    cout << ((color[a] == color[b]) ? "yes" : "no") << endl;
  }


}

C++: #include <stdio.h>
#define N 100000

int a[N]={};

int search(int c)
{
	if(c==a[c]) return c;
	
	return a[c]=search(a[c]);
}

int main(void)
{
 int i,n,m,q,s,t;

 for(i=0;i<N;i++) a[i]=i;

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

 for(i=0;i<m;i++){

  scanf("%d %d",&s,&t);

  if(search(s)==search(t)) a[t]=s;

 }

 scanf("%d",&q);

 for(i=0;i<q;i++){
 
  scanf("%d %d",&s,&t);

  if(search(s)==search(t)) printf("yes\n");
  else printf("no\n");

 }

 return 0;
}

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

vector<int> list[100000];
int n,m;

bool bfs(int s, int t){
  bool used[m];
  for(int i=0;i<m;i++) used[i]=false;
  queue<int> Q;
  Q.push(s); used[s]=true;
  while(!Q.empty()){
    int k=Q.front(); Q.pop();
    if(k==t){return true;}
    for(int i=0;i<list[k].size();i++){
      int temp=list[k][i];
      if(!used[temp]){
        Q.push(temp); used[temp]=true;
      }
    }
  }
  return false;
}
int main(){
  cin>>n>>m;
  for(int i=0;i<m;i++){
    int u,v; cin>>u>>v;
    list[u].push_back(v);
    list[v].push_back(u);
  }
  int q; cin>>q;
  for(int i=0;i<q;i++){
    int s,t; cin>>s>>t;
    if(bfs(s, t)){
      cout<<"yes"<<"\n";
    }else{
      cout<<"no"<<"\n";
    }
  }
}