Go: package main

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

// Node 頂点
type Node struct {
	gid  int   // グループID
	edge []int // 隣接辺
}

// NoID グループIDなし
const NoID = -1

// NewNode 頂点の生成
func NewNode() *Node {
	return &Node{gid: NoID, edge: []int{}}
}

func parseNode(nodes []*Node, gid int, queue []int) {
	if len(queue) == 0 {
		return
	}
	var nextQ []int
	for _, id := range queue {
		node := nodes[id]
		if node.gid == NoID {
			node.gid = gid
			for _, edge := range node.edge {
				nextQ = append(nextQ, edge)
			}
		}
	}
	parseNode(nodes, gid, nextQ)
	return
}
func digNode(nodes []*Node, id, gid int) {
	node := nodes[id]
	if node.gid != NoID {
		// 探索済み
		return
	}
	node.gid = gid
	for _, edge := range node.edge {
		digNode(nodes, edge, gid)
	}
	return
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n, m := eGetInt2(scanner)

	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		nodes[i] = NewNode()
	}

	for i := 0; i < m; i++ {
		a, b := eGetInt2(scanner)
		nodes[a].edge = append(nodes[a].edge, b)
		nodes[b].edge = append(nodes[b].edge, a)
	}

	for i := 0; i < n; i++ {
		// 幅優先探索
		// gid := i
		// queue := []int{i}
		// parseNode(nodes, gid, queue)

		// 深さ優先探索
		digNode(nodes, i, i)
	}

	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		a, b := eGetInt2(scanner)
		if nodes[a].gid == nodes[b].gid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}
func eGetInt2(scanner *bufio.Scanner) (int, int) {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0], t[1]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: #include <iostream>
#include <vector>
#include <stack>

using namespace std;
using Relations = vector<vector<int>>;

const int no_group = -1;
const int no_user_id = -1;

void depth_first_search(Relations& list_graph, int started_user_id, vector<int>& connected_group, int group_id) {
  stack<int> visited_path;
  visited_path.push(started_user_id);
  connected_group[started_user_id] = group_id;

  while(!visited_path.empty()) {
    int current_user_id = visited_path.top();

    int unvisited_adjacency_user_id = no_user_id;
    for(int i = 0; i < list_graph[current_user_id].size(); i++) {
      int adjacency_user_id = list_graph[current_user_id][i];
      if(connected_group[adjacency_user_id] == no_group) {
        unvisited_adjacency_user_id = adjacency_user_id;
        break;
      }
    }

    if(unvisited_adjacency_user_id != no_user_id) {
      connected_group[unvisited_adjacency_user_id] = group_id;
      visited_path.push(unvisited_adjacency_user_id);
    } else {
      visited_path.pop();
    }
  }
}


int main() {
  int user_num;
  int relation_num;
  Relations list_graph;

  // Init
  cin >> user_num >> relation_num;
  list_graph.resize(user_num);

  // Construct list graph
  for(int i = 0; i < relation_num; i++) {
    int user1_id, user2_id;
    cin >> user1_id >> user2_id;
    list_graph[user1_id].push_back(user2_id);
    list_graph[user2_id].push_back(user1_id);
  }

  // Group connected component
  vector<int> connected_group(user_num, no_group);

  int group_id = 0;
  for(int i =  0; i < user_num; i++) {
    if(connected_group[i] == no_group) {
      depth_first_search(list_graph, i, connected_group, group_id++);
    }
  }


  // Answer queries
  int query_num;
  cin >> query_num;
  for(int i = 0; i < query_num; i++) {
    int user1_id, user2_id;
    cin >> user1_id >> user2_id;

    if(connected_group[user1_id] == connected_group[user2_id]) {
      cout << "yes";
    } else {
      cout << "no";
    }
    cout << endl;
  }
}

C++: #include<iostream>
#include<stack>
#include<vector>

using namespace std;

static const int MAX = 100000;

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

void dfs (int r, int c) {

    int i, u, v;

    stack<int> S;
    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] == -1) {
                color[v] = c;
                S.push(v);
            }
        }
    }

}

void assignColor () {

    int id = 1, i, u;

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

}

int main () { 

    int s, t, m, q, 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<cstdio>
#include<queue>

using namespace std;

static const int N = 100000;

int m,n;

queue<int> Q[N];
int color[N];

void visit(int a,int c){
    color[a] = c;
   while( !(Q[a].size() == 0) ){
        int u = Q[a].front();
        if(color[u] == 0) visit(u,c);
        Q[a].pop();
    }
}

int main(){
    scanf("%d",&n);
    scanf("%d",&m);

    for(int i=0; i<n; i++) color[i] = 0;

    for(int i=0; i<m;i++){
        int u,v;
        scanf("%d",&u);
        scanf("%d",&v);
        Q[u].push(v);
        Q[v].push(u);
    }
    
    int cc = 1;

    for(int i=0; i<n; i++){
        if(color[i] == 0){
            visit(i,cc);
            cc++;
        }
    }

    int l; 
    scanf("%d",&l);
    for(int i =0 ;i<l;i++){
        int x,y;
        scanf("%d",&x);
        scanf("%d",&y);

        if(color[x] == color[y]) printf("yes\n");
        else printf("no\n");

    }

}

C++: #include<iostream> 
#include <list> 
#define N 100001
using namespace std; 
int n, loop;
int color=0;

int flag[N];

bool *visited = new bool[N];

class Graph{ 
    int V; 
    list<int> *adj; 
  
public: 
    void DFSUtil(int v); 
    Graph(int V);  
    void addEdge(int v, int w); 
    void connectedComponents(); 
}; 
  
void Graph::connectedComponents() { 
    for(int v = 0; v < V; v++){
        visited[v] = false; 
	}
  
    for (int v=0; v<V; v++) { 
        if (visited[v] == false) {
        	color++;
            DFSUtil(v); 
        } 
    } 
}
  
void Graph::DFSUtil(int v) 
{ 
    visited[v] = true;
    flag[v]=color;
    list<int>::iterator i; 
    for(i = adj[v].begin(); i != adj[v].end(); ++i){
        if(!visited[*i]){
            DFSUtil(*i); 
		}
	}
} 
  
Graph::Graph(int V) { 
    this->V = V; 
    adj = new list<int>[V]; 
} 
  
void Graph::addEdge(int v, int w) { 
    adj[v].push_back(w); 
    adj[w].push_back(v); 
} 

int main() {
  	int i, x,y,uji,j;
  	cin>>n>>loop;
	Graph g(n);
	 
	
	
  	for(i=0; i<loop; i++){
		cin>>x>>y;
		g.addEdge(x,y);
	}
	
	for(j= 0; j < n; j++){
	   visited[j] = false; 
	   flag[j]=0;
	}
	g.connectedComponents();
	
	cin>>uji;
	int prev=-1;
	for(i=0; i<uji; i++){
		cin>>x>>y;
		
		if(flag[y]==flag[x]){
			cout<<"yes"<<endl;
		}else{
			cout<<"no"<<endl;
		}
		prev=x;
	}
    return 0; 
}