Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	for i := 0; i < n; i++ {
		group[i] = i
	}
	
	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())

		if group[s] < group[t] {
			group[t] = group[s]
		} else {
			group[s] = group[t]
		}
	}

	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 root(s, &group) == root(t, &group) {
			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 <iostream>
#include <vector>

using namespace std;

class Disjoint {
public:
    Disjoint(int n) {
        for (int i = 0; i < n; i++) {
            id.push_back(i);
            size.push_back(1);
        }
    }

    bool isConnected(int s, int t) {
        return find(s) == find(t);
    }

    int find(int vertex) {
        while (vertex != id[vertex]) {
            id[vertex] = id[id[vertex]];
            vertex = id[vertex];
        }
        return vertex;
    }

    void connect(int s, int t) {
        int sRoot = find(s);
        int tRoot = find(t);

        if (sRoot == tRoot)
            return;

        if (size[sRoot] < size[tRoot]) {
            size[tRoot] += size[sRoot];
            id[sRoot] = id[tRoot];
        }
        else {
            size[sRoot] += size[tRoot];
            id[tRoot] = id[sRoot];
        }
    }

private:
    vector<int> id;
    vector<int> size;
};

int main() {
    int n, m;
    cin >> n >> m;

    Disjoint disjoint = Disjoint(n);
    int s, t;
    for (int i = 0; i < m; i++) {
        cin >> s >> t;
        disjoint.connect(s, t);
    }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> s >> t;
        if (disjoint.isConnected(s, t))
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<stack>
#include<vector>
#include<string.h>
#define MAX 100005
#define FOR(i,a,b) for(int i=a;i<b;i++)
using namespace std;
int N,M;
int Time=0;
vector<int> V[MAX];
int sup[MAX];
void DFS(int i,int u)
{
	stack<int> Q;
	Q.push(i);
	sup[i]=u;
	while(!Q.empty())
	{
		int k=Q.top();
		Q.pop();
		FOR(j,0,V[k].size())
			if(!sup[V[k][j]])
			{
				sup[V[k][j]]=u;
				Q.push(V[k][j]);
			}
	}
}
void solve()
{
	FOR(i,0,N)
		if(!sup[i])
			DFS(i,++Time);
}
int main()
{
	memset(sup,0,sizeof(sup));
	int s,t,q;
	scanf("%d %d",&N,&M);
	FOR(i,0,M)
	{
		scanf("%d %d",&s,&t);
		V[s].push_back(t);
		V[t].push_back(s);
	}
	solve();
	
	scanf("%d",&q);
	while(q--)
	{
		scanf("%d %d",&s,&t);
		if(sup[s]==sup[t]) printf("yes\n");
		else printf("no");
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define pb push_back
using namespace std;
int nam[100001];
bool vis[100001];
vector<int> a[100001];
queue<int> q;
bool ok=false;
int n,u,k,x,m,qq,s,e,t=1;
void dfs(int k){
	 if (k==e)
	    ok=true;
	 if (ok) return; 
     vis[k]=true;
	 for (int i=0; i<a[k].size(); i++){
	     if (!vis[a[k][i]] && !ok){
	     	dfs(a[k][i]);
		 }
     }
}
int main(){
	memset(a,0,sizeof(a));
	memset(vis,false,sizeof(vis));
	memset(nam,0,sizeof(nam));
	cin>>m>>n;
	for (int i=0; i<n; i++){
		cin>>u>>k;
		a[u].pb(k);
		a[k].pb(u);
	}
	cin>>qq;
	while (qq--){
	       ok=false; 
	       while (!q.empty()) q.pop();
		   cin>>s>>e;
		   if (!vis[s]){
		      q.push(s); 
		      while (!q.empty()){
		   	        x=q.front(); vis[x]=true; q.pop(); 
		   	        nam[x]=t;
				    for (int i=0; i<a[x].size(); i++){
	                    if (!vis[a[x][i]])
	     	               q.push(a[x][i]);
                    }
		      }
		      t++;
		  }
		  if (nam[s]==nam[e])
		      cout<<"yes"<<endl;
		   else 
		      cout<<"no"<<endl;	
	} 
	return 0;
}

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

bool DFS(const vector<set<int> >& G, int a, int b) {
    if (G[a].size() == 0) return false;

    stack<pair<int, set<int>::iterator> > S;
    vector<bool> is_visited(G.size(), false);

    set<int>::iterator idx = G[a].begin();
    do {
        if (idx == G[a].end()) {
            a   = S.top().first;
            idx = S.top().second;
            S.pop();
            idx++;
            continue;
        }
        if (*idx == b) return true;
        if (!is_visited[*idx]) {
            is_visited[*idx] = true;
            S.push(make_pair(a, idx));
            a   = *idx;
            idx = G[a].begin();
            continue;
        }
        idx++;
    } while (!S.empty());
    return false;
}


int main(){
    int n, m;
    cin >> n >> m;

    vector<set<int> > G(n);
// bool D[100000][100000];

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

// for (int i = 0; i < n; i++) {
//     cout << i << " : ";
//     for (set<int>::iterator it = G[i].begin(); it != G[i].end(); it++){
//         cout << *it << " ";
//     }
//     cout << endl;
// }



    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        if (DFS(G, a, b)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}

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

bool DFS(const vector<set<int> >& G, int a, int b) {
    if (G[a].size() == 0) return false;

    stack<pair<int, set<int>::iterator> > S;
    vector<bool> is_visited(G.size(), false);

    set<int>::iterator it = G[a].begin();
    do {
        if (it == G[a].end()) {
            a   = S.top().first;
            it = S.top().second;
            S.pop();
            it++;
            continue;
        }
        if (*it == b) return true;
        if (!is_visited[*it]) {
            is_visited[*it] = true;
            if (G[a].find(b) != G[a].end()) return true;
            S.push(make_pair(a, it));
            a   = *it;
            it = G[a].begin();
            continue;
        }
        it++;
    } while (!S.empty());
    return false;
}


int main(){
    int n, m;
    cin >> n >> m;

    vector<set<int> > G(n);
// bool D[100000][100000];

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

// for (int i = 0; i < n; i++) {
//     cout << i << " : ";
//     for (set<int>::iterator it = G[i].begin(); it != G[i].end(); it++){
//         cout << *it << " ";
//     }
//     cout << endl;
// }



    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        if (DFS(G, a, b)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}