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<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);
            }
            
        }
    }
        }

void assignColor() { 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);
         }
    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>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <stack>
#include <numeric>
#include <algorithm>
#include <tuple>
#include <stdio.h>
#include <bitset>
#include <limits.h>
#include <complex>
#include <deque>
#include <iomanip>
#include <list>
#include <cstring>
using namespace std;
#define pi pair<int,int>
#define pl pair<long long,long long>
#define chmax(a,b) (a<b ? a=b:0)
#define chmin(a,b) (a>b ? a=b:0)
#define en cout << endl //セミコロンつけろ
//#define MM 1000000000
//#define MOD MM+7
typedef long long ll;
const int MM = 1e9;
const int MOD = MM+7;
const long double PI = acos(-1);
const long long INF = 1e15;
int dx[8] = {-1,0,1,0,-1,-1,1,1};
int dy[8] = {0,-1,0,1,-1,1,1,-1};
// 'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122

template<typename T> //最大公約数
T GCD(T u,T v){
    return v ? GCD(v,u%v) : u;
}
template<typename T> //最小公倍数
T LCM(T x,T y){
    T gc = GCD(x,y);
    return x*y/gc;
}

struct Edge
{
    int to, cost;
};
struct Node
{
    int from, to, cost;
};
vector<vector<int> > g(0), revG(0);
vector<int> order(0), marker(0);

void dfs(int num, int count){
    queue<int> que;
    que.push(num);
    while (que.size()){
        int now = que.front();
        marker[now] = count;
        que.pop();
        for (int i = 0; i < g[now].size(); i++){
            int to = g[now][i];
            if (marker[to] == 0){
                que.push(to);
            }
        }
    }
}

int main(){
    int N,M; cin >> N >> M;
    g.resize(N);
    marker.assign(N, 0);
    for (int i = 0; i < M; i++){
        int s, t; cin >> s >> t;
        g[s].push_back(t);
        g[t].push_back(s);
    }
    int Q; cin >> Q;
    vector<int> uu(Q), vv(Q);
    for (int i = 0; i < Q; i++){
        int u, v; cin >> u >> v;
        uu[i] = u; 
        vv[i] = v;
    }

    int count = 1;
    for (int i = 0; i < N; i++){
        if (marker[i] == 0){
            dfs(i, count);
            count++;
        }
    }
    for (int i = 0; i < Q; i++){
        if (marker[uu[i]] == marker[vv[i]]){
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <stdio.h>
#include <cmath>
#include <set>
#include <map>
using namespace std;

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

vector<bool>visited;
bool dfs(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(dfs(g[s][i],t)){
                return true;
            }
        }
    }
    return false;
}
int main() {
    cin>>n>>m;
    g.resize(n);
    
    for(int i=0;i<m;i++){
        int s,t;
        cin>>s>>t;
        g[s].push_back(t);
        g[t].push_back(s);
    }
    cin>>q;
    for(int i=0;i<q;i++){
        visited.resize(n,false);
        int s,t;
        cin>>s>>t;
        cout<<(dfs(s,t)?"yes":"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[i]==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();
    
    for(int i=0;i<n;i++)
    {
        cin>>s>>t;
        if(color[s]==color[r])
        {
            cout<<"yes"<<endl;
        }
        else
        {
            cout<<"no"<<endl;
        }
    }
    return 0;
}

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);
      }
    }
  }
}

void assingColor(){
  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);
  }

  assingColor();
  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;
}