Go: package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX = 100000
	NIL = -1
)

var (
	n, m, q, s, t int
	G             [MAX][]int //連結リスト
	color         [MAX]int
)

func dfs(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

//幅優先探索で、頂点を色付け
func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			dfs(i, id)
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n, m = nextInt(), nextInt()
	for i := 0; i < m; i++ {
		s, t = nextInt(), nextInt()
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)

	}
	assignColor()
	q = nextInt()
	for i := 0; i < q; i++ {
		s, t = nextInt(), nextInt()
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}

}

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