Go: package main

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

const (
	MAX = 100000
	NIL = -1
)

var (
	n     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() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		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()
	scanner.Scan()
	q, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < q; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;

bool dfs(int node, int gole, vector<vector<int> >& adjlist, vector<bool>& used){

    if(node == gole) return true;

    for(int i = 0; i < adjlist[node].size(); i++){
        if(used[adjlist[node][i]] == false){
            used[adjlist[node][i]] = true;
            return dfs(adjlist[node][i], gole, adjlist, used);
        }
    }

    return false;
}

int main(){

    int n, m; cin >> n >> m;

    vector<vector<int> > adjlist(n);               

    //入力
    for(int i = 0; i < m; i++){
        int a, b; cin >> a >> b;                   // a ←→ b に辺がある
        adjlist[a].push_back(b);
        adjlist[b].push_back(a);
        
    }

    //質問
    int q; cin >> q;
    for(int i = 0; i < q; i++){
        int a, b; cin >> a >> b;
        
        vector<bool> used(n, false);
        used[a] = true;
        if(dfs(a, b, adjlist, used)) cout << "yes" << endl;
        else cout << "no" << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
vector<int> G[100];
vector<int> A;
int isVisited[100];

bool enableConnection(int l, int o)
{
    if (isVisited[l] == 1) {
	return false;
    }
    //?¨???????????????????????????????????????????????????????
    for (int i = 0; i < G[l].size(); i++) {
	if (G[l][i] == o) {
	    return true;
	}
	if (enableConnection(G[l][i], o)) {
	    return true;
	}
    }
    return false;
}

int main()
{
    int a, c;
    cin >> a;
    cin >> c;
    //setup
    for (int i = 0; i < c; i++) {
	int n;
	int m;
	cin >> n >> m;
	G[n].push_back(m);
	isVisited[i] = 0;
    }

    //then
    int b = 0;
    cin >> b;
    for (int i = 0; i < b; i++) {
	int l;
	int o;
	cin >> l;
	cin >> o;
	if (enableConnection(l, o) || enableConnection(o, l)) {
	    cout << "yes" << endl;
	} else {
	    cout << "no" << endl;
	}
    }
    return 0;
}

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

struct node {
	int id;
	vector<int> rel;
};

int n, m, q;
node* nodes;

/* DFS * TLE! *
bool canReach(int s, int d, bool* h){
	h[s] = true;
	for(int i = 0; i < nodes[s].rel.size(); i++){
		if(nodes[s].rel[i] == d) return true;
		else if(!h[nodes[s].rel[i]]){
			if(canReach(nodes[s].rel[i], d, h)) return true;
		}
	}
	return false;
}
*/

int main() {
	scanf("%d %d", &n, &m);
	
	nodes = new node[n];
	for(int i = 0; i < n; i++){
		nodes[i].id = i;
	}
	while(m--){
		int l, r;
		scanf("%d %d", &l, &r);
		nodes[l].rel.push_back(r);
		nodes[r].rel.push_back(l);
	}
	
	scanf("%d", &q);
	while(q--){
		int l, r;
		scanf("%d %d", &l, &r);
		
		bool *h = new bool[n];
		for(int i = 0; i < n; i++){
			h[i] = false;
		}
		
		queue<int> qu;
		qu.push(l);
		while(qu.size()){
			h[qu.front()] = true;
			for(int i = 0; i < nodes[qu.front()].rel.size(); i++){
				if(nodes[qu.front()].rel[i] == r){
					puts("yes");
					goto BREAK;
				}else if(!h[nodes[qu.front()].rel[i]]){
					qu.push(nodes[qu.front()].rel[i]);
				}
			}
			qu.pop();
		}
		puts("no");
		BREAK:;
	}
	return 0;
}

C++: #include <iostream>
#include <stack>
#include <vector>
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 >> N >> 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;
		}
	}
}

C++: #include <stdio.h>
#include <stack>
#include <vector>
using namespace std;

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  vector<int> relations[n];
  int index1, index2;
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &index1, &index2);
    relations[index1].push_back(index2);
    relations[index2].push_back(index1);
  }
  int color[n], id;
  for (int i = 0; i < n; i++) {
    color[i] = -1;
  }
  id = 0;
  for (int i = 0; i < n; i++) {
    if (color[i] == -1) {
      id++;
      stack<int> S;
      S.push(i);
      while (!S.empty()) {
        int v = S.top(); S.pop();
        color[v] = id;
        for (int j = 0; j < relations[v].size(); j++) {
          int nv = relations[v][j];
          if (color[nv] == -1) {
            S.push(nv);
          }
        }
      }
    }
  }
  scanf("%d", &m);
  int u1, u2;
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &u1, &u2);
    if (color[u1] == color[u2]) printf("yes\n");
    else printf("no\n");
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int main() {

	int n, m;
	cin >> n >> m;
	vector < vector<int>> G(n + 1);

	//グラフ隣接リスト読み込み
	int a, c;
	for (int i = 1; i <= m; i++) {
		cin >> a >> c;
		G[a].push_back(c);
		G[c].push_back(a);
	}

	//幅優先探索処理
	vector<bool> seen(n + 1, false);	//発見したらtrue
	deque<int> todo;					//行く所のスタック

	int s = 1;							//今いる場所

	bool brkflg;
	vector<long> color(n + 1);
	int colornum = 0;
	for (int j = 0; j < n; j++) {		//全ての頂点を調べる

		if (color[j] == 0) {
			colornum++;
			color[j] = colornum;
			brkflg = false;
			todo.clear();

			//for (int k = 0; k <= n; k++) {
			//	seen[k] = false;//一旦全クリア
			//}

			if (seen[j] == false) {			//訪れてなかったら処理すすめる
				seen[j] = true;
				todo.push_back(j);

				while (todo.empty() != true) {				//キューが空になるまで処理	
					s = todo.front();						//処理番号を指定
					int tugiflg = false;					//次があったらtrueにするフラグ
					for (int i = 0; i < G[s].size(); i++) {

						if (seen[G[s][i]] != true) {
							seen[G[s][i]] = true;
							color[G[s][i]] = colornum;
							todo.push_back(G[s][i]);
							tugiflg = true;
						}
					}
					todo.pop_front(); //次を全部探したので削除する
				}
			}
		}
	}

	int in1, in2;
	cin >> m;
	for (int i = 0; i <m; i++) {
		cin >> in1 >> in2;
		if (color[in1] == color[in2]) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}

	return 0;

}