Go: package main

import (
	"fmt"
)

var n, m int
var g [][]int
var color []int

func main() {
	fmt.Scan(&n, &m)
	g = make([][]int, n) // SNSの関係を表すグラフ
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		g[a] = append(g[a], b) // 無向重みなしグラフ
		g[b] = append(g[b], a)
	}
	color = make([]int, n) // 各ノードにつける色
	for i := 0; i < len(color); i++ {
		color[i] = -1 // 初期化しておく
	}
	type query struct {
		s, t int
	}
	var Q int
	fmt.Scan(&Q)
	qs := make([]query, Q)
	for i := 0; i < Q; i++ {
		fmt.Scan(&qs[i].s, &qs[i].t)
	}
	// fmt.Println("n", n, "m", m, "g", g, "Q", Q, "qs", qs)
	// 全ての頂点からdfsする
	id := 1
	for u := 0; u < n; u++ {
		if color[u] == -1 {
			dfs(u, id)
			id++
		}
	}
	// fmt.Println("color", color)
	for i := 0; i < Q; i++ {
		if color[qs[i].s] == color[qs[i].t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func dfs(v, c int) {
	// fmt.Printf("dfs(v %d, c %d) en:\n", v, c)
	color[v] = c
	for _, vv := range g[v] {
		// fmt.Printf("  v %d, g[v] %v, vv %d\n", v, g[v], vv)
		if color[vv] == -1 {
			dfs(vv, c)
		}
	}
}

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

using namespace std;

const int MAX = 100000;

vector<int> G[MAX];
int C[MAX] = {0};

void dfs(int u, int c) {
    if (C[u] == 0) {
        C[u] = c;
        for (auto v : G[u]) {
            if (C[v] == 0) dfs(v, c);
        }
    }
}

int main() {

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

    for (int i = 0; i < m; i++) {
        int s, t;
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    int c = 1;
    for (int i = 0; i < n; i++) {
        if (C[i] == 0) dfs(i, c++);
    }

    //
    int q;
    cin >> q;

    for (int i = 0; i < q; i++) {
        int s, t;
        cin >> s >> t;
        if (C[s] != 0 && C[s] == C[t]) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
}

C++: #include <bits/stdc++.h>

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int n, m;
struct node {
    int id;
    node* parent;
};

node find(node x){
    if(x.parent == NULL)
        return x;
    else
        x = find(*x.parent);
    return x;
}

void marge(node x, node y){
    x = find(x);
    y = find(y);
    if(x.id == y.id)
        return;
    else
        y.parent = &x;
    return;
}

bool judge(node x, node y){
    x = find(x);
    y = find(y);
    return x.id == y.id;
}

int main() {
    cin >> n >> m;
    node person[n];
    REP(i, n){
        person[i].id = i;
        person[i].parent = NULL;
    }
    REP(i, m){
        int s, t;
        cin >> s >> t;
        if(person[t].parent == NULL)
            person[t].parent = &person[s];
        else if(person[s].parent == NULL)
            person[s].parent = &person[t];
        else marge(person[s], person[t]);
    }
    int q;
    cin >> q;
    REP(i, q){
        int s, t;
        cin >> s >> t;
        if(judge(person[s], person[t]))
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    return 0;
}

C++: #include<stdio.h>
#include<queue>

#define MAX 1000

int G[MAX+1][MAX+1];
int n, m;
std::queue<int> Q;

//bool bfs(int src, int dest) {
//    bool C[MAX];
//    for (int i = 0; i < n; ++i) C[i] = false;
//
//    Q.push(src);
//    while(!Q.empty()) {
//        int cur = Q.front();Q.pop();
//        for (int i = 0; i < n; ++i) {
//            if (G[cur][i] == 0) continue;
//            if (C[i]) continue;
//            if (i == dest) return true;
//            Q.push(i);
//            C[i] = true;
//        }
//    }
//
//    return false;
//}

int main() {
    //scanf("%d %d", &n, &m);
    //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 u,k;
    //    scanf("%d %d", &u, &k);
    //    G[u][k] = 1;
    //    G[k][u] = 1;
    //}

    //int q;
    //scanf("%d", &q);
    //for (int i = 1; i <= q; ++i) {
    //    int s,d;
    //    scanf("%d %d", &s, &d);
    //    bfs(s, d) ? printf("yes\n") : printf("no\n");
    //}

    return 0;
}

C++: //#define scanf_s scanf
#include <string>
#include <stdio.h>
#include <math.h>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

#define MAX 100
#define _MAX 1000
struct Graph
{
	int id, k = 0;
	list<int> node;
	int d = -1;
};
Graph G[MAX];
list<int> co;
int bo = false;
void Connect(Graph *g,Graph *f)
{
	if (g->k >= 0) {
		int a = true;
		co.push_back(g -> id);
		printf("%d,", g->id);
		for (auto i = g->node.begin(); i != g->node.end(); ++i) {
			if (&G[*i] == f) { bo = true; return; }
			for (auto j = co.begin(); j != co.end(); ++j) {
				if (*j == *i) {
					a = false;
					break;
				}
			}
			if(a) return Connect(&G[*i], f);
			a = true;
		}
	}
}
int main(void)
{
	int n, m, q, a, b;
	int qq[_MAX] = { 0 };
	scanf_s("%d %d", &n, &m);
	for (int i = 0; i < m; ++i) {
		scanf_s("%d %d", &a, &b);
		G[a].id = a; G[b].id = b;
		++G[a].k; G[a].node.push_back(b);
		++G[b].k; G[b].node.push_back(a);
	}
	scanf_s("%d", &q);
	for (int i = 0; i < q; ++i) {
		scanf_s("%d %d", &a, &b);
		bo = 0;
		Connect(&G[a], &G[b]);
		qq[i] = bo;
		co.clear();
	}
	for (int i = 0; i < q; ++i) {
		if(qq[i]) printf("yes\n");
		else printf("no\n");
	}
}

C++: #include<iostream>
#include<map>
#include<set>

using namespace std;

int main(){
	int n, m, s, t, q, i, j;
	int id, stc = 0;
	bool answer_yes = false;
	map<int, set<int> > Adj;
	map<int, int> Stack;
	set<int> Check;
	set<int>::iterator it, itend;

	cin >> n >> m;
	for (i = 0; i < m; i++) {
		cin >> s >> t;
		Adj[s].insert(t);
		Adj[t].insert(s);
	}

	/*for (auto it1 = Adj.begin(); it1 != Adj.end(); it1++) {
		cout << "\n" << it1->first << " : " << flush;
		for (auto it2 = it1->second.begin(); it2 != it1->second.end(); it2++) {
			cout << *it2 << " " << flush;
		}
	}
	cout << endl;*/

	cin >> q;
	for (i = 0; i < q; i++) {
		cin >> s >> t;
		
		// calculate
		Stack.insert(pair<int, int>(stc, s));
		Check.insert(s);
		while (!Stack.empty()) {
			id = Stack[stc];
			//cout << "\nid : " << id << " stc : " << stc << endl;
			itend = Adj[id].end();
			for (it = Adj[id].begin(); it != itend; it++) {
				//cout << *it << " " << flush;
				if (Check.find(*it) == Check.end()) {
					if (*it == t) {
						answer_yes = true;
						Stack.clear();
						break;
					}
					stc++;
					Stack.insert(pair<int, int>(stc, *it));
					Check.insert(*it);
					break;
				}
			}
			if (it == itend) {
				Stack.erase(stc);
				stc--;
			}
		}
		if (answer_yes) {
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
			Stack.clear();
		}
		answer_yes = false;
		Check.clear();
		stc = 0;
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int n, m;
struct node {
    int parent;
};

int main() {
    cin >> n >> m;
    node person[n];
    REP(i, n)
        person[i].parent = -1;
    REP(i, m){
        int s, t;
        cin >> s >> t;
        if(person[s].parent == -1){
            person[s].parent = s;
            person[t].parent = s;
        }
        else
            person[t].parent = person[s].parent;
    }
    int q;
    cin >> q;
    REP(i, q){
        int s, t;
        cin >> s >> t;
        if(person[s].parent != -1 && person[t].parent != -1  && person[s].parent == person[t].parent)
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    return 0;
}