Go: package main

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

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

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;
}

C++: #include <iostream>
#include <vector>
#include <stack>
using namespace std;
static const int MAX_N = 100010;
static const int NIL = -1;

vector<int> G[MAX_N];
int color[MAX_N];

void bfs(int u, int id) {
  stack<int> s;
  s.push(u);
  color[u] = id;
  while (!s.empty()) {
    int f = s.top(); s.pop();
    for (int i = 0; i < G[f].size(); ++i) {
      int v = G[f][i];
      if (color[v] == NIL) {
        color[v] = id;
        s.push(v);
      }
    }
  }
}

int main() {
  int n, m, s, t, q;

  cin >> n >> m;

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

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

  int id = 1;
  for (int u = 0; u < n; ++u) {
    if (color[u] == NIL) {
      bfs(u, id++);
    }
  }

  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> s >> t;
    if (color[s] == color[t]) {
      cout << "yes\n";
    } else {
      cout << "no\n";
    }
  }
  
  return 0;
}

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

const int NMAX = 100000;
const int MMAX = 100000;

vector<int> table[NMAX]; 

int color[NMAX];

void search(int s, int id, int n) {
	queue<int> q;
	q.push(s);
	color[s] = id;

	while(true) {
		if (q.empty()) {
			return;
		}
		int current = q.front(); q.pop();
		for (int i = 0; i < table[current].size(); i++) {
			int v = table[current][i];
			if (color[v] == -1) {
				q.push(v);
				color[v] = id;
			}
		}
	}
}

void assignColor(int n) {
	int id = 0;
	for (int i = 0; i < n; i++) {
		color[i] = -1;
	}	
	
	for (int i = 0; i < n; i++) {
		if (color[i] == -1) {
			id++;
			search(i, id, n);
		}
	}
}

int main() {
	int n, m;
	scanf("%d %d", &n, &m);

	for (int i = 0; i < m; i++) {
		int s, t;
		scanf("%d %d", &s, &t);
		table[s].push_back(t);
		table[t].push_back(s);
	}

	assignColor(n);

	int q;
	scanf("%d", &q);

	for (int i = 0; i < q; i++) {
		int s, t;
		scanf("%d %d", &s, &t);

		bool result = color[s] == color[t];
		if (result) {
			printf("yes\n");
		} else {
			printf("no\n");
		}
	}

	return 0;
}