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<bits/stdc++.h>
#define REP(i,n) for(long long i=0;i<n;++i)
#define FOR(i,a,b) for(long long i=a;i<b;++i)
using namespace std;
const int MAX=1e5+1;
const int NIL=-1;
int n,color[MAX];
vector< int > graph[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();
        REP(i,graph[u].size()){
            int v=graph[u][i];
            if(color[v]==NIL){
                color[v]=c;
                S.push(v);
            }
        }
    }
}
int main(){
    int s,t,m,q;
    scanf("%d%d",&n,&m);
    REP(i,m){
        scanf("%d%d",&s,&t);
        graph[s].push_back(t);
        graph[t].push_back(s);
    }
    int id=1;
    fill(color,color+MAX-1,NIL);
    REP(u,n){
        if(color[u]==NIL) dfs(u,id++);
    }
    scanf("%d",&q);
    REP(i,q){
        scanf("%d%d",&s,&t);
        printf("%s\n",color[s]==color[t]?"yes":"no");
    }
    return 0;
}

C++: #include <iostream>
using namespace std;
  
int main() {
int d[100100];
int m,n,q;
cin>>n>>m;
for(int i=0;i<n;i++){
    d[i]=i;
}
for(int l=0;l<m;l++){
    int a,b;
    cin>>a>>b;
    int j ,h;
    j=a,h=b;
while(d[j]!=j && d[h]!=h){
    j=d[j];
    h=d[h];
}
if(d[j]==j){
	while(d[h]!=h){
		h=d[h];
	}
    d[j]=h;
d[d[a]]=h;
    d[a]=h;
d[b]=h;
}
else{
	while(d[j]!=j){
		j=d[j];
	}
  d[h]=j;
    d[b]=j;
d[a]=j;
}
}
cin>>q;
for(int y=0;y<q;y++){
    int a,b;
    cin>>a>>b;
        int j ,h;
    j=a,h=b;
while(d[j]!=j || d[h]!=h){
    j=d[j];
    h=d[h];
}
    if(j==h){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<endl;
    }
}
    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int (i) = 0;(i) < (n);(i)++)
#define SORT(c) sort((c).begin(),(c).end())
#define MOD 1000000007
using namespace std;
typedef long long ll;

struct UnionFind {
    vector<int> data;
    int sz;
    UnionFind(int sz) : data(sz, -1), sz(sz) { }
    bool unionSet(int x, int y) {
        if ((x = root(x)) == (y = root(y))) return false;
        if (data[x] > data[y]) swap(x, y);
        data[x] += data[y]; data[y] = x;
        sz--;
        return true;
    }
    bool findSet(int x, int y) { return root(x) == root(y); }
    int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
    int size(int x) { return -data[root(x)]; }
    int size() {return sz;}
};

int main(void){
  int n, m;
  cin >> n >> m;

  UnionFind uf(n);
  rep(i, m){
    int s, t;
    cin >> s >> t;
    uf.unionSet(s, t);
  }

  vector<string> ans;
  int q;
  cin >> q;
  rep(i, q){
    int a, b;
    cin >> a >> b;
    if(uf.findSet(a, b)) ans.push_back("yes");
    else ans.push_back("no");
  }
  rep(i, ans.size()) cout << ans[i] << endl;
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxx=100010;

int pre[maxx];

int find(int u){
	if(pre[u]!=u) pre[u]=find(pre[u]);
	return pre[u];
}

int main(){
	int n,m,x,y;
	cin>>n>>m;
	for(int i=0;i<n;i++) pre[i]=i;
	while(m--){
		cin>>x>>y;
		pre[find(x)]=find(y);
	}
	cin>>m;
	while(m--){
		cin>>x>>y;
		if(find(x)==find(y)) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

const int N = 100000;
int A[N][N] = {{0}};
int n;

bool dfs(int s, int t) {
    if (A[s][t] == 1) {
        return true;
    }
    for (int i = 0; i < n; ++i) {
        if (i != t && A[s][i] != 0) {
            if (dfs(i, t)) {
                return true;
            }
        }
    }
    return false;
}

int main() {
    int m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int s, t;
        cin >> s >> t;
        A[s][t] = 1;
    }

    int q;
    cin >> q;
    for (int j = 0; j < q; ++j) {
        int x, y;
        cin >> x >> y;

        if (dfs(x, y)) {
            cout << "yes";
        } else {
            cout << "no";
        }
        cout << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX = 100000;
static const int NTL = -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;
  while (!S.empty()) {
    int u = S.top(); S.pop();
    for (int i =0;i < G[u].size();i++){
      int v =G[u][i];
     if(color[v]==NTL){
      color[v]=c;
      S.push;
      }
    }
  }
}

void assigncolor() {
  int id=1;
  for(int i=0;i<n;i++) color[i]=NTL;
  for(int u =0;u<n;u++){
    if(color[u]==NTL)dfs(u,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);
  }

  assigncolor();

  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 <iostream>
#include <map>
#include <queue>
using namespace std;
int main(){
  int n,m,t1,t2,c[100000]={}; 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]={},find=false;
    cin>>t1>>t2;
    if(c[t1]&&c[t2]){
      if(c[t1]==c[t2]) cout<<"yes"<<endl;
      else cout<<"no"<<endl;
      continue;
    }
    q.push(t1);f[t1]=true;c[t1]=i+1;
    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;c[t2]=i+1;
          q.push(j);
	}
      q.pop();
      if(find) break;
    }
    if(find) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
const int MAX_N = 100100;
template<typename Type> struct UnionFind
{
    vector<Type> p, r;
    UnionFind(int sz)
    {
        p.resize(sz);
        r.resize(sz, -1);
        for(int i = 0; i < sz; i++) p[i] = i;
    }
    Type Find(Type x)
    {
        if (x == p[x]) return x;
        return p[x] = Find(p[x]);
    }
    bool isSame(Type a, Type b) { return Find(a) == Find(b); }
    void unite(Type a, Type b)
    {
        a = Find(a), b = Find(b);
        if(a == b) return;
        if(r[a] < r[b]) p[a] = b;
        else { p[b] = a; if(r[a] == r[b]) r[a]++; }
    }
};
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m; cin >> n >> m;
    UnionFind<int> UF(n + 1);
    for(int i = 0; i < m; i++)
    {
        int s, t; cin >> s >> t;
        if(UF.isSame(s, t)) continue;
        UF.unite(s, t);
    }
    int q; cin >> q;
    for(int i = 0; i < q; i++)
    {
        int s, t; cin >> s >> t;
        cout << (UF.isSame(s, t) ? "yes" : "no") << "\n";
    }
    return 0;
}