Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
	"bytes"
)

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

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

using namespace std;

static const int MAX = 100000;
static const int NIL = -1;

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

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);
            }
        }
    }
}
void assignColor() {
    int id = 0;
    for (int i = 0; i < n; i++) color[i] = NIL;
    for (int u = 0; u < n; u++) {
        if (color[u] == NIL) dfs(u, id++);
    }
}
int main() {
    int m, s, t, q;

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

    assignColor();

    cin >> q;

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

    return 0;
}

C++: #include <iostream>
using namespace std;
  
int main() {
int d[100100];
int m,n,q;
cin>>n>>m;
for(int i=0;i<n;i++){
    d[i]=i;
}
for(int l=0;l<m;l++){
    int a,b;
    cin>>a>>b;
    int j ,h;
    j=a,h=b;
while(d[j]!=j && d[h]!=h){
    j=d[j];
    h=d[h];
}
if(d[j]==j){
	while(d[h]!=h){
		h=d[h];
	}
}
else{
	while(d[j]!=j){
		j=d[j];
	}
}
d[j]=h;
d[a]=h;
d[b]=h;
}
cin>>q;
for(int y=0;y<q;y++){
    int a,b;
    cin>>a>>b;
        int j ,h;
    j=a,h=b;
while(d[j]!=j || d[h]!=h){
    j=d[j];
    h=d[h];
}
    if(j==h){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<endl;
    }
}
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<bitset>
#include<queue>
#include<set>
#include<stack>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) FOR(i,0,n)
#define MAX 100000
typedef long long ll;
int main(){
	//連結リストを作る
	int n,m;	//user_num, relation
	cin >> n>>m;
	vector<int> G[n];

	int a,b;
	rep(i,m){
	cin >> a>>b;
	G[a].push_back(b);
	G[b].push_back(a);
	}

	//色を塗っていく
	//初期化。全て0
	int color[MAX] = {};
	int id = 1;

	rep(i,n){
	if(!color[i]) {
	
	stack<int> S;
	S.push(i);	
	color[i] = id;

	while(!S.empty()){
	int v = S.top(); S.pop();

	rep(j,G[v].size()){
	int u = G[v][j];
	if(!color[u]){
	color[u] = id;
	S.push(u);
	}
	}
	}
	}
	id++;
	}	
	
	int q,c,d;
	cin >> q;
	rep(i,q){
	cin >> c >> d;

	if (color[c]==color[d]){
	cout << "yes" << endl;
	}
	else
	cout << "no" << endl;
	}
	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<vector<int> > vvi;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<string, int> psi;
typedef pair<int, int> pii;
#define rep(i,s,n) for(int i = (s); i < (n); i++)
#define loop(i) for(int i = 0;true; i++)
#define all(x) (x).begin(), (x).end()
#define yn(f) (f?"yes":"no")
#define YN(f) (f?"YES":"NO")
#define Yn(f) (f?"Yes":"No")
#define mn 1000000007
int main() {
	int N, M;
	cin >> N;
	cin >> M;
	vvi A(N);
	for (int i = 0; i < M; i++)
	{
		int l, r;
		scanf("%d", &l);
		scanf("%d", &r);
		A[l].push_back(r);
		A[r].push_back(l);
	}
	vi C(N, -1);
	vi v(N, 0);
	int num = 0;
	for (int i = 0; i < N; i++)
	{
		if (v[i]) {
			continue;
		}
		else {
			v[i] = 1;
			num++;
			C[i] = num;
		}
		stack<int> S;
		S.push(i);
		while (!S.empty()) {
			int temp = S.top();
			S.pop();
			for (int j = 0; j < A[temp].size(); j++)
			{
				if (v[A[temp][j]] == 0) {
					S.push(A[temp][j]);
					v[A[temp][j]] = 1;
					C[A[temp][j]] = num;
				}
			}

		}
	}
	for (int i = 0; i < N; i++)
	{
		
	}
	int k;
	cin >> k;
	for (int i = 0; i < k; i++)
	{
		int l, r;
		scanf("%d", &l);
		scanf("%d", &r);

		printf("%s\n", yn(C[l] == C[r]));
	}
};

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
 
struct node {
    int id;
    vector<int> rel;
};

struct memo {
	int a, b;
	bool val;
	bool operator==(memo m){
		return (this->a == m.a && this->b == m.b) || (this->a == m.b && this->b == m.a);
	}
};
 
int n, m, q;
node* nodes;
vector<memo> ms;

bool canReach(int s, int d, bool* h){
    h[s] = true;
	if(s==d) return true;
	
	memo nm;
	nm.a = s;
	nm.b = d;
	
	vector<memo>::iterator it = find(ms.begin(), ms.end(), nm);
	if(it != ms.end()) return (*it).val;
	
    for(int i = 0; i < nodes[s].rel.size(); i++){
		if(!h[nodes[s].rel[i]] && canReach(nodes[s].rel[i], d, h)){
			nm.val = true;
			ms.push_back(nm);
			return true;
        }
    }
	
	nm.val = false;
	ms.push_back(nm);
    return false;
}
 
int main() {
    scanf("%d %d", &n, &m);
     
    nodes = new node[n];
    for(int i = 0; i < n; i++){
        nodes[i].id = i;
    }
    while(m--){
        int l, r;
        scanf("%d %d", &l, &r);
        nodes[l].rel.push_back(r);
        nodes[r].rel.push_back(l);
    }
     
    scanf("%d", &q);
    while(q--){
        int l, r;
        scanf("%d %d", &l, &r);
         
        bool *h = new bool[n];
        for(int i = 0; i < n; i++){
            h[i] = false;
        }
        printf("%s\n", canReach(l, r, h) ? "yes" : "no");
        delete[] h;
    }
    return 0;
}

C++: def dfs(s):
    global t
    if s == t:
        return True
    DP[s]=1
    for i in F[s]:
        if not DP[i]:
            if dfs(i):
                return True
    return False
    
n,m = [int(i) for i in input().split()]
F = [{}]*n
for _ in range(m):
    i,j = [int(i) for i in input().split()]
    F[i][j] = 1
    F[j][i] = 1
q = int(input())
for i in range(q):
    DP = [0]*n
    s,t = [int(i) for i in input().split()]
    if dfs(s):
        print("yes")
    else:
        print("no")