Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

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

using namespace std;

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

int n, m, c;
vector<int> G[N];
int color[N];

void bfs(int u)
{
	queue<int> q;
	q.push(u);
	color[u] = c;

	while (!q.empty())
	{
		u = q.front(); q.pop();
		for (int v = 0; v < G[u].size(); ++v)
		{
			if (color[v] == -1)
			{
				q.push(v);
				color[v] = c;
			}
		}
	}
}

void DrawColor()
{
	for (int i = 0; i < n; ++i) color[i] = -1;
	for (int i = 0; i < n; ++i)
	{
		if (color[i] == -1) bfs(i);
		c++;
	}
}

int main()
{
	cin >> n >> m;
	for (int i = 0; i < m; ++i)
	{
		int u, v; cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}

	DrawColor();

	int n2, p1, p2;
	cin >> n2;
	for (int i = 0; i < n2; ++i)
	{
		cin >> p1 >> p2;
		if (color[p1] == color[p2])
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
#include<set>
using namespace std;

bool DFS(vector<vector<int> >& G, int a, int b) {
    if (G[a].size() == 0) return false;

    stack<pair<int, int> > S;
    vector<bool> is_visited(G.size(), false);
    int idx = 0;
    do {
        if (idx >= G[a].size()) {
            a   = S.top().first;
            idx = S.top().second;
            S.pop();
            idx++;
            continue;
        }
        if (G[a][idx] == b) return true;
        if (!is_visited[G[a][idx]]) {
            is_visited[G[a][idx]] = true;
            S.push(make_pair(a, idx));
            a   = G[a][idx];
            idx = 0;
            continue;
        }
        idx++;
    } while (!S.empty());
    return false;
}


int main(){
    int n, m;
    cin >> n >> m;

    vector<vector<int> > G(n);
vector<vector<bool> > D(100000, vector<bool>(100000));

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

// for (int i = 0; i < n; i++) {
//     cout << i << " : ";
//     for (set<int>::iterator it = G[i].begin(); it != G[i].end(); it++){
//         cout << *it << " ";
//     }
//     cout << endl;
// }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        if (DFS(G, a, b)) {
           cout << "yes" << endl;
        } else {
           cout << "no"  << endl;
        }
    }
    return 0;
}

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

using namespace std;
static const int MAX=100;
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);
            }
        }
    }
}

void assign_color()
{
    int id=1;
    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 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);
    }
    assign_color();
    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<cstdio>
#include<vector>
#define N 100000
using namespace std;

vector<int> G[N];
/*
??£??\??????????????????????????°???????????¨??????????????£????????????????????????DFS??§????????????

 */
int labels[N], c=0;

void dfs_init(){
    int i;
    for(i=0;i<N; i++)labels[i]=0;
}

/*
  ?????°?????¨??????????£?
  ??§???????????????????????????
  stack???????????°??§???????????????
 */
bool dfs(int u, int v){
    if (u == v) return true;
    labels[u] = 1;
    int i,x;
    for(i=0; i<G[u].size(); i++){
	x = G[u][i];
	dfs(x,v);
    }
    return false;
}

void dfs2(int u){
    labels[u]=c;
    int i;
    for(i=0; i<G[u].size(); i++){
	if (labels[G[u][i]]==0) dfs2(G[u][i]);
    }
}


int main(void) {
    int n,m,i,q, u,v;
    scanf("%d %d",&n,&m);
    for(i=0;i<m;i++){
	scanf("%d %d",&u,&v);
	G[u].push_back(v);
	G[v].push_back(u);
    }
    dfs_init();
    /*
      ????????§?????¨???????????????
     */
    for(i=0; i<n; i++){
	if(labels[i]==0){
	    c++;
	    dfs2(i);
	}
    }

    scanf("%d",&q);
    for(i=0;i<q;i++){
	scanf("%d %d", &u, &v);
	if(labels[u]>0 && labels[u]==labels[v])printf("yes\n");
	else printf("no\n");
    }
    
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int d[100001];
int main(void){
	int	n,m;
	int s,t;
	int q;
	int i,j;
	int x,y;
	int cnt;
	vector<int>v[100001];
	queue<int>que;
	cin>>n>>m;
	for(i=0;i<m;i++){
		cin>>s>>t;
		v[s].push_back(t);
		v[t].push_back(s);
	}
	for(i=0;i<n;i++)	d[i]=0;
	cnt=0;
	for(i=0;i<n;i++){
		if(d[i]==0){
			cnt++;
			d[i]=cnt;
			que.push(i);
			while(!que.empty()){
				x=que.front();
				que.pop();
				for(j=0;j<v[x].size();j++){
					y=v[x][j];
					if(d[y]==0){
						d[y]=cnt;
						que.push(y);
					}
				}
			}
		}
	}
	cin>>q;
	for(i=0;i<q;i++){
		cin>>s>>t;
		cout<<((d[s]==d[t])?"yes":"no")<<endl;
	}
	
	return 0;
}