Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	nConns := scanInt(sc)

	graph := make([][]int, nNodes)
	for i := 0; i < nConns; i++ {
		a, b := scanInt(sc), scanInt(sc)
		graph[a] = append(graph[a], b)
		graph[b] = append(graph[b], a)
	}

	groups := make([]int, nNodes)

	var setGroups func(int, int)
	setGroups = func(u int, groupId int) {
		if groups[u] > 0 {
			return
		}
		groups[u] = groupId

		for _, v := range graph[u] {
			setGroups(v, groupId)
		}
	}
	for i := 0; i < nNodes; i++ {
		setGroups(i, i+1)
	}

	nQuestions := scanInt(sc)

	w := bufio.NewWriter(os.Stdout)
	var ans string
	for i := 0; i < nQuestions; i++ {
		a, b := scanInt(sc), scanInt(sc)
		if groups[a] == groups[b] {
			ans = "yes"
		} else {
			ans = "no"
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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

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

const int N = 100000;
const int NIL = -1;

int n;
vector<int> G[N];
int color[N];

void dfs(int r, int c){
    stack<int> S;
    S.push(r);
    color[r] = c;

    while(!S.empty()){
        int x = S.top(); S.pop();
        for(int i = 0; i < G[x].size(); i++){
            int v = G[x][i];
            if(color[v] == NIL){
                color[v] = c;
                S.push(v);
            }
        }
    }
}

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

int main(){
    int q, s, t;
    cin >> n >> q;

    while(q-- > 0){
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    solve();

    cin >> q;
    while(q-- > 0){
        cin >> s >> t;
        if(color[s] == color[t])    cout << "yes" << endl;
        else                        cout << "no" << endl;
    }

    return 0;
}

C++: #include <vector>
#include <iostream>
using namespace std;
int n, q, x, y; int group[100000]; vector<int> f[100000];
int main() {
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; i++) group[i] = i, f[i].push_back(i);
	for(int i = 0; i < m; i++) {
		scanf("%d%d", &x, &y);
		if(group[x] == group[y]) continue;
		x = group[x], y = group[y];
		if(f[y].size() > f[x].size()) swap(x, y);
		for(int j = f[y].size() - 1; j >= 0; j--) {
			group[f[y][j]] = group[x];
			f[x].push_back(f[y][j]);
			f[y].pop_back();
		}
	}
	scanf("%d", &q);
	for(int i = 0; i < q; i++) {
		scanf("%d%d", &x, &y);
		printf("%s\n", group[x] == group[y] ? "yes" : "no");
	}
	return 0;
}