Go: package main

import (
	"fmt"
)

var n, m int
var g [][]int
var color []int

func main() {
	fmt.Scan(&n, &m)
	g = make([][]int, n) // SNSの関係を表すグラフ
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		g[a] = append(g[a], b) // 無向重みなしグラフ
		g[b] = append(g[b], a)
	}
	color = make([]int, n) // 各ノードにつける色
	for i := 0; i < len(color); i++ {
		color[i] = -1 // 初期化しておく
	}
	type query struct {
		s, t int
	}
	var Q int
	fmt.Scan(&Q)
	qs := make([]query, Q)
	for i := 0; i < Q; i++ {
		fmt.Scan(&qs[i].s, &qs[i].t)
	}
	// fmt.Println("n", n, "m", m, "g", g, "Q", Q, "qs", qs)
	// 全ての頂点からdfsする
	id := 1
	for u := 0; u < n; u++ {
		if color[u] == -1 {
			dfs(u, id)
			id++
		}
	}
	// fmt.Println("color", color)
	for i := 0; i < Q; i++ {
		if color[qs[i].s] == color[qs[i].t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func dfs(v, c int) {
	// fmt.Printf("dfs(v %d, c %d) en:\n", v, c)
	color[v] = c
	for _, vv := range g[v] {
		// fmt.Printf("  v %d, g[v] %v, vv %d\n", v, g[v], vv)
		if color[vv] == -1 {
			dfs(vv, c)
		}
	}
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <climits>
#include <string>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cassert>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const ll MOD = ll(1e9) + 7;
const ll INF = LLONG_MAX;
const ll MAX_N = ll(1e5) + 5;
const double EPS = 10e-8;
const double PI = 3.14159265359;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

struct UF{
    static const int MAX_N = 100000;
    int p[MAX_N];
    int r[MAX_N];

    void init(int n){
        REP(i,n){
            p[i]=i;
            r[i]=0;
        }
    }
    int find(int x){
        if(p[x]==x)return x;
        return p[x] = find(p[x]);
    }
    void unite(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y)return;
        if(r[x] < r[y])swap(x,y);
        if(r[x]==r[y])r[x]++;
        p[y]=x;
    }
    bool same(int x, int y){
        return find(x)==find(y);
    }
};

int main() {
    int n,m,q;
    cin >> n >> m;
    UF g;
    g.init(n);
    REP(i,m){
        int a,b;
        cin >> a >> b;
        g.unite(a,b);
    }
    cin >> q;
    REP(i,q){
        int a,b;
        cin >> a >> b;
        if(g.same(a,b))cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <unordered_set>
#include <vector>

int main(){
    int n;
    int m;
    std::scanf("%d", &n);
    std::scanf("%d", &m);
    std::vector<std::unordered_set<int> > relations;
    for(int i = 0; i < m; ++i){
        int s;
        int t;
        std::scanf("%d", &s);
        std::scanf("%d", &t);
        int finds = -1;
        int findt = -1;
        for(int j = 0; j < relations.size(); ++j){
            if(relations[j].find(s) != relations[j].end()){
                finds = j;
            }
            if(relations[j].find(t) != relations[j].end()){
                findt = j;
            }
            if(findt != -1 && finds != -1) break;
        }
        for(int j = 0; j < relations.size(); ++j){
        }
        if(finds == -1 && findt == -1){
            std::unordered_set<int> set;
            set.insert(s);
            set.insert(t);
            relations.push_back(set);
        } else if(findt == -1){
            relations[finds].insert(t);
        } else if(finds == -1){
            relations[findt].insert(s);
        } else if(finds != findt){

            int smaller = (relations[finds].size() < relations[findt].size()) ? finds : findt;
            int larger = (smaller == findt) ? finds : findt;
            for(auto &x: relations[smaller]){
                relations[larger].insert(x);
            }
            relations[smaller] = relations.back();
            relations.pop_back();
        }
    }
    int q;
    std::scanf("%d", &q);
    for(int i = 0; i < q; ++i){
        int s;
        int t;
        std::scanf("%d", &s);
        std::scanf("%d", &t);
        bool find = false;
        for(int j = 0; j < relations.size(); ++j){
            if(relations[j].find(s) != relations[j].end() && relations[j].find(t) != relations[j].end()){
                find = true;
		break;
            }
        }
        if(find){
            std::printf("yes\n");
        } else {
            std::printf("no\n");
        }
    }

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

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

int main(){
  int n,m,q,a,b,s,t,k,h;
  vector<int> g[100001];
  queue<int> qu;
  set<int> ch;
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>a>>b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  cin>>q;
  for(int i=0;i<q;i++){
    cin>>s>>t;
    
    k=0; 
    ch.clear();
    while(qu.size()){
      qu.pop();
    }
    qu.push(s);
    
    while(qu.size()){//?????????
      n=qu.front();qu.pop();
      if(n==t){//???????????????
	cout<<"yes"<<endl;
	k++;
	break;
      }
      for(int j=0;j<g[n].size();j++){//????????????????????´????????????
	if(ch.count(g[n][j])==0 ){
	  qu.push(g[n][j]);
	  ch.insert(g[n][j]);
	}
      }
    }
    
    if(k==0)cout<<"no"<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std;
static const int MAX = 100000;
int n;
int color[MAX];
vector<int> v[MAX];
void b(int s, int t){
	stack<int> S;
	S.push(s);
	int tp;
	color[s] = t;
	while (!S.empty()){
		int tp = S.top(); S.pop();
		for (int i = 0; i < v[tp].size(); i++){
			if (color[v[tp][i]] == MAX){
				color[v[tp][i]] = t;
				S.push(v[tp][i]);
			}
		}
	}
}
void a(){
	for (int i = 0; i < n; i++){
		color[i] = MAX;
	}
	int id = 0;
	for (int i = 0; i < n; i++){
		id++;
		if(color[i] == MAX)b(i,id);
	}
}
int main(){
	int m, s, t;
	int q;
	cin >> n >> m;
	for (int i = 0; i < m; i++){
		cin >> s >> t;
		v[s].push_back(t);
		v[t].push_back(s);
	}
	a();
	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;
}