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;

bool dfs(int node, int gole, vector<vector<int> >& adjlist, vector<bool>& used){

    if(node == gole) return true;

    for(int i = 0; i < adjlist[node].size(); i++){
        if(used[adjlist[node][i]] == false){
            used[adjlist[node][i]] = true;
            bool flag = dfs(adjlist[node][i], gole, adjlist, used);
            if(flag) return true;
        }
    }

    return false;
}

int main(){

    int n, m; cin >> n >> m;

    vector<vector<int> > adjlist(n);               

    //入力
    for(int i = 0; i < m; i++){
        int a, b; cin >> a >> b;                   // a ←→ b に辺がある
        adjlist[a].push_back(b);
        adjlist[b].push_back(a);
        
    }

    //質問
    int q; cin >> q;
    for(int i = 0; i < q; i++){
        int a, b; cin >> a >> b;
        
        vector<bool> used(n, false);
        used[a] = true;
        if(dfs(a, b, adjlist, used)) cout << "yes" << endl;
        else cout << "no" << endl;
    }

    return 0;
}

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

int main(){
	int n,m,q,i,j;
	int s,t;
	vector<int> L(100005,-1);
	cin >> n >> m;
	
	for (j=0;j<n;j++){
		L[j]=j;
	}
	
	for (i=0;i<m;i++){
		cin >> s >> t ;
		if (L[s]!=L[t]){
			int a=L[t];
			for (j=0;j<n;j++){
				if (L[j]==a){
					L[j]=L[s];
				}
			}
		}
	}
	
	cin >> q;
	for (i=0;i<q;i++){
		cin >> s >> t;
		if (L[s]==L[t]){
			cout << "yes" <<endl;
		}else{
			cout << "no" <<endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <sstream>
#include <string>
#include <utility>
#include <map>
#include <memory>
#include <set>
#include <vector>
#include <deque>
#include <list>
#include <stack>
#include <queue>

using namespace std;

class Friend
{
public:
  int solve();
  void setGroup( vector<vector<int>>& adj, vector<int>& group, int u, int grpid );
};

void Friend::setGroup( vector<vector<int>>& adj, vector<int>& group, int u, int grpid )
{
  if ( group[u] ) return;
  
  group[u] = grpid;
  
  int n = adj[u].size();
  
  for ( auto i = 0; i < n; ++i )
    {
      int v = adj[u][i];
      setGroup( adj, group, v, grpid );
    }

  return;
}

int Friend::solve()
{
  int n, m;
  cin >> n >> m;
  
  vector<vector<int>> adj( n );

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

  int grpid = 1;

  vector<int> group( n, 0 );

  for ( auto i = 0; i < n; ++i )
    {
      if ( group[i] == 0)
        {
          setGroup( adj, group, i, grpid++ );
        }
      cout << i << ":" << group[i] << endl;
    }
  
  int q;
  cin >> q;

  
  for ( auto i = 0; i < q; ++i )
    {
      int u, v;
      cin >> u >> v;
      
      if ( group[u] == group[v] )
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  
  return 0;
}

int main()
{
  Friend f;

  f.solve();

  return 0;
}

C++: #include <stdio.h>
#include <stack>
using namespace std;
int G[1000][1000];
int vis[100000];
int n;

bool dfs(int s, int t)
{
    if (G[s][t] == 1) return true;
    vis[s] = 1;
    for (int i = 0; i < n; i++)
    {
        if (G[s][i] == 1 && vis[i] == 0) return dfs(i,t); 
    } 
    return false;
}

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

C++: #include <stdio.h>
#include <stdint.h>
#include <iostream>
#include <fstream>
#include <cassert>
#include <climits>

#include <cstring>
#include <queue>
using namespace std;

#define DEBUG (0)

#if DEBUG
#define DPRINTF(...) do { \
    printf("D(L%d) %s: ", __LINE__, __func__); \
    printf(__VA_ARGS__); \
  } while (false)
#else
#define DPRINTF(...)
#endif // DEBUG


const int kInvalid = -1;
const int kMaxUsers = 100 * 1000 + 1;

int g_group[kMaxUsers]; // 400 KiB

// 100 K * sizeof(vector<int>) + 100 K * sizeof(int)
// = 2.4 MiB + 100 K * sizeof(int)
vector<int> g_list[kMaxUsers];

void setRelations(int start, int end)
{
  g_list[start].push_back(end);
  g_list[end].push_back(start);
}


void doGrouptize()
{
  // debug info
  DPRINTF("size of g_group[] %lu Byte\n", sizeof(g_group));
  DPRINTF("size of g_list[]  %lu Byte\n", sizeof(g_list));

  // init
  int groupId = 0;
  memset(g_group, kInvalid, kMaxUsers);

  for (int i = 0; i < kMaxUsers; i++)
  {
    if (g_group[i] != kInvalid)
      continue;

    g_group[i] = groupId;

    // grouptize connected users in BFS order
    queue<int> que;
    que.push(i);

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

      for (int j = 0; j < g_list[current].size(); j++)
      {
        const int next = g_list[current][j];

        if (g_group[next] != kInvalid)
          continue;

        g_group[next] = groupId;
        que.push(next);
      }
    }

    groupId++;
  }
}


void printAllRelations()
{
#if DEBUG
  for (int i = 0; i < kMaxUsers; i++)
  {
    for (int j = 0; j < g_list[i].size(); j++)
    {
      if (j == 0)
        DPRINTF("%d", i);

      printf(" %d", g_list[i][j]);

      if (j == g_list[i].size() - 1)
        cout << endl;
    }
  }
#endif // DEBUG
}


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

  DPRINTF("number of users %d\n", n);
  DPRINTF("number of relations %d\n", m);

  for (int i = 0; i < m; i++)
  {
    int s, t;
    cin >> s >> t; cin.ignore();

    setRelations(s, t);
  }

  printAllRelations();

  doGrouptize();

  int q;
  cin >> q; cin.ignore();

  for (int i = 0; i < q; i++)
  {
    int s, t;
    cin >> s >> t; cin.ignore();

    DPRINTF("src %d  dst %d\n", s, t);

    if (g_group[s] == g_group[t])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

  return 0;

}