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<stdio.h>
#include<queue>

using namespace std;

const int MAX = 100000;

int parent(int table[], int child) {
	queue<int> q;

	while (table[child] != -1) {
		q.push(child);
		child = table[child];
	}
	while (!q.empty()) {
		table[q.front()] = child;
		q.pop();
	}
	return child;
}

int main() {
	int n, m, q;
	int table[MAX];

	scanf("%d %d", &n, &m);

	for (int i = 0; i < n; i++)table[i] = -1;
	for (int i = 0; i < m; i++) {
		int s, t;
		scanf("%d %d", &s, &t);
		
		int ps = parent(table, s);
		int pt = parent(table, t);
		if (ps != pt) {
			table[ps] = pt;
		}
	}

	scanf("%d", &q);

	for (int i = 0; i < q; i++) {
		int s, t;
		
		scanf("%d %d", &s, &t);

		int ps = parent(table, s);
		int pt = parent(table, t);

		if (ps == pt) {
			printf("yes\n");
		}
		else {
			printf("no\n");
		}
	}
	return 0;
}

C++: #include<cstdio>
#include<vector>
#define N 100000
using namespace std;

vector<int> G[N];
/*
??£??\??????????????????????????°???????????¨??????????????£????????????????????????DFS??§????????????

 */
int labels[N];

void dfs_init(){
    int i;
    for(i=0;i<N; i++)labels[i]=0;
}

/*
  ?????°?????¨??????????£?
  ??§???????????????????????????
  stack???????????°??§???????????????
 */
bool dfs(int u, int v){
    if (u == v) return true;
    labels[u] = 1;
    int i,x;
    for(i=0; i<G[u].size(); i++){
	x = G[u][i];
	dfs(x,v);
    }
    return false;
}

void dfs2(int u){
    labels[u]=1;
    int i;
    for(i=0; i<G[u].size(); i++){
	if (labels[G[u][i]]==0) dfs2(G[u][i]);
    }
}


int main(void) {
    int n,m,i,q, u,v;
    scanf("%d %d",&n,&m);
    for(i=0;i<m;i++){
	scanf("%d %d",&u,&v);
	G[u].push_back(v);
	G[v].push_back(u);
    }

    
    scanf("%d",&q);
    for(i=0;i<q;i++){
	scanf("%d %d", &u, &v);
	dfs_init();
	dfs2(u);
	if(labels[u]>0 && labels[u]==labels[v])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 <cstdio>
#include <vector>

#define MAX 100000

#define WHITE 0
#define GRAY 1
#define BLACK 2


using namespace std;

vector<int> G[MAX];
int graphc[MAX];
int color[MAX];
int colorCount;

void def(int o)
{
	int v;

	for (int i = 0; i < G[o].size(); i++) {

		v = G[o][i];
		if (color[v] == WHITE) {

			color[v] = GRAY;
			def(v);
		}
	}
	color[o] = BLACK;
	graphc[o] = colorCount;
}


int main(void)
{
	int n;
	int m;
	int s;
	int t;
	int q;


	cin >> n >> m;

	for (int i = 0; i < m; i++) {
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}

	for (int i = 0; i < n; i++) {

		if (color[i] == WHITE) {
			colorCount++;
			def(i);
			
		}
	}


	for (int i = 0; i < n; i++) {
		cout << graphc[i] << endl;
	}

	cin >> q;

	for (int i = 0; i < q; i++) {

		cin >> s >> t;

		cout << ((graphc[s] == graphc[t]) ? "yes" : "no" )<< endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

vector<vector<int>> v;
vector<int> d;

int lx = 1;
void bfs(int x){
  queue<int> q;
  q.push(x);
  d[x] = lx;

  while(!q.empty()){
    int node = q.front();
    q.pop();

    for(int i=0; i<v[node].size(); ++i){
      if(d[v[node][i]] == 0){
         q.push(v[node][i]);
         d[v[node][i]] = lx;
       }
    }
  }
  ++lx;
}

int main(){
  int n, m; cin >> n >> m;

  v.resize(n);
  d.resize(n);

  for(int i=0; i<m; ++i){
    int x, y; cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }

  for(int i=0; i<n; ++i) if(d[i] == 0) bfs(i);

  int q; cin >> q;
  while(q--){
    int x, y; cin >> x >> y;
    if(d[x] == d[y]) cout << "yes" << endl;
    else cout << "no" << endl;
  }
}

C++: #include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>

void numbering(int id, int *g, int num, std::vector<int> *a){
    int tmp;
    for(int i=0;i<a[id].size();i++){
        tmp = a[id][i];
        if(g[tmp])continue;
        g[tmp] = num;
        numbering(tmp, g, num, a);
    }
}

void grouping(std::vector<int> *a, int *g, int n){
    int num = 1;
    for(int i=0;i<n;i++){
        if(g[i])continue;
        g[num] = num;
        numbering(i,g,num,a);
        num++;
    }
}

int main(){
    int i, n, m, q, s, t;
    int *g;
    scanf("%d %d",&n,&m);
    g = (int *)malloc(sizeof(int)*n);
    memset(g, 0, sizeof(int)*n);
    std::vector<int> a[n];
    for(i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        a[s].push_back(t);
        a[t].push_back(s);
    }
    scanf("%d",&q);
    grouping(a,g,n);
    for(i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        if(g[s]==g[t])printf("yes\n");
        else printf("no\n");
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,N) for(ll i=0;i<N;++i)
typedef long long int ll;
ll INF = 1LL<<60;

ll n,m;

struct UnionFind {
  vector< int > data;
 
  UnionFind(int sz) {
    data.assign(sz, -1);
  }
 
  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return (false);
    if(data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }
 
  int find(int k) {
    if(data[k] < 0) return (k);
    return (data[k] = find(data[k]));
  }
 
  int size(int k) {
    return (-data[find(k)]);
  }
};

int main(){
    cin >> n >> m;
    UnionFind tree(n);
    rep(i,m){
        int x,y; cin >> x >> y;
        tree.unite(x,y);
    }
    int q; cin >> q;
    rep(i,q){
        int x,y; cin >> x >> y;
        if(tree.find(x) != -1 && tree.find(y) != -1){
            if(tree.find(x) == tree.find(y)) cout << "yes" << endl;
            else cout << "no" << endl;
        }else cout << "no" << endl;
    }
}