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;
vector<int> graph[100000];

int fin[100000];

void dfs(int id,int group){
    fin[id] = group;
    for(int i = 0;i < graph[id].size();i++){
        if(fin[graph[id][i]] == 0)
            dfs(graph[id][i],group);
    }
}

int main(){
    int n,m;

    scanf("%d%d",&n,&m);

    for(int i = 0;i < m;i++){
        int a,b;
        scanf("%d%d",&a,&b);

        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    for(int i = 0;i < n;i++){
        sort(&graph[i][0],&graph[i][graph[i].size()]);
    }

    int group = 1;
    while(true){
       int iii;
       for(iii = 0;iii < n;iii++){
           if(fin[iii] == 0){
               dfs(iii,group);
               break;
           }
       }
       if(iii == n)
          break;
       group++; 
    }

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

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

C++: #include<iostream>
#include<vector>
#include<stack>
#include<algorithm>

bool dfs(std::vector<std::vector<int> > &n, int from, int to) {
	std::stack<int> s;
	std::vector<int> went(n.size(), 0);

	s.push(from);
	while (s.size() != 0){
		int num = s.top(); s.pop();
		for (std::vector<int>::iterator it = n[num].begin(); it != n[num].end(); it++) {
			if (*it == to) return true;
			if (went[*it] == 0) {
				s.push(*it);
				went[*it] = 1;
			}
		}
	}
	return false;
}

int main() {
	int people, relation;

	std::cin >> people >> relation;

	std::vector<std::vector<int> > n(people);

	for (int i = 0; i < relation; i++) {
		int from, to;
		std::cin >> from >> to;
		n[from].push_back(to);
		n[to].push_back(from);
	}

	int question;
	std::cin >> question;
	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		if (dfs(n, from, to)) {
			std::cout << "yes" << std::endl;
		} else {
			std::cout << "no" << std::endl;
		}
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main(){
  int n,m,t1,t2,c[100000]={}; vector <int> g[100000];
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>t1>>t2;
    g[t1].push_back(t2);
    g[t2].push_back(t1);
  }
  int l;cin>>l;
  for(int i=0;i<n;i++) if(!c[i]){
      queue <int> q; bool f[100000]={};
      q.push(i);f[i]=true;c[i]=i+1;
      for(;!q.empty();){
        for(int j=0;j<g[q.front()].size();j++){
          int v=g[q.front()][j];
          if(!f[v]){
            f[v]=true;c[v]=i+1;
            q.push(v);
          }
	}
          q.pop();
      }
    }
  for(int i=0;i<l;i++){
    cin>>t1>>t2;
    if(c[t1]==c[t2]) 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 REP0(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REP1(i,n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
#define RREP0(i,n) for(int i=n-1; i>=0; --i)
#define RREP1(i,n) for(int i=n; i>=1; --i)

#define SORT(c) sort((c).begin(),(c).end())
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define CLR(a) memset((a), 0 ,sizeof(a))

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;

const int INTINF = 2147483647;
const LL LLINF = 9223372036854775807;
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int MAX = 100000;
const int NIL = -1;

int gcd(int a,int b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int n,m,q;
VI 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();
    REP0( i, G[u].size() ){
      int v = G[u][i];
      if ( color[v] == NIL ) {
        color[v] = c;
        S.push(v);
      }
    }
  }
}


void assignColor(){
  int id=1;
  REP0(i,n) color[i]=NIL;
  REP0(u,n) if (color[u]==NIL) dfs(u, id++);
  //連結成分ごとにdfsしcoloring
}


void input(){
  std::cin >> n >> m;
  int s,t;
  REP0(i,m){
    std::cin >> s >> t;
    G[s].PB(t);
    G[t].PB(s);
  }
}


void output() {
  std::cin >> q; int s,t;
  REP0(i,q){
    std::cin >> s >> t;
    if (color[s]==color[t]) {
      std::cout << "yes" << '\n';
    }else{
      std::cout << "no" << '\n';
    }
  }
}


int main(int argc, char const *argv[]) {
  input();
  assignColor();
  output();
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#define MAX 100005
#define NIL -1 
using namespace std;

int color[MAX];
int n,m,s,t,q;
vector<int> G[MAX];

void dfs(int x, int y) {
	stack<int> S;
	S.push(x);
	color[x]=y;
	while(!S.empty()) {
		int tmp=S.top();
		S.pop();
		for(int i=0;i<G[tmp].size();i++) {
			int l=G[tmp][i];
			if(color[l]==NIL) {
				color[l]=y;
				S.push(l);
			}
		}
	}
}

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

int main() {
	int i,j;
	scanf("%d %d",&n,&m);
	for(i=0;i<m;i++) {
		scanf("%d %d",&s,&t);
		G[s].push_back(t);
		G[t].push_back(s);
	}
	
	assigncolor();
	
	scanf("%d",&q);
	for(i=0;i<q;i++) {
		scanf("%d %d",&s,&t);
		if(color[s]==color[t]) printf("yes\n");
		else printf("no\n");
	}
	
	return 0;
}