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 <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

static const int MAX =100000;
static const int NIL = -1;

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

/**
 * @param int r node
 * @param int c color_id
 */
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, G[u].size()) {
         int v = G[u][i];
         if(color[v] == NIL) {
            color[v] = c;
            S.push(v);
         }
      }
   }
}

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

int main(){
   int s, t, m, q;
   cin>>n>>m;

   rep(i, m) {
      cin >> s >> t;
      G[s].push_back(t);
      G[t].push_back(s);
   }

   assignColor();

   cin >> q;
   rep(i, q) {
      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;
static const int MAX=100000;
static const int 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);
      }
    }
  }
}

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);
  }
  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++);
  }
  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<vector>
using namespace std;

bool is_friend(const vector<vector<int> >& u, int s, int t, vector<int>& searched){
    if (s == t) return true;
    for (int i = 0; i < u[s].size(); i++) {
        if (!searched[u[s][i]]) {
            searched[u[s][i]] = 1;
            return is_friend(u, u[s][i], t, searched);
        }
    }
    return false;
}
int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<int> > u(n);
    for (int i = 0; i < m; i++) {
        int s, t;
        cin >> s >> t;
        u[s].push_back(t);
        u[t].push_back(s);
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int s, t;
        cin >> s >> t;
        vector<int> searched(n, 0);
        searched[s] = 1;
        if (is_friend(u, s, t, searched)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}

C++: #include<cstdio>
#include<stack>
#include<vector>
using namespace std;
const int big=100000;
const int nil=-1;
int n;
vector<int> tu[big];
int color[big];
void dfs(int r,int c)
{
    stack<int>a;
    a.push(r);
    color[r]=c;
    while(!a.empty())
    {
        int u=a.top();
        a.pop();
        for (int i=0;i<tu[u].size();i++)
        {
            int v=tu[u][i];
            if(color[v]==nil)
            {
                color[v]=c;
                a.push(v);
            }
        }
    }
}
void yanse()
{
    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 mian()
{
    int s,t,m,q;
    scanf("%d%d",&m,&n);
    for (int i=0;i<m;i++)
    {
        scanf("%d%d",&n,&m);
        tu[s].push_back(t);
        tu[t].push_back(s);
    }
    yanse();
    scanf("%d",&q);
    for (int i=0;i<q;i++)
    {
        scanf("%d%d",&s,&t);
        if(color[s]==color[t])
        {
            printf("yes\n");
        }
        else
        {
            printf("no\n");
        }
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;

bool DFS(const vector<vector<int> >& G, int a, int b, int i) {
    if (a == b) return true;
    if (i >= G[a].size()) return false;
    return DFS(G, G[a][i], b, 0) || DFS(G, a, b, i + 1);
}

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

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

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        vector<vector<bool> > visited(false);
        if (DFS(G, a, b, 0) || DFS(G, b, a, 0)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}