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

using namespace std;

#define NMAX 100000
#define NIL -1

int n;
stack<int> S;
vector<int> adj[NMAX + 1]; // 0-origin
int d[NMAX + 1];           // if visited
int c[NMAX + 1];           // color

int next_unvisited() {
    for (int i = 0; i < n; i++) {
        if (d[i] == 0)
            return i;
    }
    return NIL;
}

int next_unvisited_adj(int u) {
    vector<int>::iterator it;
    for (it = adj[u].begin(); it != adj[u].end(); it++) {
        if (d[*it] == 0)
            return *it;
    }
    return NIL;
}

void dfs() {
    int x; // start vertex;
    int round = 0;
    while ((x = next_unvisited()) != NIL) {
        d[x] = 1;
        c[x] = round;
        S.push(x);
        while (!S.empty()) {
            int u = S.top();
            int v = next_unvisited_adj(u);
            if (v != NIL) {
                if (d[v] == 0) {
                    d[v] = 1;
                    c[v] = round;
                    S.push(v);
                }
            } else {
                S.pop();
            }
        }
        round++;
    }
}

int main() {
    int m, q, u, v;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < m; i++) {
        scanf("%d %d", &u, &v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i = 0; i < n; i++)
        d[i] = 0;
    dfs();
    scanf("%d", &q);
    for (int i = 0; i < q; i++) {
        scanf("%d %d", &u, &v);
        if (c[u] == c[v])
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_A
// Graph I - Connected Components

#define SUBMIT

#include <utility>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <limits>
#include <numeric>
#include <functional>
#include <set>
#include <cmath>
#include <cstring>
#include <climits>
#include <memory>

using namespace std;
using ui64 = unsigned long long;
using i64 = long long;

const int NIL = -1;
const ui64 MAX_N = 100000;

vector<int> adj[MAX_N];
int assigned_group_ids[MAX_N];
int n, m, q;
int group_id;

void dfs(int u, int id) {
    assigned_group_ids[u] = id;
    // search adj nodes
    for (const auto& v : adj[u]) {
        if (assigned_group_ids[v] == NIL) dfs(v, id);
    }
}

void assign_group() {
    memset(assigned_group_ids, NIL, sizeof(assigned_group_ids));
    group_id = 0;

    for (int u = 0; u < n; ++u) {
        if (assigned_group_ids[u] == NIL) {
            dfs(u, group_id);
            group_id++;
        }
    }
}

int main() {
    memset(adj, 0, sizeof(adj));
    cin >> n >> m;

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

    assign_group();

    cin >> q;

    for (int i = 0; i < q; ++i) {
        int s, t;
        cin >> s >> t;
        if (assigned_group_ids[s] == assigned_group_ids[t]) cout << "yes" << endl;
        else cout << "no" << endl;
    }

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
const int MAX = 100000;
const int NIL = -1;
int n;
vector<int> G[MAX];
int color[MAX];
void dfs(int r,int c) {
    if(color[r] == NIL) color[r] = c;
    for(int i = 0;i < G[r].size();i++) {
        int v = G[r][i];
        if(color[v] == NIL) dfs(v,c);
    }
}
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;
    scanf("%d%d",&n,&m);
    for(int i = 0;i < m;++i) {
        scanf("%d%d",&s,&t);
        G[s].push_back(t);
        G[t].push_back(s);
    }
    assignColor();
    scanf("%d",&q);
    for(int i = 0;i < q;++i) {
        scanf("%d%d",&s,&t);
        if(color[s] == color[t]) 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 >> s >> 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 <vector>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
using namespace std;

const int N = 100000;
int n, m, q;
vector<int> M[N];
bool V[N];

int bfs(int u, int v) {
  int s;
  queue<int> q;
  REP(i, n) V[i] = false;

  s = u;
  q.push(s);
  V[s] = true;

  while (!q.empty() && !V[v]) {
    s = q.front(); q.pop();
    REP(i, M[s].size()) {
      int t = M[s][i];
      if (!V[t]) {
        q.push(t);
        V[t] = true;
      }
    }
  }

  return V[v];
}

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

  cin >> n >> m;
  REP(i, m) {
    int u, v;
    cin >> u >> v;
    M[u].push_back(v);
    M[v].push_back(u);
  }

  cin >> q;
  REP(i, q) {
    int u, v;
    string answer;
    cin >> u >> v;
    answer = bfs(u, v) ? "yes" : "no";
    cout << answer << endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <queue>
static const int MAX=100000;
using namespace std;
vector<int> follow[MAX];
int color[MAX];  //-1 represent No Visitedvoid 
bfs(int i,int c){    queue<int> Q;    Q.push(i);color[i]=c;    
while(!Q.empty())  
  {        int p=Q.front();Q.pop();        for(int j=0;j<follow[p].size();j++)        {            if(color[follow[p][j]]!=-1) continue;            //else            Q.push(follow[p][j]); color[follow[p][j]]=color[p];        }    }}
int main(){   int n,m;   cin>>n>>m;   for(int i=0;i<n;i++) color[i]=-1;   for(int i=0;i<m;i++)   {       int a,b;       cin>>a>>b;       follow[a].push_back(b);       follow[b].push_back(a);   }   int c=0;   for(int i=0;i<n;i++)   {       if(color[i]==-1)       {           bfs(i, c);           c++;       }   }   int order_num;cin>>order_num;   for(int i=0;i< order_num;i++)   {       int a,b;       cin>>a>>b;       if(color[a]==color[b]) cout<<"yes"<<endl;       else cout<<"no"<<endl;   }}