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 <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" << "\n";
    }else{
      cout << "no" << "\n";
    }
  }

  return 0;
}

C++: #include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli> > mat;
typedef vector<vector<bool> > matb;
typedef vector<string> vst;
typedef pair<lli,lli> pll;
typedef pair<double,double> pdd;

vll par;
vll rnk;
void init(lli n){
    par = vll(n);
    rnk = vll(n);
    for(lli i = 0;i < n;i++) par[i] = i;
}
lli find(lli x){
    if(par[x] == x) return x;
    else return par[x] = find(par[x]);
}
void unite(lli x,lli 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]++;
    }
}
bool same(lli x,lli y){
    return find(x) == find(y);
}

lli n,m,q;
lli s,t;

int main(){
    cin >> n >> m;
    init(n);
    for(lli i = 0;i < m;i++){
        cin >> s >> t;
        unite(s,t);
    }
    cin >> q;
    for(lli i = 0;i < q;i++){
        cin >> s >> t;
        cout << (same(s,t) ? "yes" : "no" ) << endl;
    }
    return 0;
}

C++: #include<stack>
#include<queue>
#include<conio.h>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
queue<int> Q;
vector<int> G[100000];
bool flag = false;
int s, t;
int color[100000];

void a(){
	while (!Q.empty()){
		Q.pop();
		for (int i = 0; i < G[s].size(); i++){
			//_getch();
			//cout << G[s][i] << endl;
			if (G[s][i] == t){
				flag = true;
				while (!Q.empty()) Q.pop();
				return;
			}
			else if (color[i] == 0){
				Q.push(G[s][i]);
				color[i] = 1;
			}
		}
		if (!Q.empty())s = Q.front();
	}
}

int main(){
	int n, 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);
	}
	cin >> q; fill(color, color + 100000, 0);
	for (int i = 0; i < q; i++){
		cin >> s >> t;
		Q.push(s);
		a();
		if (flag) cout << "yes" << endl;
		else cout << "no" << endl;
		flag = false;
		fill(color, color + 100000, 0);
	}

	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define MAX (int)1e5

vector<int> G[MAX];
queue<int> qu;

int main() {
	int n, m, q;
	int ind,tmp;
	cin >> n >> m;
	bool flag = 0;
	for (int i = 0; i < m; i++) {
		cin >> ind >> tmp;
		G[ind].push_back(tmp);
		G[tmp].push_back(ind);
	}
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> ind >> tmp;
		flag = 0;
		qu.push(ind);
		while (not qu.empty()) {
			int u = qu.front(); qu.pop();
			if (u == tmp) {
				flag = 1;
				break;
			}
			for (int j = 0; j < G[u].size(); j++) {
				qu.push(G[u][j]);
			}
		}
		if (flag) cout << "yes" << endl;
		else cout << "no" << endl;
	}
}

C++: #include <vector>
#include <iostream>
using namespace std;
int n, m, q, x, y; int group[100000]; vector<int> f[100000];
int main() {
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; i++) group[i] = i, f[i].push_back(i);
	for(int i = 0; i < m; i++) {
		scanf("%d%d", &x, &y);
		if(group[x] == group[y]) continue;
		x = group[x], y = group[y];
		if(f[y].size() > f[x].size()) swap(x, y);
		for(int j = f[y].size() - 1; j >= 0; j--) {
			group[f[y][j]] = group[x];
			f[x].push_back(f[y][j]);
			f[y].pop_back();
		}
	}
	scanf("%d", &q);
	for(int i = 0; i < q; i++) {
		scanf("%d%d", &x, &y);
		printf("%s\n", group[x] == group[y] ? "yes" : "no");
	}
	return 0;
}

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

using namespace std;

int main(){
	int n,m;
	cin >> n >> m;
	
	vector<int> es[n];
	int s,t;
	for(int i=0;i<m;i++){
		cin >> s >> t;
		es[s].push_back(t);
		es[t].push_back(s);
	}
	int q;
	cin >> q;
	for(int i=0;i<q;i++){
		cin >> s >> t;
		
		bool used[n];
		fill(used,used+n,0);
		bool flag = false;
		queue<int> que;
		que.push(s);
		used[s] = true;
		while(!que.empty()){
			int v = que.front(); que.pop();
			for(int j=0;j<es[v].size();j++){
				int next = es[v][j];
				if(next == t){
					cout << "yes\n";
					flag = true;
					break;
				}
				if(!used[next]){
					que.push(next);
					used[next] = true;
				}
			}
			if(flag){
				break;
			}
		}
		if(!flag){
			cout << "no\n";			
		}
	}
	return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <utility>
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
using namespace std;

int main(){
	int n,m;
	cin>>n>>m;
	vector<int> a(m),b(m),d(n);
	rep(i,m){
		cin>>a[i]>>b[i];
	}
	int q;
	cin>>q;
	vector<int> s(q),t(q);
	rep(i,q){
		cin>>s[i]>>t[i];
	}
	rep(i,n){
		d[i]=0;
	}
	int check=1;
	int i=1;//???
	int i2=0;//???????????°
	while(check){
		stack<int> st;
		st.push(i2);
		d[i2]=i;
		while(!st.empty()){
			int x=st.top();
			st.pop();
			rep(j,m){
				if(a[j]==x && d[b[j]]==0){
					st.push(b[j]);
					d[b[j]]=i;
				}
				if(b[j]==x && d[a[j]]==0){
					st.push(a[j]);
					d[a[j]]=i;
				}
			}
		}
		check=0;
		rep(j,n){
			if(d[j]==0){
				check=1;
				i++;
				i2=j;
				break;
			}
		}
	}

	rep(i,q){
		if(d[s[i]]==d[t[i]]){
			printf("yes\n");
		}else{
			printf("no\n");
		}
	}



	
}