Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = nil
	}
	for u := 0; u < n; u++ {
		if color[u] == nil {
			id++
			dfs(u, id)
		}
	}
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

	for i := 0; i < r; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	assignColor()
	sc.Scan()
	qNum, _ := strconv.Atoi(sc.Text())
	for i := 0; i < qNum; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 0
}

C++: 
#include<iostream>
#include<queue>

using namespace std;

const int MAX = 100000;

int parent(int table[], int child) {
	queue<int> q;

	while (table[child] != -1) {
		q.push(child);
		child = table[child];
	}
	while (!q.empty()) {
		table[q.front()] = child;
		q.pop();
	}
	return child;
}

int main() {
	int n, m, q;
	int table[MAX];

	cin >> n >> m;

	for (int i = 0; i < n; i++)table[i] = -1;
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		
		int ps = parent(table, s);
		int pt = parent(table, t);
		if (ps != pt) {
			table[ps] = pt;
		}
	}

	cin >> q;

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

		int ps = parent(table, s);
		int pt = parent(table, t);

		if (ps == pt) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
 
int n, m, q;
int dp[100000];
vector<int> v[100000];
 
void solve1(int p, int idx) {
    if (dp[p]<0) {
        dp[p]=idx;
        for (int i=0;i<v[p].size();i++) {
            solve1(v[p][i],idx);
        }
    }
}
 
void syoki(){
    int idx=0;
    for(int i=0;i<n;i++) {
        if(dp[i]<0) {
            solve1(i,idx++);
        }
    }
}
 
 
int main(){
    int s, t;
    cin>>n>>m;
    for (int i=0;i<m;i++){
        cin>>s>>t;
        v[s].push_back(t);
        v[t].push_back(s);
    }
    cin>>q;
    fill(dp,dp+n,-1);
    syoki();
    for(int j=0;j<q;j++){
        cin>>s>>t;
        if(dp[s]==dp[t]){
            cout<<"yes"<<endl;
        }
        else{
            cout<<"no"<<endl;
        }
    }
 
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   for(int i=0;i<(n);++i)
#define SORT(c)    sort((c).begin(),(c).end())
#define CLR(a)     memset((&a), 0 ,sizeof(a))//clear memory
#define dump(x)    cerr << #x << " = " << (x) << endl;//debug
#define debug(x)   cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define EPS        1e-10//sample:if((double)>=(double)+EPS)
//cin,cout release
//std::cin.tie(0);
//ios::sync_with_stdio(false);

using namespace std;

vector<int> G[200001];
int color[200001];
int s[1000000],head,tail;
void dfs(int r,int c){
  head=tail=0;
  s[tail++]=r;
  color[r]=c;
  while(head!=tail){
    int u=s[head++];
    for(int i=0;i<(int)G[u].size();i++){
      int v=G[u][i];
      if(color[v]==-1){
	s[tail++]=v;
	color[v]=c;
      }
    }
  }
}
int main(){
  int n,m,ia,ib,p,id=1;
  rep(i,200000)color[i]=-1;
  cin>>n>>p;
  rep(i,p){
    cin>>ia>>ib;
    G[ia].push_back(ib);
    G[ib].push_back(ia);
  }
  rep(i,n)if(color[i]==-1)dfs(i,id++);
  cin>>m;
  rep(i,m){
    cin>>ia>>ib;
    if(color[ia]==color[ib])cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }

  return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX=1000000;
static const int NIL=-1;
int n;
vector<int>G[MAX];//頂点数を1000000
                  //隣接セット
int color[MAX];

void dfs(int r,int c){
  stack<int>S;
  S.push(r);//始点rをスタックに追加
  color[r]=c; //色を入れる
  while(!S.empty()){ //スタックが空でない場合
    int u=S.top();
    S.pop(); //スタックからuを取り出し訪問
    for(int i=0;i<G[u].size();++i){
      //頂点uに隣接する頂点vを探索
      int v=G[u][i];
      if(color[v]==NIL){
	color[v]=c;
	S.push(v); //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); //t→s
  }
  int id=1; //colorに入れる色
  for(int i=0;i<n;++i){
    color[i]=NIL; //すべてにNIL
  }
  for(int u=0;u<n;++u){
    if(color[u]==NIL) dfs(u,id++);
  }
  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 <stdio.h>
#include <vector>
#include <queue>
#define NUM 100000
#define NIL -1
using namespace std;

int node_num;
vector<int> v[NUM + 1];
queue<int> Q;
int color[NUM + 1];

bool is_connected(int a, int b){
    if(color[a] == color[b]){
        return true;
    }else{
        return false;
    }
}

void add_next(int n){
    for(int i = 0; i < v[n].size(); i++){
        if(color[v[n][i]] != NIL)
            continue;
        Q.push(v[n][i]);
        color[v[n][i]] = color[n];
    }
}

void bfs(){
    for(int i = 1; i <= node_num; i++){
        if(color[i] != NIL)
            continue;
        color[i] = i;
        Q.push(i);
        add_next(i);

        while(Q.size()){
            int top = Q.front();
            Q.pop();
            add_next(top);
        }
    }
    return;
}

void init(){
    for(int i = 0; i <= NUM; i++){
        color[i] = NIL;
    }
}

int main(void){
    init();

    int m;
    scanf("%d", &node_num);
    scanf("%d", &m);

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

    bfs();
     
    int q;
    scanf("%d", &q);
    for(int i = 0; i < q; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        if(is_connected(a, b)){
            printf("yes\n");
        }else{
            printf("no\n");
        }
    }
}

C++: #include <iostream>
#include <map>
#include <queue>
using namespace std;
int main(){

  int n,m,t1,t2;
  map <int,bool> g[100000];
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>t1>>t2;
    g[t1][t2]=1;
    g[t2][t1]=1;
  }
  int l;
  cin>>l;
  for(int i=0;i<l;i++){
    queue <int> q;
    bool f[100000]={1},find=false;
    cin>>t1>>t2;
    q.push(t1);
    for(;!q.empty();){
      for(int j=0;j<n;j++) if(g[q.front()][j]&&!f[j]){
          if(j==t2) find=true;
          f[j]=true;
          q.push(j);
	}
      q.pop();
      if(find) break;
    }
    if(find) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
  return 0;
}