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 <cstdio>
#include <iostream>
#include <vector>

#define N 100000

using namespace std;

vector<int> G[N];
int color[N];

void dfs(int s,int c){
  color[s]=c;
  for(int i=0;i<G[s].size();i++){
    if(color[G[s][i]]!=c) dfs(G[s][i],c);
  }
}

int main(){
  int n,m;
  scanf("%d %d",&n,&m);
  for(int i=0;i<n;i++) color[i]=-1;
  for(int i=0;i<m;i++){
    int s,t;
    scanf("%d %d",&s,&t);
    G[s].push_back(t);
    G[t].push_back(s);
  }
  int q; scanf("%d",&q);
  for(int i=0;i<q;i++){
    int s,t;
    scanf("%d %d",&s,&t);
    dfs(s,i);
    if(color[s]==color[t]) cout << "yes\n";
    else cout << "no\n";
  }
}

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

const int maxm = 100000 + 5;
const int maxn = 100000 + 5;

struct Edge{
  int to,next;
} G[maxm<<1];

int head[maxn],cnt = 0;
int color[maxn];
bool vis[maxn];

void Insert(int u,int v){
  G[++cnt].next = head[u];
  G[cnt].to = v;
  head[u] = cnt;
}

void Dfs(int u,int c){
  vis[u] = true;
  color[u] = c;
  for (int e = head[u];e; e = G[e].next) {
    int v = G[e].to;
    if (!vis[v]) {
      Dfs(v,c);
    }
  }
}

int main(){
  int n,m;
  scanf("%d %d",&n,&m);
  for (int i=1;i<=m; i++) {
    int u,v;
    scanf("%d %d",&u,&v);
    Insert(u,v);
    Insert(v,u);
  }
  for (int i=1; i<=n; i++) {
    if (!vis[i]) {
      Dfs(i,i);
    }
  }
  int q;
  scanf("%d",&q);
  for(int i=1; i<=q; i++){
    int u,v;
    scanf("%d%d",&u,&v);
    if (color[u]==color[v]) {
      printf("yes\n");
    }
    else {
      printf("no\n");
    }
  }
}

C++: #include<iostream>
using namespace std;
#include<stdio.h>

int *P;

int root(int a)
{
    if(P[a] == a) return a;
    P[a] = root(P[a]);
    return P[a]; 
}

void join(int a, int b){ int x = root(a), y = root(b); x = P[y]; }

bool same(int a, int b){ return (root(a) == root(b) ? true : false); }

int main()
{
    int i, n, m, a, b, q;
    scanf("%d %d", &n, &m);
    P = new int [n];
    for(i = 0; i < n; i++) P[i] = i;

    for(i = 0; i < m; i++){ scanf("%d %d", &a, &b); join(a, b); }

    scanf("%d", &q);
    for(i = 0; i < q; i++){
        scanf("%d %d", &a, &b);
        if(same(a, b)){ printf("yes\n"); }else{ printf("no\n"); }
    }
    delete [] P;
    return 0; 
}

C++: // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define Graph vector< vector<int> >
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1000000007;

int main(){
    int N, M;
    cin >> N >> M;
    Graph G(N);
    rep(i, M){
        int s, t;
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    vector<int> connect(N, -1);

    for(int c = 0; c < N; c++){
        if(connect[c] != -1) continue;

        queue<int> que;
        connect[c] = c;
        que.push(c);

        while(!que.empty()){
            int v = que.front();
            que.pop();
            for(int i = 0; i < G[v].size(); i++){
                int v2 = G[v][i];
                if(connect[v2] != -1) continue;
                connect[v2] = c;
                que.push(v2);
            }
        }
    }

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

C++: #include<iostream>
#include<vector>
#include<stack>
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; //preset
  }
  
  for ( int u = 0; u < n; ++u) {
    if ( color[u] == NIL ) {
      dfs(u,id);
      id++;
    }
  }
  
  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++: #define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <climits>
#include <list>
#include <queue>

using namespace std;

list<int> *AdjacenceyList;
int CID;
int *ComponentID;
queue<int> Q;

void bfs(int v) {
	ComponentID[v] = CID;
	Q.push(v);
	while (Q.empty() != true) {
		v = Q.front();
		Q.pop();
		for (list<int>::iterator it = AdjacenceyList[v].begin(); it != AdjacenceyList[v].end(); it++) {
			if (ComponentID[*it] == 0) {
				ComponentID[*it] = CID;
				Q.push(*it);
			}
		}
	}
	CID++;
}

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

	AdjacenceyList = new list<int>[n];
	
	ComponentID = new int[n];
	for (i = 0; i < n; i++) {
		ComponentID[i] = 0;
	}
	CID = 1;

	for (i = 1; i <= m; i++) {
		cin >> s >> t;
		AdjacenceyList[s].push_back(t);
		AdjacenceyList[t].push_back(s);
	}

	for (i = 0; i < n; i++) {
		if (ComponentID[i] == 0) {
			bfs(i);
		}
	}

	cin >> q;
	for (i = 1; i <= q; i++) {
		cin >> s >> t;
		if (ComponentID[s] == ComponentID[t]) {
			printf("yes\n");
		}
		else {
			printf("no\n");
		}
	}

	delete[] AdjacenceyList;
	delete[] ComponentID;
	return 0;
}