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 <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

struct node {
	int id;
	vector<int> rel;
};

int n, m, q;
node* nodes;

/* DFS * TLE! *
bool canReach(int s, int d, bool* h){
	h[s] = true;
	for(int i = 0; i < nodes[s].rel.size(); i++){
		if(nodes[s].rel[i] == d) return true;
		else if(!h[nodes[s].rel[i]]){
			if(canReach(nodes[s].rel[i], d, h)) return true;
		}
	}
	return false;
}
*/

int main() {
	scanf("%d %d", &n, &m);
	
	nodes = new node[n];
	for(int i = 0; i < n; i++){
		nodes[i].id = i;
	}
	while(m--){
		int l, r;
		scanf("%d %d", &l, &r);
		nodes[l].rel.push_back(r);
		nodes[r].rel.push_back(l);
	}
	
	scanf("%d", &q);
	while(q--){
		int l, r;
		scanf("%d %d", &l, &r);
		
		bool *h = new bool[n];
		for(int i = 0; i < n; i++){
			h[i] = false;
		}
		
		queue<int> qu;
		qu.push(l);
		while(qu.size()){
			h[qu.front()] = true;
			for(int i = 0; i < nodes[qu.front()].rel.size(); i++){
				if(nodes[qu.front()].rel[i] == r){
					puts("yes");
					goto BREAK;
				}else if(!h[nodes[qu.front()].rel[i]]){
					qu.push(nodes[qu.front()].rel[i]);
				}
			}
			qu.pop();
		}
		puts("no");
		BREAK:;
	}
	return 0;
}

C++: #include <iostream>
#include <stack>
#include <vector>
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 <stdio.h>
#include <stack>
#include <vector>
using namespace std;

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  vector<int> relations[n];
  int index1, index2;
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &index1, &index2);
    relations[index1].push_back(index2);
    relations[index2].push_back(index1);
  }
  int color[n], id;
  for (int i = 0; i < n; i++) {
    color[i] = -1;
  }
  id = 0;
  for (int i = 0; i < n; i++) {
    if (color[i] == -1) {
      id++;
      stack<int> S;
      S.push(i);
      while (!S.empty()) {
        int v = S.top(); S.pop();
        color[v] = id;
        for (int j = 0; j < relations[v].size(); j++) {
          int nv = relations[v][j];
          if (color[nv] == -1) {
            S.push(nv);
          }
        }
      }
    }
  }
  scanf("%d", &m);
  int u1, u2;
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &u1, &u2);
    if (color[u1] == color[u2]) printf("yes\n");
    else printf("no\n");
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int main() {

	int n, m;
	cin >> n >> m;
	vector < vector<int>> G(n + 1);

	//グラフ隣接リスト読み込み
	int a, c;
	for (int i = 1; i <= m; i++) {
		cin >> a >> c;
		G[a].push_back(c);
		G[c].push_back(a);
	}

	//幅優先探索処理
	vector<bool> seen(n + 1, false);	//発見したらtrue
	deque<int> todo;					//行く所のスタック

	int s = 1;							//今いる場所

	bool brkflg;
	vector<long> color(n + 1);
	int colornum = 0;
	for (int j = 0; j < n; j++) {		//全ての頂点を調べる

		if (color[j] == 0) {
			colornum++;
			color[j] = colornum;
			brkflg = false;
			todo.clear();

			//for (int k = 0; k <= n; k++) {
			//	seen[k] = false;//一旦全クリア
			//}

			if (seen[j] == false) {			//訪れてなかったら処理すすめる
				seen[j] = true;
				todo.push_back(j);

				while (todo.empty() != true) {				//キューが空になるまで処理	
					s = todo.front();						//処理番号を指定
					int tugiflg = false;					//次があったらtrueにするフラグ
					for (int i = 0; i < G[s].size(); i++) {

						if (seen[G[s][i]] != true) {
							seen[G[s][i]] = true;
							color[G[s][i]] = colornum;
							todo.push_back(G[s][i]);
							tugiflg = true;
						}
					}
					todo.pop_front(); //次を全部探したので削除する
				}
			}
		}
	}

	int in1, in2;
	cin >> m;
	for (int i = 0; i <m; i++) {
		cin >> in1 >> in2;
		if (color[in1] == color[in2]) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}

	return 0;

}

C++: #include <stdio.h>
#include <set>
#define MAX_USER (100000)

typedef std::set<long> Circle;

void merge_circle( Circle** circleinfo , long uid1 , long uid2 )
{
	Circle* src;
	Circle* dst;
	if( circleinfo[ uid1 ]->size() < circleinfo[ uid2 ]->size() )
	{
		src = circleinfo[ uid1 ];
		dst = circleinfo[ uid2 ];
	}
	else
	{
		src = circleinfo[ uid2 ];
		dst = circleinfo[ uid1 ];
	}

	Circle::const_iterator itr;
	for( itr = src->begin() ; itr != src->end() ; itr++ )
	{
		circleinfo[ *itr ] = dst;
		dst->insert( *itr );
	}
}

void add_member( Circle** circleinfo , long uid1 , long uid2 )
{
	if( circleinfo[ uid1 ] == 0 && circleinfo[ uid2 ] == 0 )
	{
		//create new circle
		Circle* entry = new Circle;
		entry->insert( uid1 );
		entry->insert( uid2 );

		circleinfo[ uid1 ] = entry;
		circleinfo[ uid2 ] = entry;
	}
	else if( circleinfo[ uid1 ] != 0 && circleinfo[ uid2 ] == 0 )
	{
		circleinfo[ uid1 ]->insert( uid2 );
		circleinfo[ uid2 ] = circleinfo[ uid1 ];
	}
	else if( circleinfo[ uid1 ] == 0 && circleinfo[ uid2 ] != 0 )
	{
		circleinfo[ uid2 ]->insert( uid1 );
		circleinfo[ uid1 ] = circleinfo[ uid2 ];
	}
	else
	{
		if( circleinfo[ uid1 ] != circleinfo[ uid2 ] )
		{
			merge_circle( circleinfo , uid1 , uid2 );
		}
	}
}

int main( int argc , char** argv )
{

	//read setting
	long user_count;
	long friendship_count;
	scanf( "%ld %ld" , &user_count , &friendship_count );

	//initialize
	Circle** user_info = new Circle*[ user_count ];
	for( long i = 0 ; i < user_count ; i++ )
	{
		user_info[ i ] = 0;
	}

	//read user friendship
	for( long i = 0 ; i < friendship_count ; i++ )
	{
		long uid1;
		long uid2;
		scanf( "%ld %ld" , &uid1 , &uid2 );

		add_member( user_info , uid1 , uid2 );
	}

	//read number of question
	long question_count;
	scanf( "%ld" , &question_count );

	//process question
	for( long i = 0 ; i < question_count ; i++ )
	{
		long uid1;
		long uid2;
		scanf( "%ld %ld" , &uid1 , &uid2 );

		if( user_info[ uid1 ] == user_info[ uid2 ] )
		{
			printf( "yes\n" );
		}
		else
		{
			printf( "no\n" );
		}
	}

	delete[] user_info;
	return 0;
}