Go: package main

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

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

	for i := 0; i < r; 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)
	}
	assignColor()
	sc.Scan()
	qNum, _ := strconv.Atoi(sc.Text())
	for i := 0; i < qNum; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 0
}

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define MAX_N 500000
vector<int>X[MAX_N]; queue<int>Q;
int COLOR[MAX_N], N, M, Q1, A, B;
int main() {
	cin >> N >> M;
	for (int i = 0; i < M; i++) { cin >> A >> B; X[A].push_back(B); X[B].push_back(A); }
	int cnt = 0;
	for (int i = 0; i < N; i++) {
		if (COLOR[i] == 0) {
			Q.push(i); cnt++; COLOR[i] = cnt;
			while (!Q.empty()) {
				int a = Q.front(); Q.pop();
				for (int j = 0; j < X[a].size(); j++) {
					int to = X[a][j];
					if (COLOR[to] == 0) {
						COLOR[to] = cnt;
						Q.push(to);
					}
				}
			}
		}
	}
	cin >> Q1;
	for (int i = 0; i < Q1; i++) {
		cin >> A >> B;
		if (COLOR[A] == COLOR[B]) {
			cout << "1" << endl;
		}
		else {
			cout << "0" << endl;
		}
	}
	return 0;
}

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