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 <iostream>
#include <vector>
#include<stack>
using namespace std;
static const int MAX = 10000;
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 assign(){
	int id=1;
	for(int i=0;i<n;i++){
		color[i] = NIL;
	}
	for(int j=0;j<n;j++){
		if(color[NIL]==NIL){
			dfs(j,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);
	}
	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 <algorithm>
#include <cmath>
#include <climits>

using namespace std;

const int WHITE=0, GRAY=1, BLACK=2;

struct DFS {
    int n;
    vector<vector<int>> g;
    vector<int> group;
    vector<int> state;
    int groupId = 0;
    
    DFS(int n):n(n) {
        g.assign(n, vector<int>());
        group.assign(n, -1);
        state.assign(n, WHITE);
    }
    
    void addEdge(int u, int v) {
        g[u].push_back(v);
    }
    
    void solve() {
        for (int i = 0; i < n; i++) {
            if (state[i] == WHITE) {
                groupId++;
                dfs(i);
            }
        }
    }
    
    void dfs(int u) {
        state[u] = GRAY;
        group[u] = groupId;
        for (int v : g[u]) {
            if (state[v] == WHITE) {
                dfs(v);
            } 
        }
        state[u] = BLACK;
    }
    
    void print() {
        for (int i = 0; i < n; i++) {
            cout << i << ":";
            for (int v : g[i]) {
                cout << v << " ";
            }
            cout << endl;
        }
    }
};

int main() {
    ios::sync_with_stdio(false);
    int n, e;
    cin >> n >> e;
    
    DFS dfs(n);
    
    for (int i = 0; i < e; i++) {
       int u, v;
       cin >> u >> v;
       dfs.addEdge(u, v);
       dfs.addEdge(v, u);
    }
   
    dfs.solve();
    
    int q;
    cin >> q;
    
    for (int i = 0; i < q; i++) {
        int u, v;
        cin >> u >> v;
        if (dfs.group[u] == dfs.group[v]) {
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }
    
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#define MAX 100000
using namespace std;
int n,m;
vector<vector<int> > list(MAX);
int grp[MAX];
int group;

void dfs(int now){
	stack<int> next;
	grp[now]=group;	next.push(now);
	while(!next.empty()){
		now=next.top();	next.pop();
		for(int i=0;i<(int)list[now].size();i++){
			if(grp[list[now][i]]==-1){
				grp[list[now][i]]=group;
				next.push(list[now][i]);
			}
		}
	}
}

int main(){
	cin>>n>>m;
	int s,t;
	for(int i=0;i<m;i++){
		cin>>s>>t;
		list[s].push_back(t);
		list[t].push_back(s);
	}
	for(int i=0;i<n;i++)	grp[i]=-1;
	group=1;
	for(int i=0;i<n;i++){
		if(grp[i]==-1){
			dfs(i);
			group++;
		}
	}
	int q;	cin>>q;
	for(int i=0;i<q;i++){
		cin>>s>>t;
		if(grp[s]==grp[t])	cout<<"yes"<<endl;
		else 	cout<<"no"<<endl;
	}
	return 0;
}

C++: #include <string>
#include <vector>
#include <stack>
#include <iostream>
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);
      }
    }
  }
}

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>
#include<cstdio>
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 ) {
            int v = G[u][i];
            //頂点uに隣接する頂点vを探索
            if ( color[v] == NIL ) {
                color[v] = c;
                S.push(v);//id(色)は変わらない
            }
        }
    }
}

void assignColor() {
    int id = 1;             //全てにNIL
    for ( int i = 0; i < n; ++i ) color[i] = NIL;
    for ( int u = 0; u < n; ++u ) {
        if ( color[u] == NIL ) dfs( u , id++ ); //id(色)にかぶりがないように
    }
}

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

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

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

    assignColor();

    scanf("%d",&q);

    for ( int i = 0; i < q; ++i ) {
        scanf("%d %d",&s,&t);
        if ( color[s] == color[t] ) {
            puts("yes");
        } else {
            puts("no");
        }
    }

    return 0;
}