Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = []int{}
		group[i] = -1
	}
	
	fmt.Scan(&m)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < m; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		
		g[s] = append(g[s], t)
		g[t] = append(g[t], s)
	}
	for i := 0; i < n; i++ {
		if group[i] != -1 {
			continue
		}
		q := []int{i}
		for len(q) > 0 {
			pop := q[0]
			if len(q) > 1 {
				q = q[1:]
			} else {
				q = []int{}
			}
			if group[pop] == -1 {
				q = append(q, g[pop]...)
				group[pop] = i
			}
		}
	}
	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if group[s] == group[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

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