Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = []int{}
		group[i] = -1
	}
	
	fmt.Scan(&m)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < m; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		
		g[s] = append(g[s], t)
		g[t] = append(g[t], s)
	}
	for i := 0; i < n; i++ {
		if group[i] != -1 {
			continue
		}
		q := []int{i}
		for len(q) > 0 {
			pop := q[0]
			if len(q) > 1 {
				q = q[1:]
			} else {
				q = []int{}
			}
			if group[pop] == -1 {
				q = append(q, g[pop]...)
				group[pop] = i
			}
		}
	}
	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if group[s] == group[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define loop(i,a,b) for(i=a;i<b;i++)
#define rloop(i,a,b) for(i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
const int inf=1000000001;
const ll INF=1e16;
#define MOD 1000000007
#define mod 1000000009
#define pi 3.14159265358979323846
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;
static const int N=100000;

int n;
vii v(N,vi());
vi color(N),nt(N);

bool dfs(int,int);
int next(int);

int main(){
	int m,i,j;
	cin>>n>>m;
	int s,t;
	loop(i,0,m){
		cin>>s>>t;
		v[s].push_back(t);
		v[t].push_back(s);
	}
	int q;
	cin>>q;
	loop(i,0,q){
		cin>>s>>t;
		bool flag=dfs(s,t);
		if(flag==true){
			cout<<"yes"<<endl;
		}else{
			cout<<"no"<<endl;
		}
	}
}

bool dfs(int s,int t){
	if(s==t){
		return true;
	}
	int i;
	loop(i,0,n+1){
		color[i]=WHITE;
	}
	stack<int> st;
	st.push(s);
	color[s]=GRAY;
	while(!st.empty()){
		int u=st.top();
		int u2=next(u);
		if(u2==t){
			return true;
		}else if(u2!=-1){
			color[u2]=GRAY;
			st.push(u2);
		}else{
			color[u]=BLACK;
			st.pop();
		}
	}
	return false;
}

int next(int u){
	int i;
	loop(i,0,v[u].size()){
		if(color[v[u][i]]==WHITE){
			return v[u][i];
		}
	}
	return -1;
}

C++: #include<iostream>
#include<vector>
#include<stack>
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;
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
#define max 100000
#define 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;
}

C++: #include<iostream>
#include<vector>
#include<stack>

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

C++: #include <iostream>
#include <vector>
#include <set>

using namespace std;

vector<set<int>> A;

bool dfs(int s, int t) {
    auto& S = A[s];
    if (S.find(t) != S.end()) {
        return true;
    }
    for (auto it = S.begin(); it != S.end(); ++it) {
        if (*it != t) {
            auto i = *it;
            if (dfs(i, t)) {
                return true;
            }
        }
    }
    return false;
}

int main() {
    int n;
    int m;
    cin >> n >> m;
    A.resize(n);
    for (int i = 0; i < m; ++i) {
        int s, t;
        cin >> s >> t;
        A[s].insert(t);
    }

    int q;
    cin >> q;
    for (int j = 0; j < q; ++j) {
        int x, y;
        cin >> x >> y;

        if (dfs(x, y) || dfs(y, x)) {
            cout << "yes";
        } else {
            cout << "no";
        }
        cout << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
using namespace std;

const int N = 10000;
int n, m, q;
int M[N][N];
bool V[N];

int bfs(int u, int v) {
  int s;
  queue<int> q;
  REP(i, n) V[i] = false;

  s = u;
  q.push(s);
  V[s] = true;

  while (!q.empty() && !V[v]) {
    s = q.front(); q.pop();
    REP(i, n) {
      if (M[s][i] == 1 && !V[i]) {
        q.push(i);
        V[i] = true;
      }
    }
  }

  return V[v];
}

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

  REP(i, n) {
    REP(j, n) {
      M[i][j] = 0;
    }
  }

  cin >> n >> m;
  REP(i, m) {
    int u, v;
    cin >> u >> v;
    M[u][v] = M[v][u] = 1;
  }

  cin >> q;
  REP(i, q) {
    int u, v;
    string answer;
    cin >> u >> v;
    answer = bfs(u, v) ? "yes" : "no";
    cout << answer << endl;
  }
}