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>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;

void solve(){
  int n, m; cin >> n >> m;
  vector<vector<int> > v(n);
  for(int i = 0; i < m; i++){
    int u, x; cin >> u >> x;
    v[u].push_back(x);
    v[x].push_back(u);
  }
  queue<int> q;
  vector<int> c(n, 0);
  int t = 1;
  for(int j = 0; j < n; j++){
    if(c[j] == 0){
      q.push(j);
      c[j] = t;
      while(q.size() != 0){
        int x = q.front();
        q.pop();
        for(int i = 0; i < v[x].size(); i++){
          if(c[v[x][i]] == 0){
            q.push(v[x][i]);
            c[v[x][i]] = t;
          }
        }
      }
    }
    t++;
  }
  int qu; cin >> qu;
  vector<string> ans(qu);
  for(int i = 0; i < qu; i++){
    int a, b; cin >> a >> b;
    if(c[a] == c[b]) ans[i] = "yes";
    else ans[i] = "no";
  }
  for(int i = 0; i < qu; i++){
    cout << ans[i] << endl;
  }
  return;
}

int main(){
  solve();
  return 0;
}

C++: #include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<queue>
#include<stack>
#include<stdio.h>
#include<vector>
#include<set>
#include<map>
#include<array>
#include<iomanip>
#define rep(i,n) for(int i=0;i<n;i++)
typedef int long long ll;

using namespace std;

static const int MAX=1000000;
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;
    rep(i,n)color[i]=nil;
    rep(i,n){
        if(color[i]==nil){
            dfs(i,id);
            id++;
        }
    }
    
    
}

int main(){
    int s,t,m,q;
    cin>>n>>m;
    rep(i,m){
        cin>>s>>t;
        g[s].push_back(t);
        g[t].push_back(s);
    }
    assignColor();
    
    cin>>q;
    
    rep(i,q){
        cin>>s>>t;
        if(color[s]==color[t]){
            cout<<"yes"<<endl;
        }else{
            cout<<"no"<<endl;
        }
    }
  /*  rep(i,n){
        cout<<i<<" "<<color[i]<<endl;
    }*/
    

return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
struct UnionFind{
  int n;
  vector<int> p,r;
  UnionFind(){}
  UnionFind(int n):n(n){init();}
  void init(){
    p.clear();
    r.clear();
    p.resize(n);
    r.resize(n,1);
    for(int i=0;i<n;i++) p[i]=i;
  }
  int find(int x){
    return (p[x]==x?x:p[x]=find(p[x]));
  }
  bool same(int x,int y){
    x=find(x);y=find(y);
    return x==y;
  }
  void unite(int x,int y){
    x=find(x);y=find(y);
    if(x==y) return;
    if(r[x]<r[y]) swap(x,y);
    p[y]=x;
    r[x]+=r[y];
  }
};
signed main(){
  int n,m;
  cin>>n>>m;
  UnionFind uf(n);
  for(int i=0;i<m;i++){
    int s,t;
    cin>>s>>t;
    uf.unite(s,t);
  }
  int q;
  cin>>q;
  for(int i=0;i<q;i++){
    int s,t;
    cin>>s>>t;
    cout<<(uf.same(s,t)?"yes":"no")<<endl;
  } 
  return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
queue<int>Q; int x[200000], n, m, q, s, t; vector<int>a[100000];

bool bfs(int A,int B) {
	memset(x, 0, sizeof(x));
	x[A] = 1;
	while (!Q.empty()) {
		for (int k = 0; k < a[Q.front()].size(); k++) {
			if (x[a[Q.front()][k]] == 0) { Q.push(a[Q.front()][k]); x[a[Q.front()][k]] = 1; }
		}
	}
	if (x[B] == 1) {
		return true;
	}
	else {
		return false;
	}
}

int main() {
	cin >> n >> m >> q;
	for (int i = 0; i < m; i++) {
		cin >> s >> t;
		a[s].push_back(t);
		a[t].push_back(s);
	}
	for (int i = 0; i < q; i++) {
		cin >> s >> t;
		if (bfs(s, t)) { 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;
    int i, u, v;
    while ( !S.empty() ) {
        u = S.top();
        S.pop();
        for ( i = 0; i < G[u].size(); i++ ) {
            v = G[u][i];
            if ( color[v] == NIL ) {
                color[v] = c;
                S.push(v);
            }
        }
    }
}
 
void assignColor() {
    int id = 0;
    int i, u;
    for ( i = 0; i < n; i++ ) color[i] = NIL;
    for ( u = 0; u < n; u++ ) {
        if ( color[u] == NIL ) dfs(u, id++);
    }
}
 
int main() {
    int s, t, m, q, i;
 
    cin >> n >> m;
 
    for ( i = 0; i < m; i++ ) {
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }
 
    assignColor();
 
    cin >> q;
 
    for ( i = 0; i < q; i++ ) {
        cin >> s >> t;
        if ( color[s] == color[t] ) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
 
    return 0;
}