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<stdio.h>
#include<vector>
#include<string>
#include<iostream>
using namespace std;

vector<vector<int> >g;
int n,m,q,s,t;

vector<bool>visited;
vector<int>color;
void dfs(int s){
    for(int i=0;i<g[s].size();i++){
        if(color[g[s][i]]==-1){
            color[g[s][i]]=color[s];
            dfs(g[s][i]);
        }
    }
}
bool dfs2(int s,int t){
    if(s==t)return true;
    visited[s]=true;
    for(int i=0;i<g[s].size();i++){
        if(!visited[g[s][i]]){
            if(dfs2(g[s][i],t)){
                return true;
            }
        }
    }
    return false;
}
int main() {
    scanf("%d %d",&n,&m);
    g.resize(n);
    color.resize(n,-1);
    for(int i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        g[s].push_back(t);
        g[t].push_back(s);
    }
    for(int i=0;i<n;i++){
        if(color[i]==-1){
            color[i]=i;
            dfs(i);
        }
    }
    scanf("%d",&q);
    visited.resize(n);
    for(int i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        for(int j=0;j<n;j++)visited[j]=false;
        string s1=(dfs2(s,t)?"yes":"no");
        string s2=(color[s]==color[t]?"yes":"no");
        //if(s1!=s2&&s1=="yes")return 1;
        cout<<s1<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <map>
#include <queue>
using namespace std;
int main(){
  int n,m,t1,t2,c[100000]={}; map <int,bool> g[100000];
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>t1>>t2;g[t1][t2]=1;g[t2][t1]=1;
  }
  int l;cin>>l;
  for(int i=0;i<l;i++){
    queue <int> q;
    bool f[100000]={},find=false;
    cin>>t1>>t2;
    if(c[t1]&&c[t2]){
      if(c[t1]==c[t2]) cout<<"yes"<<endl;
      else cout<<"no"<<endl;
      continue;
    }
    q.push(t1);f[t1]=true;c[t1]=i+1;
    for(;!q.empty();){
      for(int j=0;j<n;j++) if(g[q.front()][j]&&!f[j]){
          if(j==t2) find=true;
          f[j]=true;c[t2]=i+1;
          q.push(j);
	}
      q.pop();
    }
    if(find) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define MAX_N 500000
vector<int>X[MAX_N]; queue<int>Q;
int COLOR[MAX_N], N, M, Q1, A, B;
int main() {
    cin >> N >> M;
    for (int i = 0; i < M; i++) { cin >> A >> B; X[A].push_back(B); X[B].push_back(A); }
    int cnt = 0;
    for (int i = 0; i < N; i++) {
        if (COLOR[i] == 0) {
            Q.push(i); cnt++; COLOR[i] = cnt;
            while (!Q.empty()) {
                int a = Q.front(); Q.pop();
                for (int j = 0; j < X[a].size(); j++) {
                    int to = X[a][j];
                    if (COLOR[to] == 0) {
                        COLOR[to] = cnt;
                        Q.push(to);
                    }
                }
            }
        }
    }
    cin >> Q1;
    for (int i = 0; i < Q1; i++) {
        cin >> A >> B;
        if (COLOR[A] == COLOR[B]) {
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }
    return 0;
}

C++: #include<iostream>    // ???????????????????§?????????£????????????
using namespace std;
#include<stdio.h>

typedef struct fox { int val; fox *prev, *next; } fox_t;

fox_t* init()
{
	fox_t* head = new fox_t;
	head->val = -1;
	head->next = NULL;
	head->prev = NULL;
	return head;
}

fox_t* make_tail(fox_t* head)
{
	fox_t* tail = new fox_t;
	tail->val = -1;
	head->next = tail;
	tail->next = NULL;
	tail->prev = head;
	return tail;
}

bool isEmpty(fox_t* head, fox_t* tail)
{
	return (head->next == tail ? true : false);
}

fox_t* b_insert(fox_t* tail, int data)
{
	fox_t* fox = new fox_t;
	fox->val = data;
	fox->prev = tail->prev;
	fox->next = tail;
	tail->prev->next = fox;
	tail->prev = fox;
	return fox;
}

void f_show(fox_t* head, fox_t* tail)
{
	cout << "----??????????????¨???----" << endl;
	fox_t* fox = head->next;

	while(fox != tail){
		cout << fox->val;
		fox = fox->next;
		if(fox != tail) cout << " ";
	};
	cout << endl;
	return;
}
// docking.
void unionfox(fox_t* head1, fox_t* tail1, fox_t* head2, fox_t* tail2)
{
	if(isEmpty(head1, tail1) || isEmpty(head2, tail2)) return;
	fox_t* fox = tail1->prev;
	cout << tail1->prev << endl;
	cout << head2->next << endl;
	fox->next = head2->next;
	head2->next->prev = fox;
	tail2->prev->next = tail1;
	tail1->prev = tail2->prev;
	delete tail2;
	delete head2;
}

int main()
{
	int color[1001];
	int c_num = 0;
	fox_t* foxhead[1001];
	fox_t* foxtail[1001];
	int i, n, m, q;
	int a, b;
	cin >> n >> m;
	for(i = 0; i < n; i++){
		color[i] = -1;
		foxhead[i] = init();
		foxtail[i] = make_tail(foxhead[i]);
	}
	int c_a, c_b, val;

	for(i = 0; i < m; i++){
		cin >> a >> b;
		c_a = color[a];
		c_b = color[b];
		if(c_a < c_b) swap(a, b);  // color[a] >= color[b].
		if(c_a < 0 && c_b < 0){
			color[a] = c_num; color[b] = c_num;
			b_insert(foxtail[c_num], a);
			b_insert(foxtail[c_num], b);
			c_num++;
		}else if(c_b < 0){
			color[b] = c_a;
			b_insert(foxtail[c_a], b);
		}else if(c_a == c_b){
			continue;
		}else{ // color[a] != color[b].
			// b????????¬?????????color???a???color??????????????§???union?????????
			fox_t* fox = foxhead[c_b]->next;
			while(fox != foxtail[c_b]){
				val = fox->val;
				color[val] = c_a;
				fox = fox->next;
			};
			unionfox(foxhead[c_a], foxtail[c_a], foxhead[c_b], foxtail[c_b]);
		}
	}
	cin >> q;
	for(i = 0; i < q; i++){
		cin >> a >> b;
		if(color[a] >= 0 && (color[a] == color[b])){
			printf("yes\n"); }else{ printf("no\n"); 
			}
	}
	return 0;
}