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<bits/stdc++.h>
#define REP(i,n) for(long long i=0;i<n;++i)
#define FOR(i,a,b) for(long long i=a;i<b;++i)
using namespace std;
const int MAX=1e5+1;
const int NIL=-1;
int n,color[MAX];
vector< int > graph[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();
        REP(i,graph[u].size()){
            int v=graph[u][i];
            if(color[v]==NIL){
                color[v]=c;
                S.push(v);
            }
        }
    }
}
int main(){
    int s,t,m,q;
    scanf("%d%d",&n,&m);
    REP(i,m){
        scanf("%d%d",&s,&t);
        graph[s].push_back(t);
        graph[t].push_back(s);
    }
    int id=1;
    fill(color,color+MAX-1,NIL);
    REP(u,n){
        if(color[u]==NIL) dfs(u,id++);
    }
    scanf("%d",&q);
    REP(i,q){
        scanf("%d%d",&s,&t);
        printf("%s\n",color[s]==color[t]?"yes":"no");
    }
    return 0;
}

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[d[a]]=h;
    d[a]=h;
d[b]=h;
}
else{
	while(d[j]!=j){
		j=d[j];
	}
  d[h]=j;
    d[b]=j;
d[a]=j;
}
}
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>
#define rep(i,n) for(int (i) = 0;(i) < (n);(i)++)
#define SORT(c) sort((c).begin(),(c).end())
#define MOD 1000000007
using namespace std;
typedef long long ll;

struct UnionFind {
    vector<int> data;
    int sz;
    UnionFind(int sz) : data(sz, -1), sz(sz) { }
    bool unionSet(int x, int y) {
        if ((x = root(x)) == (y = root(y))) return false;
        if (data[x] > data[y]) swap(x, y);
        data[x] += data[y]; data[y] = x;
        sz--;
        return true;
    }
    bool findSet(int x, int y) { return root(x) == root(y); }
    int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
    int size(int x) { return -data[root(x)]; }
    int size() {return sz;}
};

int main(void){
  int n, m;
  cin >> n >> m;

  UnionFind uf(n);
  rep(i, m){
    int s, t;
    cin >> s >> t;
    uf.unionSet(s, t);
  }

  vector<string> ans;
  int q;
  cin >> q;
  rep(i, q){
    int a, b;
    cin >> a >> b;
    if(uf.findSet(a, b)) ans.push_back("yes");
    else ans.push_back("no");
  }
  rep(i, ans.size()) cout << ans[i] << endl;
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxx=100010;

int pre[maxx];

int find(int u){
	if(pre[u]!=u) pre[u]=find(pre[u]);
	return pre[u];
}

int main(){
	int n,m,x,y;
	cin>>n>>m;
	for(int i=0;i<n;i++) pre[i]=i;
	while(m--){
		cin>>x>>y;
		pre[find(x)]=find(y);
	}
	cin>>m;
	while(m--){
		cin>>x>>y;
		if(find(x)==find(y)) 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>
#include<vector>
#include<stack>
using namespace std;
static const int MAX = 100000;
static const int NTL = -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]==NTL){
      color[v]=c;
      S.push;
      }
    }
  }
}

void assigncolor() {
  int id=1;
  for(int i=0;i<n;i++) color[i]=NTL;
  for(int u =0;u<n;u++){
    if(color[u]==NTL)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 <map>
#include <queue>
using namespace std;
int main(){
  int n,m,t1,t2,c[100000]={}; map <int,bool> g[100000];
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>t1>>t2;g[t1][t2]=1;g[t2][t1]=1;
  }
  int l;cin>>l;
  for(int i=0;i<l;i++){
    queue <int> q;
    bool f[100000]={},find=false;
    cin>>t1>>t2;
    if(c[t1]&&c[t2]){
      if(c[t1]==c[t2]) cout<<"yes"<<endl;
      else cout<<"no"<<endl;
      continue;
    }
    q.push(t1);f[t1]=true;c[t1]=i+1;
    for(;!q.empty();){
      for(int j=0;j<n;j++) if(g[q.front()][j]&&!f[j]){
          if(j==t2) find=true;
          f[j]=true;c[t2]=i+1;
          q.push(j);
	}
      q.pop();
      if(find) break;
    }
    if(find) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
  return 0;
}