Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	for i := 0; i < n; i++ {
		group[i] = i
	}
	
	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())

		if group[s] < group[t] {
			group[t] = group[s]
		} else {
			group[s] = group[t]
		}
	}

	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 root(s, &group) == root(t, &group) {
			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 <set>
#include <vector>
#include <queue>
#include <stack>
#include <utility>
#include <algorithm>

using namespace std;

typedef long long signed int ll;

constexpr ll NIL = -20000000000;

int n,m,q,s,t,z[100000];
vector<pair<int,int>> w;

void dfs(int n,int c) {
    z[n] = c;
    auto r = lower_bound(w.begin(),w.end(),make_pair(n,0));
    auto l = lower_bound(w.begin(),w.end(),make_pair(n+1,0));
    for(auto itr = r;itr != l;itr++) if(z[itr->second] != c)dfs(itr->second,c);
}

int main() {
    for(int i = 0;i < 100000;i++)z[i] = -1;
    cin >> n >> m;
    for(int i = 0;i < m;i++) {
        cin >> s >> t;
        w.push_back(make_pair(s,t));
        w.push_back(make_pair(t,s));
    }
    sort(w.begin(),w.end());
    cin >> q;
    int cc = 1;
    for(int i = 0;i < q;i++) {
        cin >> s >> t;
        if(z[s] == -1) dfs(s,cc++);
        if(z[s] == z[t]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
}

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

bool isFriend(std::vector<int> *a, int s, int t, int *checked, int c){
    int tmp;
    checked[s] = c;
    if(s==t)return true;
    for(int i=0;i<a[s].size();i++){
        tmp = a[s][i];
        if(checked[tmp]>=c)continue;
        if(isFriend(a,tmp,t,checked,c))return true;
    }
    return false;
}

int main(){
    int i, n, m, q, s, t;
    int *checked;
    scanf("%d %d",&n,&m);
    checked = (int *)malloc(sizeof(int)*n);
    memset(checked, 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);
    for(i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        if(isFriend(a,s,t,checked,i+1))printf("yes\n");
        else printf("no\n");
    }
    return 0;
}

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

int n;
vector<int> G[100001];
int vis[100001];
int re[100001];

void dfs( int u, int key){
    if(vis[u]) return;
    re[u] =key;
    vis[u] = 1;
    int num = G[u].size();
    for( int i = 0; i < num; i++ ){
        int v = G[u][i];
        dfs(v,key);
    }
}


int main(){
    int i,j,k,m;
    scanf("%d %d",&n,&m);
    while(m--){
        cin >> j >> k;
        G[j].push_back(k);
        G[k].push_back(j);
    }
    for( i = 0; i < n; i++ ){
        dfs(i,i+1);
    }
    int q;
    cin >> q;
    for( i = 0; i < q; i++ ){
        cin >> j >> k;
        cout << ((re[j]==re[k])? "yes":"no") << endl;
    }
}

C++: /*
 * ALDS1_11_D.cpp
 *
 *  Created on: May 8, 2018
 *      Author: 13743
 */

#include<iostream>
#include<vector>

using namespace std;

int visited[100000];
int la = 1;

void dfsRec(vector<int>* a, int v) {
	if(visited[v] != 0) return;
	visited[v] = la;
	for(int &x:a[v]) {
		dfsRec(a, x);
	}
}

int main() {
	int n, m;
	vector<int> adj[100000];

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

	for(int i=0; i<n; i++) {
		if(visited[i] == 0) {
			dfsRec(adj, i);
			la++;
		}
	}

	int q;
	cin >> q;
	while(q--) {
		cin >> u >> v;
		(visited[u]==visited[v]) ? printf("yes\n") : printf("no\n");
	}
}

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

bool DFS(const vector<vector<int> >& G,
         vector<bool>& is_visited, int a, int b, int i) {
    is_visited[a] = true;
    if (a == b) return true;
    if (i >= G[a].size()) return false;
    if (!is_visited[G[a][i]] && DFS(G, is_visited, G[a][i], b, 0)) {
        return true;
    }
    return DFS(G, is_visited, a, b, i + 1);
}

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

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

// for (int i = 0; i < n; i++) {
//     cout << i << " : ";
//     for (int j = 0; j < G[i].size(); j++){
//         cout << G[i][j] << " ";
//     }
//     cout << endl;
// }
//


    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        vector<bool> is_visited(n, false);
        if (DFS(G, is_visited, a, b, 0)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v){
  const char* dlm = "";
  for(const auto& x:v) os << exchange(dlm, " ") << x;
  return os;
}
struct UnionFind{
  using val_t=size_t;
  vector<val_t> par;
  explicit UnionFind(size_t n): par(n) {
    iota(begin(par),end(par),0);
  }
  void connect(val_t s, val_t t){
    // par[root(s)]=t;
    par[root(s)]=root(t);
  }
  val_t root(val_t s){
    if(par[s]!=s) par[s]=root(par[s]);
    return par[s];
  }
  bool visit(val_t s, val_t t){
    return root(s)==root(t);
  }
};
int32_t main(){
  size_t n,m; cin>>n>>m;
  UnionFind tree(n);
  size_t s,t;
  // cout<<tree.par<<endl;;
  for(size_t i=0; i<m; ++i){
    cin>>s>>t;
    tree.connect(s,t);
  }
  // cout<<tree.par<<endl;
  size_t q; cin>>q;
  for(size_t i=0; i<q; ++i){
    cin>>s>>t;
    cout<<(tree.visit(s,t)?"yes\n":"no\n");
  }
}

C++: #include <iostream>
#include <vector>
#define eol '\n'
using namespace std;

vector<int> visit;

bool bfs(int s, int t, vector< vector<int> > list) {
  if (s == t) {
    return true;
  }
  
  // visit flag
  visit[s] = 1;
  
  for (int i = 0; i < list[s].size(); i++) {
    int next = list[s][i];
    if (visit[next] == 0) {
      if (bfs(next, t, list)) {
        return true;
      }
    }
  }
  return false;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n,m,s,t,q;
  cin >> n >> m;

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

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    visit = vector<int>();
    if (bfs(s,t,list)) {
      cout << "yes" << eol;
    } else {
      cout << "no" << eol;
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define r(i,n) for(auto i=0;i<n;i++)
#define s(c) static_cast<int>((c).size())

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

	set<int> A[n+1];
	int v1, v2;
	r(i, m){
		cin >> v1 >> v2;

		A[v1].insert(v2);
		A[v2].insert(v1);
	}

	//初期の未到達状態は0
	//必要に応じて連結成分ごとに1から順に分類
	int group[n]{};
	stack<int> s;

	int q, groupNum = 0, gn1, gn2;
	cin >> q;
	r(i, q){
		cin >> v1 >> v2;

		if(group[v1] == 0 && group[v2] == 0){
			groupNum++;

			s.push(v1);
			group[v1] = groupNum;

			while(!s.empty()){
				int v = s.top();
				int size = s(A[v]);
				for(int tmp : A[v]){
					if(!group[tmp]){
						s.push(tmp);
						group[tmp] = groupNum;
					}else{
						size--;
					}
				}
				if(!size)
					s.pop();
			}
		}

		gn1 = group[v1];
		gn2 = group[v2];
		//printf("group[%d]=%d, group[%d]=%d: ", v1, gn1, v2, gn2);
		printf(gn1 == gn2 ? "yes\n" : "no\n");
	}

	return 0;
}