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 <iostream>
using namespace std;

int uni[100000];

int oya(int a){
if (uni[a]<0){
	return a;
}
else{
	uni[a]=oya(uni[a]);
	return oya(uni[a]);
}
}

bool ren(int a,int b){
	a=oya(a);
	b=oya(b);
	if(a==b){
	return	false;
	}
	else{
		
	return true;	
	}
}

int main() {

int n,m;
cin>>n>>m;
for(int y=0;y<n;y++){
	uni[y]=-1;
}

for(int r=0;r<m;r++){
int s,g;
cin>>s>>g;
if(ren(s,g)){
	s=oya(s);
	uni[s]=g;
	}
}
int q;
cin>>q;
for(int rr=0;rr<q;rr++){
	int s,g;
	cin>>s>>g;
	if(ren(s,g)==true){
		cout<<"NO"<<endl;
	}
	else{
		cout<<"YES"<<endl;
	}
}
	return 0;
}

C++: #include <iostream>
#include <vector>
#define eol '\n'
using namespace std;

vector<int> visit;

bool bfs(int s, int t, vector<vector<int>> list) {
  if (s == t) {
    return true;
  }
  
  // visit flag
  visit[s] = 1;
  
  for (int i = 0; i < list[s].size(); i++) {
    int next = list[s][i];
    if (visit[next] == 0) {
      if (bfs(next, t, list)) {
        return true;
      }
    }
  }
  return false;
}

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

  int n,m,s,t,q;
  cin >> n >> m;

  visit.resize(n,0);
  vector<vector<int>> list(n, {});
  
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    list[s].push_back(t);
    list[t].push_back(s);
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;

    if (bfs(s,t,list)) {
      cout << "yes" << eol;
    } else {
      cout << "no" << eol;
    }
  }
  return 0;
}

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

vector<int>q[100010];
int color[100010],n;

void dfs(int r ,int id){
	color[r]=id;
	stack<int>s;
	s.push(r);
	while(!s.empty()){
		int u=s.top();s.pop();
		for(int i=0;i<q[u].size();i++){
			int v=q[u][i];
			if(color[v]==-1){
				color[v]=id;
				s.push(v);
			}
		}
	}
}
void paint(){
	int id=1;
	for(int i=0;i<n;i++){
		if(color[i]==-1){
			dfs(i,id);
			id++;
		}
	}
}

int main() {
	int m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int s,t;
		cin>>s>>t;
		q[s].push_back(t);
		q[t].push_back(s);
	}
	for(int i=0;i<n;i++){
		color[i]=-1;
	}
	paint();
	int k;
	cin>>k;
	for(int i=0;i<k;i++){
		int s,t;
		cin>>s>>t;
		cout<< (color[s]==color[t]? "yes":"no");
		cout<<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;
    int i, u, v;
    while ( !S.empty() ) {
        u = S.top();
        S.pop();
        for ( i = 0; i < G[u].size(); i++ ) {
            v = G[u][i];
            if ( color[v] == NIL ) {
                color[v] = c;
                S.push(v);
            }
        }
    }
}
 
void assignColor() {
    int id = 1;
    int i, u;
    for ( i = 0; i < n; i++ ) color[i] = NIL;
    for ( u = 0; u < n; u++ ) {
        if ( color[u] == NIL ) dfs(u, id++);
    }
}
 
int main() {
    int s, t, m, q, i;
 
    cin >> n >> m;
 
    for ( i = 0; i < m; i++ ) {
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }
 
    assignColor();
 
    cin >> s;
 
    for ( 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 <queue>
using namespace std;

vector<int> list[100000];
int n,m;

bool bfs(int s, int t){
  bool used[m];
  for(int i=0;i<m;i++) used[i]=false;
  queue<int> Q;
  Q.push(s); used[s]=true;
  while(!Q.empty()){
    int k=Q.front(); Q.pop();
    if(k==t){return true;}
    for(int i=0;i<list[k].size();i++){
      int temp=list[k][i];
      if(!used[temp]){
        Q.push(temp); used[temp]=true;
      }
    }
  }
  return false;
}
int main(){
  cin>>n>>m;
  for(int i=0;i<m;i++){
    int u,v; cin>>u>>v;
    list[u].push_back(v);
    list[v].push_back(u);
  }
  int q; cin>>q;
  for(int i=0;i<q;i++){
    int s,t; cin>>s>>t;
    if(bfs(s, t)){
      cout<<"yes"<<"\n";
    }else{
      cout<<"no"<<"\n";
    }
  }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <stack>
using namespace std;
#define N 100000
#define WHITE 0
#define GRAY 1
#define BLACK 2

vector<int> G[100];
int color[N], num_members;
int cnt[N];

string dfs(int r, int target) {
    stack<int> S;
    S.push(r);
    color[r] = GRAY;

    while(!S.empty()) {
        int u = S.top();
        int v = G[u][cnt[u]];
        if(u==target) {
            return "yes";
        }
        cnt[u]++;

        if(v!=-1) {
            if(color[v]==WHITE) {
                color[v] = GRAY;
                S.push(v);
            }
        }
        else {
            S.pop();
            color[u] = BLACK;
        }
    }

    return "no";
}

int main() {
    int num_relations, from_id, to_id;
    int num_tests, test_from_id, test_to_id;

    cin >> num_members >> num_relations;
    for(int i=0; i<num_relations; i++) {
        cin >> from_id >> to_id;
        G[from_id].push_back(to_id);
    }
    //最後に番兵的な何かを仕込んでおく
    for(int i=0; i<num_members; i++) {
        G[i].push_back(-1);
    }

    cin >> num_tests;
    for(int i=0; i<num_tests; i++) {
        for(int j=0; j<num_members; j++) {
            color[j] = WHITE;
            cnt[j] = 0;
        }
        cin >> test_from_id >> test_to_id;
        cout << dfs(test_from_id, test_to_id) << endl;
    }
    
    return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct Node {
  vector<int> friends;
  int depth;
};
vector<Node> nodes;


void bfs(int id) {
  queue<int> q;
  q.push(id);

  for (auto el : nodes) {
    el.depth = -1;
  }
  nodes[id].depth = 0;

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto fid : nodes[u].friends) {
      if (nodes[fid].depth <= 0) {
        q.push(fid);
        nodes[fid].depth = nodes[u].depth + 1;
      }
    }
  }
}

bool isAccessible(int id){
  return (nodes[id].depth > 0);
}


int main() {
  int nrel, nuser;
  cin >> nuser;
  cin >> nrel;
  nodes.resize(nuser);
  int tmp1, tmp2;
  for (int i = 0; i < nrel; i++) {
    cin >> tmp1 >> tmp2;
    nodes[tmp1].friends.push_back(tmp2);
    nodes[tmp2].friends.push_back(tmp1);
  }

  int q, s, t;
  cin >> q;
  for (int i = 0; i< q; i++) {
    cin >> s >> t;
    bfs(s);
    cout << (isAccessible(t) ? "yes" : "no") << endl;
  }
}