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 <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 j=0; j<G[u].size(); j++){
            int v = G[u][j];
            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 i=0; i<n; i++){
        if(color[i]==NIL)
            dfs(i, 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>
#define rep(i, n) for (int i=0; i<(n); i++)
using namespace std;
static const int N=100000;
static const int NIL=-1;

int n,m,q;
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 u=S.top();
    S.pop();
    rep(i, G[u].size()) {
      int v=G[u][i];
      if (color[v]==NIL) {
        color[v]=c;
        S.push(v);
      }
    }
  }
}

void assignColor() {
  int id=1;
  rep(i, n) color[i]=NIL;
  rep(u, n) {
    if (color[u]==NIL) dfs(u, id++);
  }
}

int main() { 
  cin>>n>>m;
  rep(i, m) {
    int s,t; cin>>s>>t;
    G[s].push_back(t);
    G[t].push_back(s);
  }
  assignColor();
  cin>>q;
  vector<int> X(q), Y(q);
  rep(i, q) cin>>X[i]>>Y[i];
  rep(i, q) {
    if (color[X[i]]==color[Y[i]]) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
  return 0;
}

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

int n, m, s, t, u;
vector<vector <int> > a;
int f[100000];

void path(int s1, int w) {
	f[s1] = w;
	for (int i = 0; i < (int) a[s1].size(); i++)
		if (f[a[s1][i]] == 0)
			path(a[s1][i], w);
	return;
}

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

	a.resize(n);

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

	for (int i = 0; i < n; i++)
		f[i] = 0;

	int j = 0, k = 1;
	while (j < n) {
		if (f[j] == 0) {
			path(j, k);
			k++;
		}
		j++;
	}

	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> s >> t;

	if(f[s] == f[t])
		cout << "yes" << endl;
	else
		cout << "no" << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#define eol '\n'
using namespace std;

vector<int> group;

void bfs(int v, int g, vector< vector<int> > list) {
  if (group[v] != 0) {
    return;
  }
  group[v] = g;
  for (int i = 0; i < list[v].size(); i++) {
    bfs(list[v][i], g, list);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n,m,s,t,q;
  cin >> n >> m;

  vector< vector<int> > list(n, vector<int>());
  group = vector<int>(n,0);
  
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    list[s].push_back(t);
    list[t].push_back(s);
  }

  int g = 1;
  for (int i = 0; i < n; i++) {
    if (group[i] == 0) {
      bfs(i, g, list);
      g++;
    }
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    if (group[s] == group[t]) {
      cout << "yes" << eol;
    } else {
      cout << "no" << eol;
    }
  }
  return 0;
}

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

struct node {
	int parent;
	std::vector<int> relation;
};

void dfs(std::vector<node> &n, int people) {
	int d[100000];

	std::stack<int> s;
	for (size_t i = 0; i < n.size(); i++) {
		s.push(i);
		/*
		for (int k = 0; k < people; k++) {
			d[k] = 0;
		}
		*/
		while (s.size() != 0) {
			int num = s.top(); s.pop();
			d[num] = 1;
			for (std::vector<int>::iterator it = n[num].relation.begin(); it != n[num].relation.end(); it++) {
				if (d[*it] == 1) {
					n[num].relation.erase(it);
					it--;
				} else {
					s.push(*it);
					d[*it] = 1;
				}
			}
		}
	}
}

void makeparent(std::vector<node> &n) {
	std::queue<int> q;
	for (size_t i = 0; i < n.size(); i++) {
		n[i].parent = -1;
	}
	for (size_t i = 0; i < n.size(); i++) {
		q.push(i);
		int parent = i;
		while (q.size() != 0) {
			parent = q.front(); q.pop();
			for (size_t j = 0; j < n[parent].relation.size(); j++) {
				q.push(n[parent].relation[j]);
				n[n[parent].relation[j]].parent = parent;
			}
		}
	}
}

bool isconnect(std::vector<node> n, int from, int to) {
	std::stack<int> s;
	while (n[from].parent != -1) {
		from = n[from].parent;
	}
	if (to == from) return true;
	s.push(from);
	while(s.size() != 0) {
		from = s.top(); s.pop();
		for (std::vector<int>::iterator it = n[from].relation.begin(); it != n[from].relation.end(); it++) {
			if (to == *it) return true;
			s.push(*it);
		}
	}
	return false;
}

int main() {
	int people, relation;
	
	std::cin >> people >> relation;

	std::vector<node> n(people);

	for (int i = 0; i < relation; i++) {
		int from, to;
		std::cin >> from >> to;
		n[from].relation.push_back(to);
		n[to].relation.push_back(from);
	}

	dfs(n, people);
	makeparent(n);

	/*
	for (size_t i = 0; i < n.size(); i++) {
		std::cout << i << " " << n[i].parent << std::endl;
	}

	for (size_t i = 0; i < n.size(); i++) {
		for (size_t j = 0; j < n[i].relation.size(); j++) {
			std::cout << i << n[i].relation[j] << std::endl;
		}
	}
	*/

	int question;
	std::cin >> question;

	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		if (isconnect(n, from, to)) {
			std::cout << "yes" << std::endl;
		} else {
			std::cout << "no" << std::endl;
		}
	}

	return 0;
}