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<cstdio>
#include<vector>
#include<queue>
#define Lens 1005
#define Nil -1

using namespace std;
int n, m, q;
queue<int> Q;
vector<int> A[Lens];
int c[Lens], d[Lens];
int way[Lens][Lens];

void bns(int k, int v)
{
	Q.push(k);
	c[k] = v;
	while (!Q.empty())
	{
		k = Q.front();
		Q.pop();
		c[k] = v;
		for (int i = 0; i < A[k].size(); i++)
		{
			int tmp = A[k][i];
			if (c[tmp] == Nil)
			{
				Q.push(tmp);
			}
		}
	}
}

int main()
{
	int v = 1;
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int s, t;
		cin >> s >> t;
		c[s] = c[t] = Nil;
		A[s].push_back(t);
		A[t].push_back(s);
	}
	for (int i = 0; i < n; i++)
	{
		if (c[i] == Nil)
		{
			bns(i, v);
			v++;
		}

	}
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		int s, t;
		cin >> s >> t;

		if (c[s] == c[t]&&c[s]!=Nil&&c[s]!=0)
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}
	return 0;
}

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

using namespace std;
struct user {
    user() {};
    user(int i) : id {i}, fs {}, group {-1} {};
    int id;
    vector<int> fs;
    int group;
};

void add_friends(user *us, int x, int y){
    us[x].fs.push_back(y);
    us[y].fs.push_back(x);
}

void dfs(user* us, int id, int g){
    if(us[id].group == g){
        return;
    }
    us[id].group = g;
    vector<int> fs = us[id].fs;
    for(auto x : fs){
        dfs(us, x, g);
    }
}

void make_group(user* us, int n){
    int g = 0;
    for(int i = 0; i < n; ++i){
        if(us[i].group < 0){
            dfs(us, i, g);
            ++g;
        }
    }
}

int main(){
    int n, m;
    cin >> n >> m;
    user us[n];
    for(int i = 0; i < n; ++i){
        us[i] = user(i);
    }
    for(int i = 0; i < m; ++i){
        int a, b;
        cin >> a >> b;
        add_friends(us, a, b);
    }
    make_group(us, n);
    int q;
    cin >> q;
    for(int i = 0; i < q; ++ i){
        int a, b;
        cin >> a >> b;
        if(us[a].group == us[b].group){
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
}

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

int main(){
	int n,m,q,i,j;
	int s,t;
	vector<int> L(100005,-1);
	cin >> n >> m;
	
	for (j=0;j<n;j++){
		L[j]=j;
	}
	
	for (i=0;i<m;i++){
		cin >> s >> t ;
		if (L[s]!=L[t]){
			int a=L[t];
			for (j=0;j<n;j++){
				if (L[j]==a){
					L[j]=L[s];
				}
			}
		}
	}
	
	cin >> q;
	for (i=0;i<q;i++){
		cin >> s >> t;
		if (L[s]==L[t]){
			cout << "yes" <<endl;
		}else{
			cout << "no" <<endl;
		}
	}
	
	for (i=0;i<n;i++){
		cout << L[i] << endl;
	} 
	
	return 0;
}

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

vector<int> *v;
int *color;
int n;

void dfs(int i, int c){
        if(color[i]==c) return;
        color[i]=c;
        for(int j=0;j<v[i].size();j++)
                dfs(v[i][j], c);
}

void assignColor(){
        int c = 1;
        for(int i=0;i<n;i++){
                if(color[i]!=0) continue;
                dfs(i,c);
                c++;
        }

}

int main(){
        int m,q,a,b;
        cin>>n>>m;
        v = new vector<int>[n];
        color = new int[n];
        for(int i=0;i<m;i++){
                cin>>a>>b;
                v[a].emplace_back(b);
                v[b].emplace_back(a);
        }
        for(int i=0;i<n;i++) color[i] = 0;

        assignColor();

        cin>>q;
        for(int i=0;i<q;i++){
                cin>>a>>b;
                if(color[a]==color[b]) cout<<"yes"<<endl;
                else cout<<"no"<<endl;
        }

        return 0;
}

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

vector<int> g[100000];
int c[100000],n;


void dfs(int r,int a){
  stack<int> s;
  s.push(r);
  c[r]=a;
  while(!s.empty()){
    int u=s.top();
    s.pop();
    for(int i=0;i<g[u].size();i++){
      int v=g[u][i];
      if(c[v]==-1){
	c[v]=a;
	s.push(v);
      }
    }
  }
}

void color(){
  int id=1;
  for(int i=0;i<n;i++) c[i]=-1;
  for(int i=0;i<n;i++){
    if(c[i]==-1) dfs(i,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);
  }
  color();
  cin>>q;
  for(int i=0;i<q;i++){
    cin>>s>>t;
    if(c[s]==c[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; }

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

int uni[100000];

int oya(int a){
if (uni[a]<0){
	return a;
}
else{
	uni[a]=oya(uni[a]);
	return oya(uni[a]);
}
}

bool ren(int a,int b){
	a=oya(a);
	b=oya(b);
	if(a==b){
	return	false;
	}
	else{
		
	return true;	
	}
}

int main() {

int n,m;
cin>>n>>m;
for(int y=0;y<n;y++){
	uni[y]=-1;
}

for(int r=0;r<m;r++){
int s,g;
cin>>s>>g;
if(ren(s,g)){
	s=oya(s);
	uni[s]=g;
	}
}
int q;
cin>>q;
for(int rr=0;rr<q;rr++){
	int s,g;
	cin>>s>>g;
	if(ren(s,g)==true){
		cout<<"NO"<<endl;
	}
	else{
		cout<<"YES"<<endl;
	}
}
	return 0;
}

C++: #include <iostream>
#include <vector>
#define eol '\n'
using namespace std;

vector<int> visit;

bool bfs(int s, int t, vector<vector<int>> list) {
  if (s == t) {
    return true;
  }
  
  // visit flag
  visit[s] = 1;
  
  for (int i = 0; i < list[s].size(); i++) {
    int next = list[s][i];
    if (visit[next] == 0) {
      if (bfs(next, t, list)) {
        return true;
      }
    }
  }
  return false;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n,m,s,t,q;
  cin >> n >> m;

  visit.resize(n,0);
  vector<vector<int>> list(n, {});
  
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    list[s].push_back(t);
    list[t].push_back(s);
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;

    if (bfs(s,t,list)) {
      cout << "yes" << eol;
    } else {
      cout << "no" << eol;
    }
  }
  return 0;
}