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++: #ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var) \
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

struct Solver {
  explicit Solver(int n)
      : connected_(n, vector<bool>(n, false)) {}
  void register_friend(int s, int t) {
    connected_[s][t] = connected_[t][s] = true;
  }
  bool can_reach(int s, int t) {
    if (s == t)
      return true;
    vector<bool> done(connected_.size(), false);
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
      auto v = q.front();
      q.pop();
      if (v == t)
        return true;
      if (done[v])
        continue;
      done[v] = true;
      auto& con = connected_[v];
      for (size_t i = 0; i < con.size(); ++i) {
        if (con[i]) {
          auto v2 = static_cast<int>(i);
          if (v2 == t)
            return true;
          if (!done[v2]) {
            q.push(v2);
          }
        }
      }
    }
    return false;
  }

 private:
  vector<vector<bool>> connected_;
};

int main() {
  int n, m;
  cin >> n >> m;
  Solver sol(n);
  for (int i = 0; i < m; ++i) {
    int s, t;
    cin >> s >> t;
    sol.register_friend(s, t);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int s, t;
    cin >> s >> t;
    auto ok = sol.can_reach(s, t);
    cout << (ok ? "yes" : "no") << endl;
  }
}

C++: // 連結成分
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define NIL -1

vector<int> G[100005];
int color[100005];
int n,col;

void dfs(int r, int c);
void assign_number();

int main(){
    int m;
    cin >> n >> m;
    for(int i = 0; i < m; i++){
        int s,t; cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    col = 0;
    assign_number();
    
    int q;
    cin >> q;
    
    for(int i = 0; i < q; i++){
        int s,t; cin >> s >> t;
        cout << (color[s] == color[t] ? "yes" : "no") << endl;
    }
    return 0;
}

void assign_number(){
    // 未訪問の頂点を選んで探索していく
    for(int i = 0; i < n; i++){
        color[i] = NIL;
    }

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

void dfs(int r, int c){
    // 頂点rから探索しそのグループにcの番号をふる
    stack<int> s;
    s.push(r);
    color[r] = c;

    while(!s.empty()){
        int top = s.top();
        s.pop();
        int size = G[top].size();
        for(int i = 0; i < size; i++){
            int v = G[top][i];
            if(color[v] == NIL){
                // まだ探索されていない頂点
                color[v] = c;
                s.push(v);
            }
        }
    }

}

C++: #include <cstdio>
#include <vector>
using namespace std;
const int MAXN = 1e5 + 7;
int col = 1;
int idx[MAXN];
vector<int> G[MAXN];
void dfs(int cur) {
	idx[cur] = col;
	for (int i = 0; i < G[cur].size(); i++) {
		if (!idx[G[cur][i]]) dfs(G[cur][i]);
	}
}
int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    while (m--) {
    	int s, t;
    	scanf("%d%d", &s, &t);
    	G[s].push_back(t);
    	G[t].push_back(s);
	}
	for (int i = 0; i < n; i++) {
		if (!idx[i]) {
			dfs(i);
			col++;
		}
	}
	int q;
	scanf("%d", &q);
	for (int i = 0; i < q; i++) {
		int s, t;
		scanf("%d%d", &s, &t);
		if (idx[s] == idx[t]) printf("yes\n");
		else printf("no\n");
	}
	return 0;
}

C++: #include<stack>
#include<queue>
#include<conio.h>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
queue<int> Q;
vector<int> G[100000];
bool flag = false;
int s, t;
int color[100000];

void a(){
	while (!Q.empty()){
		Q.pop();
		for (int i = 0; i < G[s].size(); i++){
			//_getch();
			//cout << G[s][i] << endl;
			if (G[s][i] == t){
				flag = true;
				while (!Q.empty()) Q.pop();
				return;
			}
			else if (color[i] == 0){
				Q.push(G[s][i]);
				color[i] = 1;
			}
		}
		if (!Q.empty())s = Q.front();
	}
}

int main(){
	int n, 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);
	}
	cin >> q; fill(color, color + 100000, 0);
	for (int i = 0; i < q; i++){
		cin >> s >> t;
		Q.push(s);
		a();
		if (flag) cout << "yes" << endl;
		else cout << "no" << endl;
		flag = false;
		fill(color, color + 100000, 0);
	}

	return 0;
}

C++: /* ALDS1_11_D: Connected Components */

#include <iostream>
#include <vector>
#include <stack>
using std::vector;
using std::stack;

void grouping(int id, int group_id, int group_ids[], vector<int> sns[]) {
    stack<int> S;
    S.push(id);
    group_ids[id] = group_id;

    while(! S.empty()) {
        id = S.top();
        S.pop();
        vector<int> friends = sns[id];
        for (int i = 0; i < friends.size(); i++) {
            int friend_id = friends[i];
            if (group_ids[friend_id] < 0) {
                group_ids[friend_id] = group_id;
                S.push(friend_id);
            }
        }
    }
}

void assign_groups(int n_users, vector<int> sns[], int group_ids[]) {
    int group_id = 0;
    std::fill_n(group_ids, n_users, -1);
    for (int id = 0; id < n_users; id++) {
        if (group_ids[id] < 0) {
            grouping(id, group_id, group_ids, sns);
            group_id++;
        }
    }
}

int main() {
    std::ios::sync_with_stdio(false);

    int n_users, n_relations;
    std::cin >> n_users >> n_relations;

    int id, friend_id, n_friends;
    vector<int> *sns = new vector<int>[n_users];
    for (int i = 0; i < n_relations; i++) {
        std::cin >> id >> friend_id;
        sns[id].push_back(friend_id);
        sns[friend_id].push_back(id);
    }

    int group_ids[n_users];
    assign_groups(n_users, sns, group_ids);

    int n_questions;
    std::cin >> n_questions;

    for (int i = 0; i < n_questions; i++) {
        std::cin >> id >> friend_id;
        bool same_group = group_ids[id] == group_ids[friend_id];
        std::cout << ( same_group ? "yes" : "no" ) << "\n";
    }
    return 0;
}