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 <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;

const int maxn = 100000 + 5;
int color[maxn];
bool vis[maxn];
vector <int> G[maxn];

void dfs(int u, int c)
{
    vis[u] = true;
    color[u] = c;

    for (int i = 0; i < G[u].size(); i++)
        if (!vis[G[u][i]])
        dfs(G[u][i], c);
}

int main()
{
    int n, m;
    while (scanf("%d %d", &n, &m) != EOF)
    {
        for (int i = 0; i < n; i++)
            G[i].clear();

        int u, v;
        for (int i = 0; i < m; i++)
        {
            scanf("%d %d", &u, &v);
            G[u].push_back(v);
            G[v].push_back(u);
        }

        memset(color, 0, sizeof(color));
        memset(vis, false, sizeof(vis));

        int c = 0;
        for (int i = 0; i < n; i++)
        {
            if (!vis[i])
            {
                ++c;
                dfs(i, c);
            }
        }

        int q;
        scanf("%d", &q);
        while (q--)
        {
            int u, v;
            scanf("%d %d", &u, &v);
            if (color[u] == color[v])
                printf("yes\n");
            else
                printf("no\n");
        }
    }
    return 0;
}

C++: #include <iostream>
#include <stack>
using namespace std;

const int N = 10000;
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
const int NIL = -1;

int n, M[N][N];
int color[N], cc[N], cp = 0;
int nt[N];

int next(int u){
	for (int v = nt[u]; v <= n; v++){
		nt[u] = v + 1;
		if (M[u][v]) return v;
	}
	return NIL;
}

void dfs_visit(int r){
	for (int i = 0; i < n; i++) nt[i] = 0;

	stack<int> S;
	S.push(r);
	color[r] = GRAY;
	cc[r] = cp;

	while (!S.empty()){
		int u = S.top();
		int v = next(u);
		if (v != NIL){
			if (color[v] == WHITE){
				color[v] = GRAY;
				cc[v] = cp;
				S.push(v);
			}
		}
		else {
			S.pop();
			color[u] = BLACK;
		}
	}
	cp++;
}

void dfs(){
	for (int i = 0; i < n; i++){
		color[i] = WHITE;
		nt[i] = 0;
	}

	for (int u = 0; u < n; u++){
		if (color[u] == WHITE) dfs_visit(u);
	}

}

int main(){
	int m, q;
	cin >> n >> m;
	for (int i = 0; i < n; i++){
		for (int j = 0; j < n; j++) M[i][j] = 0;
	}
	for (int i = 0; i < m; i++){
		int s, t;
		cin >> s >> t;
		M[s][t] = M[t][s] = 1;
	}

	dfs();

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

	return 0;
}

C++: //グラフは隣接リスト表現で扱う。
//メモリ量O(E), 計算量O(V+E) (Vは頂点, Eは辺の数)
//辺が少ない疎なグラフで有利。

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

#define MAX 105000
#define 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 bfs(int r, int c){
    //最初だけ別にpushしてやる
    queue<int> Q;
    Q.push(r);
    color[r] = c;

    while(!(Q.empty())){
        int u = Q.front(); Q.pop();
        for(int i=0; i<G[u].size(); i++){
            int v = G[u][i];
            if(color[v] == NIL){
                color[v] = c;
                Q.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++);
        if(color[u] == NIL) bfs(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;
        }
    }
    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++){
    int v=g[u][i];
    if(color[v]==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;
     }
  }
  return 0;
}

C++: #include<iostream>
#include<climits>
#include<vector>
#include<list>
#include<functional>
#include<algorithm>
#include<string>
#include<cmath>
#include<complex>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<tuple>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF (int)1e9
#define INFL (ll)1e11
using namespace std;
typedef long long ll;

typedef pair<int, int> P;

void dfs(int s, int id, vector<vector<int>> a, vector<int> &comp) {
	stack<int> st;
	st.push(s);
	comp[s] = id;
	while (!st.empty()) {
		int i = st.top();
		int k = a[i].size();
		REP(j, k + 1) {
			if (j < k) {
				int t = a[i][j];
				if (comp[t] != id) {
					comp[t] = id;
					st.push(t);
					break;
				}
			}
			else st.pop();
		}
	}
}

void divide(int size, vector<vector<int>> a, vector<int> &comp) {
	int s = 0;
	int id = 1;
	while (1) {
		dfs(s, id, a, comp);
		while (s < size && comp[s]>0) s++;
		if (s == size) break;
		id++;
	}
	return;
}

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

	int n, m;
	scanf("%d %d", &n, &m);
	//cin >> n >> m;
	vector<vector<int>> a(n);
	REP(i, m) {
		int s, t;
		scanf("%d %d", &s, &t);
		//cin >> s >> t;
		a[s].push_back(t);
		a[t].push_back(s);
	}
	int q;
	cin >> q;
	vector<int> comp(n, 0);
	divide(n, a, comp);
	REP(i, q) {
		int s, t;
		scanf("%d %d", &s, &t);
		//cin >> s >> t;
		if (comp[s] == comp[t]) printf("yes\n");
		else printf("no\n");
	}

	return 0;
}