Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

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

int n,m,q,s,t;
int tree[100002];
int sn[100002];
list<int>l[100002];

int slv(int x,int id);

int main(){
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>s>>t;
    l[s].push_back(t);
    l[t].push_back(s);
  }

  for(int i=0;i<n;i++){
    tree[i]=-1;
    sn[i]=0;
  }
  int id=1;
  for(int i=0;i<n;i++){
    if(tree[i]==-1){
      slv(i,id);
      id++;
    }
  }

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

  return(0);
}

int slv(int x,int id){
  tree[x]=id;
  sn[x]=1;
  for(int i=0;i<l[x].size();i++){
    int y=l[x].front();
    l[x].pop_front();
    l[x].push_back(y);
    if(sn[y]==0){
      slv(y,id);
    }
  }
  return(0);
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int grey=1;
const int white=0;
const int black=-1;

const int maxn=100005;
int head[maxn+5],pos=0;
int color[maxn+5],id[maxn+5];
int n,m;

struct node
{
    int k;
    int next;
};
struct node G[2*maxn+5];

void add(int u,int v)
{
    G[++pos].k=v;
    G[pos].next=head[u];
    head[u]=pos;
}

void bfsvisit(int u,int k)
{
    color[u]=black;
    queue<int> q;
    q.push(u);
    id[u]=k;
    while(!q.empty())
    {
        int r=q.front();
        q.pop();
        for(int j=head[r];j;j=G[j].next)
        {
            int v=G[j].k;
            if(color[v]==white)
            {
                q.push(v);
                color[v]=black;
                id[v]=k;
            }
        }
    }
}

void bfs()
{
    memset(id,0,sizeof(id));
    memset(color,0,sizeof(color));
    int k=0;

    for(int i=0;i<n;i++)
    {
        if(color[i]==white)
        {
            bfsvisit(i,k++);
        }
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v);
        add(v,u);
    }

    bfs();
    int q,s,t;
    scanf("%d",&q);
    while(q--)
    {
        scanf("%d%d",&s,&t);
        if(id[s]==id[t])
            printf("yes\n");
        else printf("no\n");
    }
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX=100000;
static const int NIL=-1;

int n,color[MAX];
vector<int>G[MAX];

void dfs(int r,int c){
  stack<int> S;
  int u,i,v;
  
  S.push(r);
  color[r]=c;

  
  while(!S.empty()){
    u=S.top();
    S.pop();
    for(i=0;i<G[u].size();i++){
      v=G[u][i];
      if(color[v]==NIL){
	color[v]=c;
	S.push(v);
      }
    }
  }
}

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

int main(){
  int s,t,m,q;
  int i;
  cin>>n>>m;
  for(i=0;i<m;i++){
    cin>>s>>t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  assignColor();

  cin>>q;

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