Go: package main

import (
	"fmt"
)

var n, m int
var g [][]int
var color []int

func main() {
	fmt.Scan(&n, &m)
	g = make([][]int, n) // SNSの関係を表すグラフ
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		g[a] = append(g[a], b) // 無向重みなしグラフ
		g[b] = append(g[b], a)
	}
	color = make([]int, n) // 各ノードにつける色
	for i := 0; i < len(color); i++ {
		color[i] = -1 // 初期化しておく
	}
	type query struct {
		s, t int
	}
	var Q int
	fmt.Scan(&Q)
	qs := make([]query, Q)
	for i := 0; i < Q; i++ {
		fmt.Scan(&qs[i].s, &qs[i].t)
	}
	// fmt.Println("n", n, "m", m, "g", g, "Q", Q, "qs", qs)
	// 全ての頂点からdfsする
	id := 1
	for u := 0; u < n; u++ {
		if color[u] == -1 {
			dfs(u, id)
			id++
		}
	}
	// fmt.Println("color", color)
	for i := 0; i < Q; i++ {
		if color[qs[i].s] == color[qs[i].t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func dfs(v, c int) {
	// fmt.Printf("dfs(v %d, c %d) en:\n", v, c)
	color[v] = c
	for _, vv := range g[v] {
		// fmt.Printf("  v %d, g[v] %v, vv %d\n", v, g[v], vv)
		if color[vv] == -1 {
			dfs(vv, c)
		}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define NIL -1

int c = 0,n,m;
int color[100000];
vector<int> li[100000];

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 < li[u].size();i++) {
      if(color[li[u][i]] == NIL) {
        S.push(li[u].at(i));
        color[li[u][i]] = c;
      }
    }
  }
}

void assignColor() {
  for(int i = 0;i < n;i++) {
    if(color[i] == NIL) dfs(i,c++);
  }
}



int main() {

  
  cin >> n >> m;


  for(int i = 0;i < m;i++) {
    int a,b;
    cin >> a >> b;
    li[a].push_back(b);
    li[b].push_back(a);
  }

  for(int i = 0;i < n;i++) {
    color[i] = NIL;
  }

  int q;
  cin >> q;

  assignColor();

  for(int i = 0;i < q;i++) {
    int a,b;
    cin >> a >> b;
    cout << ((color[a] == color[b]) ? "yes" : "no") << endl;
  }


}

C++: #include <stdio.h>
#define N 100000

int a[N]={};

int search(int c)
{
	if(c==a[c]) return c;
	
	return a[c]=search(a[c]);
}

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

 for(i=0;i<N;i++) a[i]=i;

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

 for(i=0;i<m;i++){

  scanf("%d %d",&s,&t);

  if(search(s)==search(t)) a[t]=s;

 }

 scanf("%d",&q);

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

  if(search(s)==search(t)) printf("yes\n");
  else printf("no\n");

 }

 return 0;
}

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

vector<int> list[100000];
int n,m;

bool bfs(int s, int t){
  bool used[m];
  for(int i=0;i<m;i++) used[i]=false;
  queue<int> Q;
  Q.push(s); used[s]=true;
  while(!Q.empty()){
    int k=Q.front(); Q.pop();
    if(k==t){return true;}
    for(int i=0;i<list[k].size();i++){
      int temp=list[k][i];
      if(!used[temp]){
        Q.push(temp); used[temp]=true;
      }
    }
  }
  return false;
}
int main(){
  cin>>n>>m;
  for(int i=0;i<m;i++){
    int u,v; cin>>u>>v;
    list[u].push_back(v);
    list[v].push_back(u);
  }
  int q; cin>>q;
  for(int i=0;i<q;i++){
    int s,t; cin>>s>>t;
    if(bfs(s, t)){
      cout<<"yes"<<"\n";
    }else{
      cout<<"no"<<"\n";
    }
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
 
using ll = long long;
using ld = long double;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n) - 1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\n'
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;}
template<class T> inline void printv(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
 
void solve();
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(numeric_limits<double>::max_digits10);
    solve();
    return 0;
}

class UnionFind {
    vector<int> parent;

    public:
        UnionFind(int size) {
            parent = vector<int>(size, -1);
        }

        int find(int x) {
            if (parent[x] < 0) {
                // parent itself
                return x;
            } else {
                return parent[x] = find(parent[x]);
            }
        }

        bool unite(int x, int y) {
            int rX, rY;
            if ((rX = find(x)) == (rY = find(y))) {
                // already united
                return false;
            }
            if (size(rX) < size(rY)) {
                int tmp = rX;
                rX = rY;
                rY = tmp;
            }
            parent[rX] += parent[rY];
            parent[rY] = rX;
            return true;
        }

        int size(int x) {
            return -parent[find(x)];
        }

        bool is_same(int x, int y) {
            return find(x) == find(y);
        }

        // CAUTION!! : This method is heavy due to scanning linearly.
        int component_count() {
            int c = 0;
            for (const int x : parent) {
                if (x < 0) {
                    c++;
                }
            }
            return c;
        }
};

void solve() {
    int n, m;
    cin >> n >> m;
    UnionFind uf(n);
    int a, b;
    REP(i, m) {
        cin >> a >> b;
        uf.unite(a, b);
    }
    int q;
    cin >> q;
    REP(i, q) {
        cin >> a >> b;
        cout << (uf.is_same(a, b) ? "yes" : "no") << endl;
    }
}

C++: #include <cstdio>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

int NIL = -1;
const int MAX = 100000;
vector<int> SNS[MAX];
int color[MAX];

void dfs(int v, int colorIdx) {

	stack<int> S;
	S.push(v);
	color[v] = colorIdx;

	while (!S.empty()) {
		int s = S.top();
		S.pop();
		for (int i = 0; i < (int) SNS[s].size(); ++i) {
			int r = SNS[s][i];
			if (color[r] == NIL) {
				S.push(r);
				color[r] = colorIdx;
			}
		}
	}
}

void makeColor(int color[], int n) {
	int colorIdx = 0;
	for (int i = 0; i < n; ++i) {
		if (color[i] == NIL)
			dfs(i, colorIdx++);
	}
}

int main() {

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

	for (int i = 0; i < m; ++i) {
		int s, t;
		scanf("%d %d", &s, &t);
		SNS[s].push_back(t);
		SNS[t].push_back(s);
	}

	for (int i = 0; i < n; ++i)
		color[i] = NIL;

	makeColor(color, n);

	int q;
	scanf("%d", &q);
	for (int i = 0; i < q; ++i) {
		int s, t;
		scanf("%d %d", &s, &t);
		if (color[s] == color[t])
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}

	return 0;
}