Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	nConns := scanInt(sc)

	graph := make([][]int, nNodes)
	for i := 0; i < nConns; i++ {
		a, b := scanInt(sc), scanInt(sc)
		graph[a] = append(graph[a], b)
		graph[b] = append(graph[b], a)
	}

	groups := make([]int, nNodes)

	var setGroups func(int, int)
	setGroups = func(u int, groupId int) {
		if groups[u] > 0 {
			return
		}
		groups[u] = groupId

		for _, v := range graph[u] {
			setGroups(v, groupId)
		}
	}
	for i := 0; i < nNodes; i++ {
		setGroups(i, i+1)
	}

	nQuestions := scanInt(sc)

	w := bufio.NewWriter(os.Stdout)
	var ans string
	for i := 0; i < nQuestions; i++ {
		a, b := scanInt(sc), scanInt(sc)
		if groups[a] == groups[b] {
			ans = "yes"
		} else {
			ans = "no"
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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];
}
d[j]=h;
while(a!=h ||b!=h){
 
    d[a]=h;
    d[b]=h;
a=d[a];
b=d[b];
}
}

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++: //16D8103013E Sen Shunkou
#include<iostream>
#include<queue>
int main(){
    //input
    int n,m;
    std::cin>>n;
    std::cin>>m;
    int A[n][n];
    bool visited[n];
    int judge[n];

    for(int i=0;i<n;i++){
        judge[i]=-1;
        visited[i]=false;
        A[i][0]=0;
        for(int j=1;j<n;j++){
            A[i][0]=0;
            A[i][j]=-1;
        }    
    }

    int cnt_i=0,cnt_j=0;
    for(int k=0;k<m;k++){
        int i,j;
        std::cin>>i;
        std::cin>>j;
        A[i][0]++; cnt_i=A[i][0];
        A[j][0]++; cnt_j=A[j][0];
        A[i][cnt_i]=j;
        A[j][cnt_j]=i;
    }

    /*for(int i=0;i<10;i++){
        for(int j=0;j<10;j++){
            std::cout<<" "<<A[i][j];
        }
        std::cout<<std::endl;
    }*/

    int n_2;
    std::cin>>n_2;
    for(int i=0;i<n_2;i++){
        int s,e;
        std::cin>>s;
        std::cin>>e;
        
        int cnt=0;
        for(int i=0;i<n;i++){
            judge[i]=-1;
            visited[i]=false;
        }
        
        std::queue<int> que;
        que.push(s);
        /*for(int i=0;i<10;i++){
            for(int j=0;j<10;j++){
                //std::cout<<" "<<A[i][j];
            }
            //std::cout<<std::endl;
        }*/
        while(!que.empty()){
            int q=que.size();
            for(int k=0;k<q;k++){
                //std::cout<<"quesize"<<que.size()<<"\n";
                int i=que.front();
                que.pop();
                visited[i]=true;
                judge[i]=cnt;
                //std::cout<<i<<"cnt"<<cnt<<"\n";
                //std::cout<<A[i][0]<<"\n";
                if(A[i][0]!=0){
                    for(int j=0;j<A[i][0];j++){
                        if(visited[A[i][j+1]]!=true){
                            visited[A[i][j+1]]=true;
                            que.push(A[i][j+1]);
                        }
                    }
                }
            }
            cnt++;
        }
        if(judge[e]==-1){
            std::cout<<"no\n";
        }else std::cout<<"yes\n";
    }

    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 assigncolor()
{
	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++);
	}
}

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 <cmath>
#include <vector>
#include <stack>
#include <algorithm>

using namespace std;

#define ll long long
#define vi vector<int>
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)

#define NIL -1

int N,M,Q,s,t;
vi T[100005], Color;

void dfs(int r, int c){
    stack<int> S;
    S.push(r);
    Color[r] = c;
    while(!S.empty()){
        int u = S.top(); S.pop();
        rep(i, T[u].size()){
            int v = T[u][i];
            if(Color[v] == NIL){ //色が違っていたらスタックに入れて探索する
                Color[v] = c;
                S.push(v);
            }
        }
    }
}

void AssignColor(){
    int id = 1;

    Color.resize(N,NIL);

    rep(u,N){ //色が違っていたらスタックに入れて探索する
        if(Color[u] == NIL) dfs(u,id++);
    }
}

void solve(int s, int t){
    if(Color[s] == Color[t]) cout << "yes" << endl;
    else cout << "no" << endl;
}

int main(){
    cin >> N >> M;

    rep(i,M){
        cin >> s >> t;
        T[s].push_back(t);
        T[t].push_back(s);
    };

    AssignColor();

    cin >> Q;

    rep(i,Q){
        cin >> s >> t;
        solve(s, t);
    };
}