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++: #define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

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

vector<int> vec[100010],color(100010,-1);

void dfs(int u){
  if(color[u]==-1) color[u]=u;
  
  stack<int> s;
  s.push(u);
  while(!s.empty()){
    int r=s.top(); s.pop();
    for(auto x:vec[r]){
      if(color[x]==-1){
        color[x]=u;
        s.push(x);
      }
    }
  }
}
  

int main() {
  int n,m,q;
  cin>>n>>m;
  
  int s,t;
  rep(i,m){
    cin>>s>>t;
    vec[s].push_back(t);
    vec[t].push_back(s);
  }
  
  rep(i,n){
    if(color[i]==-1) dfs(i);
  }
  
  cin>>q;
  rep(i,q){
    cin>>s>>t;
    if(color[s]==color[t]) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
const ll mod = ll(1e9) + 7;
const int INF = int(1e9);

int par[100000]; //parent
int rnk[100000]; //depth

//init with n elements
void init(int n) {
	for (int i = 0; i < n; i++) {
		par[i] = i;
		rnk[i] = 0;
	}
}
//find a root of the tree
int find(int x) {
	if (par[x] == x) {
		return x;
	}
	else {
		return par[x] = find(par[x]);
	}
}

//merge the sets contains x,y
void unite(int x, int y) {
	x = find(x);
	y = find(y);
	if (x == y)return;

	if (rnk[x] < rnk[y]) {
		par[x] = y;
	}
	else {
		par[y] = x;
		if (rnk[x] == rnk[y])rnk[x]++;
	}
}

//the set contains x and y or not
bool same(int x, int y) {
	return find(x) == find(y);
}
int main()
{
    int n,m;
    cin>>n>>m;
    init(n);
    rep(i,m){
        int a,b;
        cin>>a>>b;
        unite(a,b);
    }
    int q;
    cin>>q;
    vector<string>ans(q);
    rep(i,q){
        int a,b;
        cin>>a>>b;
        if(same(a,b))ans[i]="yes";
        else ans[i]="no";
    }
    rep(i,q)cout<<ans[i]<<endl;
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main(){
        int N,M;
        cin >> N >> M;
        vector <vector <int> > friends(N,vector <int>(N));
        vector <int> groups(N,-1);
        for(int i=0 ; i<M ; i++ ){
                int s,t;
                cin >> s >> t;
                friends[s][t]=friends[t][s]=1;
        }
        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();
                        for(int l=0 ; l<N ; l++ ){
                                if(friends[now][l] && groups[l]==-1 )
                                        cycle.push(l);
                        }
                        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 <iostream>

using namespace std;

const int N = 100000;
int A[N][N] = {{0}};
int n;

bool dfs(int s, int t) {
    if (A[s][t] == 1) {
        return true;
    }
    for (int i = 0; i < n; ++i) {
        if (i != t && A[s][i] != 0) {
            if (dfs(i, t)) {
                return true;
            }
        }
    }
    return false;
}

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

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

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

    return 0;
}