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 <vector>
#include <cstdio>
class union_find{
????????std::vector<int> parent;
public:
????????int root(int a){return parent[a]==a?a:(parent[a]=root(parent[a]));}
????????union_find(int n):parent(n+1){for(int i=1;i<=n;i++)parent[i]=i;}
????????int same(int a,int b){return root(a)==root(b);}
????????int unite(int a,int b){
????????????????int x=root(a),y=root(b);//if(x==y)return 0;
????????????????parent[x]=y;
????????????????return 1;
????????}
};
int main(){
????????int n,m;
????????scanf("%d%d",&n,&m);
????????union_find uf(n);
????????int a,b;
????????for(;m--;uf.unite(a,b))scanf("%d%d",&a,&b);
????????for(scanf("%d",&m);m--;puts(uf.same(a,b)?"yes":"no"))scanf("%d%d",&a,&b);
}

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;
}