Go: package main

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

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

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