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 <algorithm>
#include <cstdio>
#include <vector>
#include <stack>
using namespace std;
#define maxn 10005
#define NTL -1
int color[maxn];
vector<int> g[maxn];
stack<int> s;
int n,m,q;
void tarjan(int r,int c)
{
    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]==NTL)
            {
                color[v]=c;
                s.push(v);
            }
        }
    }
}
void assigncolor()
{
    int id=0;
    for(int i=0;i<n;i++)
        color[i]=NTL;
    for(int u=0;u<n;u++)
    {
        if(color[u]==NTL)
            tarjan(u,id++);
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    assigncolor();
    scanf("%d",&q);
    while(q>0)
    {
        q--;
        int u,v;
        scanf("%d%d",&u,&v);
        if(color[u]==color[v])
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}

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() {
    while(1){}
    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<bits/stdc++.h>

using namespace std;

vector<int> numbers;

void setNum(vector<vector<int> > a){
    numbers.resize(a.size());
    for(int i = 0; i < numbers.size(); i++)numbers[i] = -1;
    int num = 0;
    queue<int> place;
    place.push(0);
    while(place.size()){
        int now = place.front();
        place.pop();
        numbers[now] = num;
        for(int i = 0; i < a[now].size(); i++){
            int next = a[now][i];
            if(numbers[next] == -1){
                place.push(next);
            }
        }
        if(place.size() == 0){
            for(int i = 0; i < numbers.size(); i++){
                if(numbers[i] == -1){
                    place.push(i);
                    num++;
                    break;
                }
            }
        }
    }
}

int main(void){
    int n, m;
    cin >> n >> m;
    vector<vector<int> > a(n);
    for(int i = 0; i < m; i++){
        int s, t;
        cin >> s >> t;
        a[s].push_back(t);
        a[t].push_back(s);
    }
    int q;
    cin >> q;
    setNum(a);
    for(int i = 0; i < q; i++){
        int s, t;
        cin >> s >> t;
        if(numbers[s] == numbers[t])cout << "yes" << endl;
        else cout << "no" << endl;
    }
}

C++: #include<bits/stdc++.h>
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);
  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);
      }
    }
  }
}
int main(){
  int s,t,m,p;
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>s>>t;
    G[s].push_back(t);
    G[t].push_back(s);
  }
  int id=1;
  for(int i=0;i<n;i++){
    color[i]=NIL;
  }
  for(int i=0;i<n;i++){
    if(color[i]==NIL){
      dfs(i,id++);
    }
  }
  cin>>p;
  for(int i=0;i<p;i++){
    cin>>s>>t;
    if(color[s]==color[t]){
      cout<<"yes"<<endl;
    }else{
      cout<<"no"<<endl;
    }
  }
  return(0);
}

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

using namespace std;
struct user {
    user() {};
    user(int i) : id {i}, fs {}, group {-1} {};
    int id;
    vector<int> fs;
    int group;
};

void add_friends(user *us, int x, int y){
    us[x].fs.push_back(y);
    us[y].fs.push_back(x);
}

void dfs(user* us, int id, int g){
    stack<int> ss {};
    ss.push(id);
    while(ss.size()){
        int p = ss.top();
        ss.pop();
        us[p].group = g;
        vector<int> fs = us[p].fs;
        for(auto x : fs){
            if(us[x].group == g){
                continue;
            }
            ss.push(x);
        }
    }
}

void make_group(user* us, int n){
    int g = 0;
    for(int i = 0; i < n; ++i){
        if(us[i].group < 0){
            dfs(us, i, g);
            ++g;
        }
    }
}

int main(){
    int n, m;
    cin >> n >> m;
    user us[n];
    for(int i = 0; i < n; ++i){
        us[i] = user(i);
    }
    for(int i = 0; i < m; ++i){
        int a, b;
        cin >> a >> b;
        add_friends(us, a, b);
    }
    make_group(us, n);
    int q;
    cin >> q;
    for(int i = 0; i < q; ++ i){
        int a, b;
        cin >> a >> b;
        if(us[a].group == us[b].group){
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rp(i,j) for(long long i=0;i<j;i++)
#define rep(i,j,k) for(long long i=j;i<k;i++)
#define mod 1000000007
#define NIL -1
#define MAX 100000
typedef long long ll;
int n;
vector<int>a[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();
        rp(i,a[u].size()){
            int v=a[u][i];
            if(color[v]==NIL){
                color[v]=c;
                S.push(v);
            }
        }
    }
}
void colordefine(){
    int id=1;
    rp(i,n)color[i]=NIL;
    rp(i,n){
        if(color[i]==NIL)dfs(i,id++);
    }
}
int main(){
    int m,s,t,q;
    cin>>n>>m;
    rp(i,m){
        cin>>s>>t;
        a[s].push_back(t);
        a[t].push_back(s);
    }
    colordefine();
    cin>>q;
    rp(i,q){
        cin>>s>>t;
        string ans;
        if(color[s]==color[t])ans="yes";
        else ans="no";
        cout<<ans<<endl;
    }
}