Go: package main

import (
	"bufio"
	"container/list"
	"os"
	"strconv"
	"fmt"
)

const (
	MAX = 100000
	NIL = -1
)

var (
	n     int
	G     [MAX][]int
	color [MAX]int
)

func DFS(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

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

func main() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}

	assignColor()
	scanner.Scan()
	q, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < q; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

C++: #include <iostream>
#include <queue>
#include <cstdio>
#include <vector>
using namespace std;
#define MAX_N 100000
int n;

bool can_connect(int s,int t,const vector< vector<int> > &g) {
  bool visited[n];
  queue<int> que;
  for(int i=0;i<n;i++) visited[i] = false;
  while(!que.empty()) que.pop();

  visited[s] = true;
  que.push(s);
  while(!que.empty()) {
    int v = que.front(); que.pop();
    for(int i=0;i<g[v].size();i++) {
      if(!visited[g[v][i]]) {
        if(g[v][i] == t) return true;
        visited[g[v][i]] = true;
        que.push(g[v][i]);
      }
    }
  }
  return false;
}

int main() {
  int m,s,t,q;
  scanf("%d %d\n",&n,&m);
  vector< vector<int> > g(n);
  for(int i=0;i<m;i++) {
    scanf("%d %d",&s,&t);
    g[s].push_back(t);
    g[t].push_back(s);
  }
  /*
  for(int i=0;i<g.size();i++) {
    printf("%d : ",i);
    for(int j=0;j<g[i].size();j++) {
      printf("%d ",g[i][j]);
    }
    printf("\n");
  }
  */

  scanf("%d",&q);
  for(int i=0;i<q;i++) {
    scanf("%d %d",&s,&t);
    //printf("%d,%d: ",s,t);
    if(can_connect(s,t,g)){
      printf("yes\n");
    }
    else {
      printf("no\n");
    }
  }
}

C++: #include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
int main(){
    int n;
    int m;
    std::scanf("%d", &n);
    std::scanf("%d", &m);

    std::unordered_map<int, int> map;
    for(int i = 0; i < m; ++i){
        int s;
        int t;
        std::scanf("%d", &s);
        std::scanf("%d", &t);
        if(map.find(s) == map.end() && map.find(t) == map.end()){
            int small = std::min(s, t);
            int large = std::max(s, t);
            map.insert(std::make_pair(small, small));
            map.insert(std::make_pair(large, small));
        } else if(map.find(s) == map.end()){
            int key = map[t];
            while(key != map[key]){
                key = map[key];
            }
            if(key <= s){
                map.insert(std::make_pair(s, key));
            } else {
                map[key] = s;
                map.insert(std::make_pair(s, s));
            }
        } else if(map.find(t) == map.end()){
            int key = map[s];
            while(key != map[key]){
                key = map[key];
            }
            if(key <= t){
                map.insert(std::make_pair(t, key));
            } else {
                map[key] = t;
                map.insert(std::make_pair(t, t));
            }
        } else {
            int keys = map[s];
            while(keys != map[keys]){
                keys = map[keys];
            }
            int keyt = map[t];
            while(keyt != map[keyt]){
                keyt = map[keyt];
            }
            int small = std::min(keys, keyt);
            int large = std::max(keys, keyt);
            map[large] = small;
        }
    }

    int q;
    std::scanf("%d", &q);
    for(int i = 0; i < q; ++i){
        int s;
        int t;
        std::scanf("%d", &s);
        std::scanf("%d", &t);
        if(map.find(s) == map.end() || map.find(t) == map.end()){
            printf("no\n");
            continue;
        }
        int keys = map[s];
        while(keys != map[keys]){
            keys = map[keys];
        }
        int keyt = map[t];
        while(keyt != map[keyt]){
            keyt = map[keyt];
        }
        if(keys == keyt){
            std::printf("yes\n");
        } else {
            std::printf("no\n");
        }
    }

    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 <queue>
using namespace std;
struct Info{
        int s;
        int t;
};
int main(){
        int N,M;
        cin >> N >> M;
        int groups[100000]={-1};
        for(int i=0 ; i<=N ; i++ )
                groups[i]=-1;
        Info friends[100000];
        for(int i=0 ; i<M ; i++ )
                cin >> friends[i].s >> friends[i].t ;
        int group=0;
        for(int i=0 ; i<N ; i++ ){
                if(groups[i]!=-1)
                        continue;
                queue <int> cycle;
                cycle.push(i);
                while(cycle.size()!=0){
                        int now=cycle.front();cycle.pop();
                        for(int l=0 ; l<M ; l++ ){
                                if(now==friends[l].s && groups[friends[l].t]==-1)
                                        cycle.push(friends[l].t);
                        }
                        groups[now]=group;
                }
                group++;
        }
        cin >> N;
        for(int i=0 ; i<N ; i++ ){
                int s,t;
                cin >> s>> t;
                if(groups[s]==groups[t])
                        cout<<"yes"<<endl;
                else
                        cout<<"no"<<endl;
        }
        return 0;
}

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

int a[200000];
int search(int k){
  if(a[k]==k){return k;}
  int p=search(a[k]);
  a[k]=p;
  return p;
}




int main(){
  for(int i=0;i<200000;i++){
    a[i]=i;
  }
  int n,m;
  cin>>n>>m;
  for(int i=0;i<m;i++){
    int p,q;
    cin>>p>>q;
    a[search(p)]=search(q);
  }

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

  
  
  return 0;
}