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>
using namespace std;
  
int main() {
int d[100100];
int m,n,q;
cin>>n>>m;
for(int i=0;i<n;i++){
    d[i]=i;
}
for(int l=0;l<m;l++){
    int a,b;
    cin>>a>>b;
    int j ,h;
    j=a,h=b;
while(d[j]!=j && d[h]!=h){
    j=d[j];
    h=d[h];
}
if(d[j]==j){
	while(d[h]!=h){
		h=d[h];
	}
    d[j]=h;
    d[a]=h;
d[b]=h;
}
else{
	while(d[j]!=j){
		j=d[j];
	}
  d[h]=j;
    d[b]=j;
d[a]=h;
}
}
cin>>q;
for(int y=0;y<q;y++){
    int a,b;
    cin>>a>>b;
        int j ,h;
    j=a,h=b;
while(d[j]!=j || d[h]!=h){
    j=d[j];
    h=d[h];
}
    if(j==h){
        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 <cstdio>
#include <cstdlib>
#include <cstring>

class queue{
    int head;
    int tail;
    int size;
    int *q;
public:
    queue(int n){
        head=0;
        tail=0;
        size=n;
        q = (int *)malloc(sizeof(int)*size);
    }
    void push(int n){
        if(tail-head>=size){
            return;
        }
//        printf("%d in.\n",n);
        q[tail%size]=n;
        tail++;
    }
    int pop(){
        if(tail<=head){
            return -1;
        }
        head++;
        return q[(head-1)%size];
    }
};

bool isFriend(int s, int t,int n,char **f,int *kakunin,int c){
    bool hoge;
    int tmp;
    queue ob(n);
    ob.push(s);
    kakunin[s]=c;
    for(int i=0;i<n;i++){
        hoge = true;
        while(true){
            tmp = ob.pop();
            if(tmp==-1)return false;
            for(int j=0;j<n;j++){
                if(f[tmp][j]&&(kakunin[j]<c)){
                    ob.push(j);
                    kakunin[j] = c;
                    f[s][j] = 1;
                    hoge = false;
                    if(j==t)return true;
                }
            }
        }
        if(hoge)return false;
    }
    return false;
}

int main(){
    int i,j,k,s,t,q,n,m;

    scanf("%d %d",&n,&m);

    char **f = (char **)malloc(sizeof(char *)*n);
    int *kakunin = (int *)malloc(sizeof(int)*n);
    memset(kakunin,0,sizeof(int)*n);

    for(i=0;i<n;i++){
        f[i] = (char *)malloc(sizeof(char)*n);
        memset(f[i],0,sizeof(char)*n);
    }

    for(i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        f[s][t] = 1;
        f[t][s] = 1;
    }

    scanf("%d",&q);
    for(i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        if(isFriend(s,t,n,f,kakunin,i+1))printf("yes\n");
        else printf("no\n");
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
using namespace std;
#define MAX 100000
#define 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 <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define erep(i,a,b) for(int i=a;i<=(int)(b);++i)
#define per(i,a,b) for(int i=(b);i>(a);--i)
#define eper(i,a,b) for(int i=((int)(a));i>=b;--i)
#define pb push_back
#define mp make_pair
#define INF 2000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a, b)*b;}

template <typename T> class union_find {
  vector<T> par, rank;

public:
  union_find(size_t n) {
    par.reserve(n + 1);
    rank.reserve(n + 1);
  }

public:
  void init(T n) { // add a set
    rep(i, 0, n) {
      par[i] = i;
      rank[i] = 0;
    }
  }

public:
  T find(T x) { // find the parent of x
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }

public:
  void unite(T x, T y) { // unite x and y
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y])
      par[x] = y;
    else
      par[y] = x;
    if (rank[x] == rank[y])
      rank[x]++;
  }

public:
  bool same(T x, T y) { // compare x's parent and y's parent
    return find(x) == find(y);
  }
};

int n, m;
vector<int> s, t;
int q;  
int main() {
 ios::sync_with_stdio ( false );
 cin.tie ( 0 );
    cin >> n >> m;
    union_find<int> uf = union_find<int>(n+1);
    uf.init(n);
    rep(i, 0, m) {
        int a, b;
        cin >> a >> b;
        s.pb(a); t.pb(b);
        uf.unite(a, b);
    }
    cin >> q;
    rep(i, 0, q) {
        int k, l; cin >> k >> l;
        if (uf.same(k, l)) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}