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 <cstdio>
#include <iostream>
#include <vector>
#include <stack>
using namespace std;

static const int MAX_NUM = 100000;

void DFS(int group[], vector<int> vec[], int gnum, int start)
{
    stack<int> S;
    int target;
    int i;
    int tmp;

    S.push(start);
    group[start] = gnum;

    while (S.empty() == false) {
        target = S.top();
        for (i = 0; i < vec[target].size(); i++) {
            if (group[vec[target][i]] == 0) {
                group[vec[target][i]] = gnum;
                S.push(vec[target][i]);
                break;
            }
        }
        if (i == vec[target].size()) {
            S.pop();
        }
    }

    return;
}

void SetupGroup(int group[], vector<int> vec[], int elemnum)
{
    int i;
    int gnum = 0;

    for (i = 0; i < elemnum; i++) {
        if (group[i] == 0) {
            ++gnum;
            DFS(group, vec, gnum, i);
        }
    }
}

int main(void)
{
    int i;
    int num;
    int elemnum;
    int qnum;
    int ele1, ele2;
    vector<int> vec[MAX_NUM];
    int group[MAX_NUM] = {0};

    scanf("%d %d", &elemnum, &num);
    for (i = 0; i < num; i++) {
        scanf("%d %d", &ele1, &ele2);
        vec[ele1].push_back(ele2);
        vec[ele2].push_back(ele1);
    }

    SetupGroup(group, vec, elemnum);

    scanf("%d", &qnum);
    for (i = 0; i < qnum; i++) {
        scanf("%d %d", &ele1, &ele2);
        if (group[ele1] == group[ele2]) {
            printf("yes\n");
        }
        else {
            printf("no\n");
        }
    }
}

C++: #include<iostream>
#include<cstring>
using namespace std;

bool x[10000][10000];
bool y[10000];

int main(){
	int n,m,a,b,q;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		cin>>a>>b;
		x[a][b]=true;
		x[b][a]=true;
	}
	cin>>q;
	for(int h=0;h<q;h++){
		cin>>a>>b;
		memset(y,0,sizeof(y));
		y[a]=true;
		for(int i=0;i<100;i++){
			for(int j=0;j<n;j++){
				if(y[j]==true){
					for(int k=0;k<n;k++){
						if(x[j][k]==true){
							y[k]=true;
						}
					}
				}
				if(y[b]==true){
					cout<<"yes"<<endl;
					goto Exit;
				}
			}
		}
		cout<<"no"<<endl;
		Exit:;
	}
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxx=100010;

int pre[maxx];

int find(int u){
	while(pre[u]!=u) u=find(pre[u]);
	return u;
}

int main(){
	int n,m,x,y;
	cin>>n>>m;
	for(int i=0;i<n;i++) pre[i]=i;
//	for(int i=0;i<n;i++) cout<<i<<" ";cout<<endl;
//	for(int i=0;i<n;i++) cout<<pre[i]<<" ";cout<<endl;
	while(m--){
		cin>>x>>y;
		pre[find(x)]=find(y);
//		for(int i=0;i<n;i++) cout<<i<<" ";cout<<endl;
//		for(int i=0;i<n;i++) cout<<pre[i]<<" ";cout<<endl;
	}
	cin>>m;
	while(m--){
		cin>>x>>y;
		if(find(x)==find(y)) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}

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

using namespace std;

const int N = 100000;
int n;
int color[N];
vector<int> v[N];

bool isFriend(int s, int t){
  for(int i=0;i<n;i++) color[i] = 0;
  queue<int> q;
  int x;
  q.push(s);
  while(!q.empty()){
    x = q.front(); q.pop();
    for(int i=0;i<v[x].size();i++){
      int y = v[x][i];
      if(y == t) return true;
      if(color[y] == 0){
        q.push(y);
        color[y] = 1;
      }
    }
  }
  return false;
}

int main(){
  int m;
  cin >> n >> m;
  int s, t;
  for(int i=0;i<m;i++){
    cin >> s >> t;
      v[s].push_back(t);
      v[t].push_back(s);
  }
  int qn; cin >> qn;
  for(int i=0;i<qn;i++){
    cin >> s >> t;
    if(isFriend(s, t)) cout << "yes" << endl;
    else cout << "no" << endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
using namespace std;
vector<int> neighbors[100005];
//why choose vector, not matrix
int root[100005];

void dfs(int i, int start){
	root[i] = start;
	//visut all i's neighbors
	//c++ 11
	for(int j : neighbors[i]){
		if(root[j]==-1){
			dfs(j,start);
		}
	}
}

int main(){
	int n,m,x,y;
	memset(root, -1, sizeof(root));
	scanf("%d %d",&n,&m);
	for(int i = 0; i<m; i++){
		scanf("%d %d",&x,&y)
		neighbors[x].push_back(y);
		neighbors[y].push_back(x);
	}
	for(int i = 0; i<n; i++){
		if(root[x]= -1){
			dfs(i,i);
		}
	}
	scanf("%d", &q);
	for(int i = 0; i<q; i++){
		scanf("%d %d",&x, &y);
		if(root[x] = root[y]){
			printf("Yes\n");
		}else printf("no\n");
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define NIL -1
#define MAX 100000

ll conv(string num) { stringstream ss; ss << num << flush; ll n; ss>>n; return n; }

string conv(ll n) { stringstream ss; ss << n << flush; return ss.str(); }

vector<string> split(string s, string delim) {
    vector<string> res;
    int pos = 0;
    while(true) {
        int found = s.find(delim, pos);
        if(found >= 0) {
            res.push_back(s.substr(pos, found - pos));
        }
        else {
            res.push_back(s.substr(pos));
            break;
        }
        pos = found + delim.size();
    }
    return res;
}
int ALPH_SIZE = 26;
char ALPH_L[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char ALPH_U[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

int m, n;
vector<int> G[MAX];
int color[MAX];

void dfs(int r, int c) {
    stack<int> s;
    s.push(r);
    color[r] = c; // set color to the init point 

    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;  // set same color to connected points
                s.push(v);
            }

        }
    }
}


void assignColor() {
    int id = 1;

    for(int i=0; i<MAX; i++) {
        color[i] = NIL;
    }

    for(int i=0; i<MAX; i++) {
        if (color[i] == NIL) {
            dfs(i, id++);
        }
    }
}



int main() {
    int v, w, m, n, s, t;
    cin >> n >> m;
    for(int i=0; i<m; i++) {
        cin >> v >> w;
        G[v].push_back(w);
        G[w].push_back(v);
    }

    assignColor();

    int q; 

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