Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	for i := 0; i < n; i++ {
		group[i] = i
	}
	
	fmt.Scan(&m)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < m; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if group[s] < group[t] {
			group[t] = group[s]
		} else {
			group[s] = group[t]
		}
	}

	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if root(s, &group) == root(t, &group) {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

C++: #include <cstdio>
#include <algorithm>
#define rep(i, n) for(int i=0; i<(n); ++i)

int parent[100005];

int find(int x){
   if( parent[x] == x )return x;
   return parent[x] = find( parent[x] );
}

void unite(int x, int y)
{
   x = find(x);
   y = find(y);

   if(x == y)return;
   parent[x] = y;
}

int main()
{
   int N, M;
   int x, y, tmp;
   scanf("%d%d", &N, &M);
   std::iota(parent, parent + N, 0);

   rep(i, M){
      scanf("%d%d", &x, &y);
      unite(x, y);
   }

   int Q;
   scanf("%d", &Q);
   while(Q--){
      scanf("%d%d", &x, &y);
      puts( find(x) == find(y) ? "yes" : "no" );
   }
}

C++: #include<stack>
#include<queue>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
queue<int> Q;
vector<int> G[100000];
bool flag = false;
int s, t;

void a(){
	while (!Q.empty()){
		Q.pop();
		for (int i = 0; i < G[s].size(); i++){
			if (G[s][i] == t){
				flag = true;
				while (!Q.empty()) Q.pop();
				return;
			}
			else {
				Q.push(G[s][i]);
			}
		}
		if(!Q.empty())s = Q.front();
	}
}

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

	for (int i = 0; i < m; i++){
		cin >> s >> t;
		G[s].push_back(t); //??£???????????£?????????
	}
	cin >> q;
	for (int i = 0; i < q; i++){
		cin >> s >> t;
		Q.push(s);
		a();
		if (flag) cout << "yes" << endl;
		else cout << "no" << endl;
		flag = false;
	}

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX = 100000;
static const int NIL = -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] == NIL)
            {
                color[v] = c;
                S.push(v);
            }
        }
    }
}

void assignColor()
{
    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 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<vector>
#include<stack>
using namespace std;

static const int MAX = 100000;
static const int NIL = -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] == NIL ) {
	color[v] = c;
	S.push(v);
      }
    }
  }
}

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

  int id = 1;
  
  for ( int i = 0; i < n; ++i) {
    color[i] = NIL; //preset
  }
  
  for ( int u = 0; u < n; ++u) {
    if ( color[u] == NIL ) {
      dfs(u,id);
      id++;
    }
  }
  
  scanf("%d", &q);
  for ( int 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<unordered_map>
int main(){
  return 0;
}

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

using namespace std;

class UnionFind {
public:
	vector<int> par;

	UnionFind(int n) {
		par.resize(n);
		for (int i = 0; i < n; i++) par[i] = i;
	}

	UnionFind() {};

	int root(int x) {
		if (par[x] == x)return x;
		else return par[x] = root(par[x]);
	}

	void unite(int x, int y) {
		x = root(x);
		y = root(y);
		if (x == y)return;
		par[x] = y;
	}

	bool same(int x, int y) {
		return root(x) == root(y);
	}
};

int main() {
	int n, m, s, t, q;
	cin >> n >> m;
	UnionFind uf(n);
	while (m--) cin >> s >> t, uf.unite(s, t);
	cin >> q;
	while (q--)cin >> s >> t, cout << (uf.same(s, t) ? "yes" : "no") << endl;
}

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

vector<int> list[100000];
int n,m;

bool bfs(int s, int t){

  bool used[m];
  for(int i=0;i<m;i++) used[i]=false; //初期化

  queue<int> Q;
  Q.push(s); used[s]=true;

  while(!Q.empty()){
    int k=Q.front(); Q.pop();
    for(int i=0;i<list[k].size();i++){
      int temp=list[k][i];
      if(temp==t){return true;}
      if(!used[temp]){ //未訪問なら
        Q.push(temp); used[temp]=true;
      }
    }
  }
  return false;
}
int main(){

  cin>>n>>m;

  for(int i=0;i<m;i++){
    int u,v; cin>>u>>v;
    list[u].push_back(v);
    list[v].push_back(u);
  }

  int q; cin>>q;

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

}

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

int n;
vector<int> g[100001];
char c[100001];
int visit[100001];
int finish[100001];
int group[100001];
int m;
int main() {

    cin >> n >> m;
    for (int i1 = 1; i1 <=m; ++i1) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }

   stack<int> s;
    for (int l = 0; l < n; ++l) {
        c[l] = 'w';

    }
    int time = 0;
    int start = -1;
    int v;
    int nowg = 0;
    while(true) {
        nowg++;
        start++;
        if(start == n) break;
        if(c[start] == 'w') {
            time++;
            s.push(start);
            c[start] = 'g';
            group[start] = nowg;
            visit[start] = time;
            while (s.size() != 0) {
                time++;
                v = s.top();
                int next = -1;
                for (int i = 0; i < g[v].size() ; ++i) {
                    if(c[g[v].at(i)] == 'w') {
                        next = g[v].at(i);
                        break;
                    }
                }
                if(next == -1) {
                    c[v] = 'b';
                    finish[v] = time;
                    s.pop();
                } else {
                    c[next] = 'g';
                    group[next] = nowg;
                    visit[next] = time;
                    s.push(next);
                }
            }
        }
    }

    int q;
    cin >> q;
    for (int m = 0; m < q; ++m) {
        int a, b;
        cin >> a >> b;
        if(group[a] == group[b]) cout << "yes";
        else cout << "no";
        cout << endl; 


    }



    return 0;
}