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>
using namespace std;

bool search(vector<int> member,int x){
  for(int i=0; i<member.size(); i++){
    if(member[i] == x)
      return true;
  }
    return false;
}

int main(){
  int n,m,join[10000],s,t;
  vector<int> group[10000];
  cin >> n >> m;

  for(int i=0; i<n; i++)
    join[i] = i;
    
  for(int i=0; i<m; i++){
    cin >> s >> t;
    bool flag = search(group[join[t]],s);
    flag = search(group[join[s]],t);

    if(!flag){
      int x = join[t];
      group[join[s]].push_back(join[t]);
      
      for(int j=0; j<group[x].size(); j++){
	group[join[s]].push_back(group[join[t]][j]);
	join[group[join[t]][j]]=join[s];
      }
	  
      join[x] = join[s];
    }
  }
 
  int q;
  cin >> q;
  
  bool answer[10000];
  for(int i=0; i<q; i++){
    cin >> s >> t;
    
    if(join[s] == join[t])
      answer[i]=true;
    else
      answer[i]=false;
  }

  for(int i=0; i<q; i++){
    if(answer[i])
      cout << "yes\n";
    else
      cout << "no\n";
  }
}

C++: #include <cstdio>
#include <algorithm>
#define rep(i, n) for(int i=0; i<(n); ++i)

int parent[100005];

int find(int x){
   if( parent[x] == x )return x;
   return parent[x] = find( parent[x] );
}

void unite(int x, int y)
{
   x = find(x);
   y = find(y);

   if(x == y)return;
   parent[x] = y;
}

int main()
{
   int N, M;
   int x, y, tmp;
   scanf("%d%d", &N, &M);
   std::iota(parent, parent + N, 0);

   rep(i, M){
      scanf("%d%d", &x, &y);
      unite(x, y);
   }

   int Q;
   scanf("%d", &Q);
   while(Q--){
      scanf("%d%d", &x, &y);
      puts( find(x) == find(y) ? "yes" : "no" );
   }
}

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

void a(){
	while (!Q.empty()){
		Q.pop();
		for (int i = 0; i < G[s].size(); i++){
			if (G[s][i] == t){
				flag = true;
				while (!Q.empty()) Q.pop();
				return;
			}
			else {
				Q.push(G[s][i]);
			}
		}
		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); //??£???????????£?????????
	}
	cin >> q;
	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;
	}

	return 0;
}

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

    return 0;
}

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

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

  int id = 1;
  
  for ( int i = 0; i < n; ++i) {
    color[i] = NIL; //preset
  }
  
  for ( int u = 0; u < n; ++u) {
    if ( color[u] == NIL ) {
      dfs(u,id);
      id++;
    }
  }
  
  scanf("%d", &q);
  for ( int i = 0; i < q; ++i) {
    scanf("%d %d", &s, &t);
    if ( color[s] == color[t] ) {
      printf("yes\n");
    } else {
      printf("no\n");
    }
  }

  return (0);
}

C++: #include<iostream>
#include<unordered_map>
int main(){
  return 0;
}

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

using namespace std;

class UnionFind {
public:
	vector<int> par;

	UnionFind(int n) {
		par.resize(n);
		for (int i = 0; i < n; i++) par[i] = i;
	}

	UnionFind() {};

	int root(int x) {
		if (par[x] == x)return x;
		else return par[x] = root(par[x]);
	}

	void unite(int x, int y) {
		x = root(x);
		y = root(y);
		if (x == y)return;
		par[x] = y;
	}

	bool same(int x, int y) {
		return root(x) == root(y);
	}
};

int main() {
	int n, m, s, t, q;
	cin >> n >> m;
	UnionFind uf(n);
	while (m--) cin >> s >> t, uf.unite(s, t);
	cin >> q;
	while (q--)cin >> s >> t, cout << (uf.same(s, t) ? "yes" : "no") << endl;
}

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

vector<int> list[100000];
int n,m;

bool bfs(int s, int t){

  bool used[m];
  for(int i=0;i<m;i++) used[i]=false; //初期化

  queue<int> Q;
  Q.push(s); used[s]=true;

  while(!Q.empty()){
    int k=Q.front(); Q.pop();
    for(int i=0;i<list[k].size();i++){
      int temp=list[k][i];
      if(temp==t){return true;}
      if(!used[temp]){ //未訪問なら
        Q.push(temp); used[temp]=true;
      }
    }
  }
  return false;
}
int main(){

  cin>>n>>m;

  for(int i=0;i<m;i++){
    int u,v; cin>>u>>v;
    list[u].push_back(v);
    list[v].push_back(u);
  }

  int q; cin>>q;

  for(int i=0;i<q;i++){
    int s,t; cin>>s>>t;
    if(bfs(s,t)){
      cout<<"yes\n";
    }else{
      cout<<"no\n";
    }
  }

}