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

struct UnionFind{
    vector<int> parent;
    UnionFind(int size): parent(size, -1) {}
    
    int root(int x){
        return parent[x]<0? x: parent[x] = root(parent[x]);
    }
    void uniteSet(int x, int y){
        x = root(x);
        y = root(y);
        if (x==y) return;
        if (parent[x]>parent[y]) swap(x,y);
        //parent[x] += parent[y];
        parent[y] = x;
    }
    bool findSet(int x, int y){
        return root(x) == root(y);
    }
};

int main(){cin.tie(0);ios::sync_with_stdio(false);
    
    int nb_v, nb_lines;
    cin >>nb_v
        >>nb_lines;
    UnionFind G(nb_v);
    for(int i=0; i<nb_lines; ++i){
        int v1, v2;
        cin >>v1
            >>v2;
        G.uniteSet(v1,v2);
    }

    cin >>nb_lines;
    for(int i=0; i<nb_lines; ++i){
        int v1, v2;
        cin >>v1
            >>v2;
        cout<<(G.findSet(v1, v2)? "yes\n":"no\n");
    }    
    for(auto& a:G.parent) cout<<a<<" ";
    
}

C++: #include <iostream>
#include <vector>
#include <stack>
using namespace std;
const int MAX=100000;
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[s].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;
    }
}

C++: #include<stdio.h>
#include<vector>
#include<string>
using namespace std;

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

vector<bool>visited;
vector<int>color;
void dfs(int s){
    for(int i=0;i<g[s].size();i++){
        if(color[g[s][i]]==-1){
            color[g[s][i]]=color[s];
            dfs(g[s][i]);
        }
    }
}
bool dfs2(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(dfs2(g[s][i],t)){
                return true;
            }
        }
    }
    return false;
}
int main() {
    scanf("%d %d",&n,&m);
    g.resize(n);
    color.resize(n,-1);
    for(int i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        g[s].push_back(t);
        g[t].push_back(s);
    }
    for(int i=0;i<n;i++){
        if(color[i]==-1){
            color[i]=i;
            dfs(i);
        }
    }
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        string s1=(dfs2(s,t)?"yes":"no");
        string s2=(color[s]==color[t]?"yes":"no");
        if(s1!=s2)return 1;
        printf((color[s]==color[t]?"yes\n":"no\n"));
    }
    return 0;
}

C++: #include <cstdio>
#include <vector>
#include <queue>
using namespace std;

int main()
{
  int n, m, v1, v2;
  scanf("%d%d", &n, &m);
  vector<int> v[100000];

  for (int i = 0; i < m; i++)
  {
    scanf("%d%d", &v1, &v2);
    v[v1].push_back(v2);
    v[v2].push_back(v1);
  }

  int group[n];
  for (int i = 0; i < n; i++)
    group[i] = -1;

  for (int i = 0, id = 0; i < n; i++, id++)
  {
    queue<int> que;
    if (group[i] == -1)
      que.push(i);
    while (!que.empty())
    {
      int x = que.front();
      que.pop();
      group[x] = id;
      for (int j = 0; j < v[x].size(); j++)
        if (group[v[x][j]] == -1)
          que.push(v[x][j]);
    }
  }

  int q;
  scanf("%d", &q);

  for (int i = 0; i < q; i++)
  {
    scanf("%d%d", &v1, &v2);
    if (group[v1] != -1 && group[v1] == group[v2])
      printf("yes\n");
    else
      printf("no\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 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<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 ( 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;
    
    cin >> n >> m;
    
    for ( i = 0; i < m; i++ ){
        con >> s >> t;
        G.push_back(t);
        G.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;
}