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>

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int n, m;
struct node {
    int parent;
    vector<int> child;
};

int main() {
    cin >> n >> m;
    node person[n];
    REP(i, n)
        person[i].parent = -1;
    REP(i, m){
        int s, t;
        cin >> s >> t;
        if(person[s].parent == -1 && person[t].parent == -1){
            person[s].parent = s;
            person[t].parent = s;
            person[s].child.push_back(t);
        } else if(person[s].parent == -1){
            person[s].parent = person[t].parent;
            person[t].child.push_back(s);
        } else if(person[t].parent == -1){
            person[t].parent = person[s].parent;
            person[s].child.push_back(t);
        } else {
            person[t].parent = person[s].parent;
            REP(j, person[t].child.size()){
                person[person[t].child[j]].parent = person[s].parent;
                person[person[s].parent].child.push_back(person[t].child[j]);
            }
            person[t].child.clear();
        }
    }
    int q;
    cin >> q;
    REP(i, q){
        int s, t;
        cin >> s >> t;
        if(person[s].parent != -1 && person[t].parent != -1  && person[s].parent == person[t].parent)
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;

int n, m;
vector<int> color(MAX, NIL);
vector<vector<int>> M(MAX);

void dfs(int i, int id) {
  int size = M[i].size();
  for (int j = 0; j < size; j++) {
    if (color[M[i][j]] == NIL) {
      color[M[i][j]] = id;
      dfs(M[i][j], id);
    }
  }
}

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

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

  for (int i = 0; i < m; i++) {
    int id, f;
    cin >> id >> f;
    M[id].push_back(f);
    M[f].push_back(id);
  }
  int qn;
  cin >> qn;
  vector<vector<int>> q(qn, vector<int>(2));
  for (int i = 0; i < qn; i++) {
    cin >> q[i][0] >> q[i][1];
  }

  assignColor();

  for (int i = 0; i < qn; i++) {
    if (color[q[i][0]] == color[q[i][1]]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
using UL = unsigned int;
using ULL = unsigned long long;
using LL = long long;
#define rep(i,n) for(UL i=0; i<(n); i++)

vector<UL> E[100000];
UL D[100000];

int main() {
 UL N,M; scanf("%u%u",&N,&M);
 rep(i,M){
  UL u,v; scanf("%u%u",&u,&v);
  E[u].push_back(v);
  E[v].push_back(u);
 }
 rep(i,N) D[i]=~0u;
 rep(s,N){
  if(D[s]!=~0u) continue;
  queue<UL> Q; Q.push(s); D[s]=s;
  while(Q.size()){
   UL p=Q.front(); Q.pop();
   for(UL e:E[p]){
    if(D[e]!=~0u) continue;
    D[e]=D[p];
    Q.push(e);
   }
  }
 }
 UL Q; scanf("%u",&Q);
 rep(q,Q){
  UL s,t; scanf("%u%u",&s,&t);
  if(D[s]==D[t]) printf("yes\n");
  else printf("no\n");
 }
 return 0;
}

C++: 
#include<stdio.h>
#define Num 100000
 
int a[Num];
int search(int c){
   if(c == a[c])
      return c;
   return a[c] = search(a[c]);
 }
int main(){
   int i,x,y,n,m,z;
   for(i=0;i<Num;i++){
      a[i] = i;
   }
   scanf("%d %d",&x,&y);
   for(i=0;i<y;i++){
      scanf("%d %d", &n,&m);
      if(search(n) != search(m)){
         a[search(m)] = search(n);
      }
   }
   scanf("%d",&z);
   for(i=0;i<z;i++){
      scanf("%d %d",&n,&m);
      if(search(n) == search(m))
         printf("yes\n");
      else
         printf("no\n");
   }
 
   return 0;
}

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

int group[N],nt[N];
vector<int> vec[N];
int n,m;

int dfs_visit(int x,int k){
  stack<int> S;
  S.push(x);
  while(!S.empty()){
    int u=S.top();S.pop();
    for(int i=0;i<vec[u].size();i++){
      int v=vec[u][i];
      if(group[v]==0){
        group[v]=k;
        S.push(v);
      }
    }
  }
}
void dfs(){
  int id=1;
  for(int i=0;i<n;i++){
    if(group[i]==0) dfs_visit(i,id++);
  }
}
int main(){
  cin>>n>>m;
  for(int i=0;i<m;i++){
    int s,t;
    cin>>s>>t;
    vec[s].push_back(t);
    vec[t].push_back(s);
  }
  dfs();
  int q;
  cin>>q;
  for(int i=0;i<q;i++){
    int a,b;
    cin>>a>>b;
    if(group[a]==group[b]){
      cout<<"yes"<<"\n";
    }else{
      cout<<"no"<<"\n";
    }
  }
}

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

int main(){
  int m,s,t,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++){
  color[i]=NIL;
}

  cin>>q;

  for(int u=0;u<n;u++){
    int id=1;
    if(color[u]==NIL){
      dfs(u,id++);
    }
  }

  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 <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

const int INF = 1 << 30;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 10000;

vector<int> G[MAX];
int T[MAX];
int A[N], B[N];

int find_next(int i, int n) {
	int ret = -1;
	for (int j = i; j < n; j++) {
		if (T[j]) continue;
		ret = j;
		break;
	}
	return ret;
}

int main() {
	int n, m; cin >> n >> m;
	rep(i, m) {
		int s, t; cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
		T[i] = 0;
	}
	int q; cin >> q;
	rep(i, q) {
		int a, b; cin >> a >> b;
		A[i] = a; B[i] = b;
	}

	queue<int> Q;
	int next = 0, type = 0;
	while (true) {
		if (Q.empty()) {
			int i = find_next(next, n);
			if (i == -1) break;
			type++;
			T[i] = type;
			Q.push(i);
			continue;
		}
		int i = Q.front(); Q.pop();
		for (auto j : G[i]) {
			if (T[j]) continue;
			T[j] = type;
			Q.push(j);
		}
	}
	rep(i, q) {
		if (T[A[i]] == T[B[i]]) cout << "yes" << endl;
		else cout << "no" << endl;
	}
}