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

int n, m;

bool frend(vector<vector<int>> &M, int i, int t) {
  for (int z = i + 1; z < n; z++) {
    if (M[i][z] == 0) continue;
    if (M[i][z] == 1 && z == t) return true;
    if (frend(M, z, t)) {
      return true;
    } else {
      return false;
    }
  }
  return false;
}

int main() {
  cin >> n >> m;
  vector<vector<int>> M(n, vector<int>(n));

  for (int i = 0; i < m; i++) {
    int id, f;
    cin >> id >> f;
    M[id][f] = 1;
  }
  int qn;
  cin >> qn;
  vector<vector<int>> q(qn, vector<int>(2));
  for (int i = 0; i < qn; i++) {
    cin >> q[i][0] >> q[i][1];
  }

  for (int i = 0; i < qn; i++) {
    bool res = frend(M, q[i][0], q[i][1]);
    if (res) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

C++: #include <bits/stdc++.h>

using namespace std;
bool frag;

vector<vector<int>> makelist(int n, int m) {
	vector<vector<int>> Adjlist(n);
	int a, b;

	for(int i = 0; i < m; i++) {
		cin >> a >> b;
		Adjlist[a].push_back(b);
		Adjlist[b].push_back(a);
	}

	return Adjlist;
}

void dfs_run(int a, int b, vector<vector<int>>& adj, vector<bool>& checker) {
	//cout << a << endl;
	while(!adj[a].empty()) {
		int tmp;
		tmp = adj[a].back();
		adj[a].pop_back();	
		if(checker[tmp] == false) {
			continue;
		}
		checker[tmp] = false;
		if(tmp == b) {
			frag = true;
		}else{
			if(frag == false) {
				dfs_run(tmp, b, adj, checker);
			}
		}
	}
}

bool dfs(int n, int a, int b, vector<vector<int>> adj) {
	vector<bool> checker(n, true);

	checker[a] = false;
	dfs_run(a, b, adj, checker);
	return frag;
}

int main(void)
{
	int n, m, q;

	cin >> n >> m;
	vector<vector<int>> Adjlist = makelist(n, m);

	cin >> q;
	for(int i = 0; i < q; i++) {
		int a, b;
		cin >> a >> b;

		frag = false;
		if(dfs(n, a, b, Adjlist)) {
			cout << "yes" << endl;
		}else{
			cout << "no" << endl;
		}
	}

	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<stack>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#define white 0
#define grey 1
#define nil -1
#define maxn 100000
using namespace std;

vector<int> g[maxn+5];
int color[maxn+5];
int n,m;

void dfs(int s,int t)
{
    stack<int>q;
    q.push(s);
    color[r]=t;
    while(!q.empty())
    {
        int v=q.top();
        q.pop();
        for(int j=0;j<g[v].size();j++)
        {
            int u=g[v][j];
            if(color[u]==nil)
            {
                q.push(u);
                color[u]=t;
            }
        }
    }
}

void work()
{
    int id=1;
    for(int i=0;i<n;i++)
        color[i]=nil;
    for(int i=0;i<n;i++)
        if(color[i]==nil)
           dfs(i,id++);
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        g[x].push_back(y);
        g[y].push_back(x);
    }
    work();

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

C++: #include <bits/stdc++.h>

using namespace std;

#define ll long long
typedef complex<double> P;

int main() {
  int n, m; cin >> n >> m;
  vector<int> adj[n];

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

  int sd[n] = {1};
  queue<int> que;
  que.push(0);
  int c = 1;

  while (!que.empty()) {
    int v = que.front(); que.pop();

    for (int i = 0; i < adj[v].size(); i++) {
      if(!sd[adj[v][i]]) {
        que.push(adj[v][i]);
        sd[adj[v][i]] = c;
      }
    }

    if(que.empty()) {
      for (int i = 0; i < n; i++) {
        if(!sd[i]) {
          sd[i] = ++c;
          que.push(i);
          break;
        }
      }
    }

  }

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

C++: #include <iostream>
#include <vector>
using namespace std;
vector<int> G[100];
vector<int> A;
int isVisited[100];

void setupVisited(int n)
{
    for (int i = 0; i < n; i++) {
	isVisited[i] = 0;
    }
}
bool enableConnection(int l, int o)
{
    for (int i = 0; i < G[l].size(); i++) {
	if (G[l][i] == o) {
	    return true;
	}
	if (isVisited[l] == 1) {
	    return false;
	}
	isVisited[l] = 1;
	if (enableConnection(G[l][i], o) || enableConnection(o, G[l][i])) {
	    return true;
	}
    }
    return false;
}

int main()
{
    int a, c;
    cin >> a;
    cin >> c;
    //setup
    for (int i = 0; i < c; i++) {
	int n;
	int m;
	cin >> n >> m;
	G[n].push_back(m);
	G[m].push_back(n);
	isVisited[i] = 0;
    }

    //then
    int b = 0;
    cin >> b;
    for (int i = 0; i < b; i++) {
	int l;
	int o;
	cin >> l;
	cin >> o;
	setupVisited(c);
	if (enableConnection(l, o) || enableConnection(o, l)) {
	    cout << "yes" << endl;
	} else {
	    cout << "no" << endl;
	}
    }
    return 0;
}

C++: #include <stdio.h>
#define rep(i, n) for(int i = 0; i < n; ++i)
 
int main(void) {
  int n, m;
  scanf("%d%d", &n, &m);
  int uf[n];
  rep(i, n) uf[i] = i;
  rep(i, m) {
    int s, t, nows, nowt;
    scanf("%d%d", &s, &t);
    nows = s, nowt = t;
    while(uf[nows] != nows) {
      nows = uf[nows];
    }
    while(uf[nowt] != nowt) {
      nowt = uf[nowt];
    }
    uf[nowt] = nows;
  }
  rep(i, n) {
    int nows = i;
    while(uf[nows] != nows) {
      nows = uf[nows];
    }
    uf[i] = nows;
  }
  /*rep(i, n) printf("%d ", uf[i]);
  printf("\n");*/
  int q;
  scanf("%d", &q);
  rep(i, q) {
    int s, t;
    scanf("%d%d", &s, &t);
    if(uf[s] != uf[t]) printf("no\n");
    else printf("yes\n");
  }
  return 0;
}