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 <bits/stdc++.h>
using namespace std;

constexpr int MAX = 100010;

struct Edge{
    int v;
    Edge* next;
};

int N, M;
Edge* G[MAX];
int component[MAX];

void push( int u, int v ){
    Edge* e = new Edge;
    e->v = v;
    e->next = nullptr;

    Edge* i;
    for( i = G[u]; i->next != nullptr; i = i->next ){}
    i->next = e;
}

void dfs( int s, int k ){
    component[s] = k;
    for( Edge* e = G[s]->next; e != nullptr; e = e->next ){
        if( !component[e->v] ){
            dfs( e->v, k );
        }
    }
}

int main(){
    for( int i = 0; i < MAX; i++ ){
        Edge* e = new Edge;
        e->v = -1;
        e->next = nullptr;       
        G[i] = e;
    }
    
    cin >> N >> M;
    for( int i = 0; i < M; i++ ){
        int u, v; cin >> u >> v;
        push( u, v );
        push( v, u );
    }

    int k = 1;
    for( int i = 0; i < N; i++ ){
        if( !component[i] ){
            dfs( i, k );
            k++;
        }
    }

    int q; cin >> q;
    for( int i = 0; i < q; i++ ){
        int u, v; cin >> u >> v;
        if( component[u] == component[v] ){
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }
}

C++: #include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>

void dfs(int node, std::vector<std::vector<int>>& v, std::vector<int>& colors, int color) {
    colors[node] = color;
    for (int i=0; i<v[node].size(); i++) {
        if (node != v[node][i] and colors[v[node][i]] == 0) {
            dfs(v[node][i], v, colors, color);
        }
    }
} 

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n, m, q, s, t, color=1;
    std::cin >> n >> m;
    std::vector<std::vector<int>> v(n);
    std::vector<int> colors(n);
    for (int i=0; i<m; i++) {
        std::cin >> s >> t;
        v[s].push_back(t);
        v[t].push_back(s);
    }
    for (int i=0; i<n; i++) {
        if (colors[i] == 0) {
            dfs(i, v, colors, color++);
        }
    }
    std::cin >> q;
    for (int i=0; i<q; i++) {
        std::cin >> s >> t;
        if (colors[s] == colors[t] and colors[s] != 0) {
            std::cout << "yes\n";
        } else {
            std::cout << "no\n";
        }
    }

    return 0;
}

C++: /*
* @Author: Samson
* @Date:   2018-05-07 21:12:41
* @Last Modified by:   Samson
* @Last Modified time: 2018-05-07 21:44:27
*/
//   @URL : 
#include<bits/stdc++.h>
#include<algorithm>
#include<vector>
#include<stack>
#include <cstdlib>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;

int n,m,q,color[MAXN];
vector<int> v[MAXN];

void dfs(int x,int id)
{
	stack<int> s;
	int t;
	s.push(x);
	color[x] = id;
	while(s.size())
	{
		t = s.top();
		s.pop();
		for(int i = 0; i < v[t].size(); ++i)
		{
			int val = v[t][i];
			if(color[val] == -1)
			{
				color[val] = id;
				s.push(val);
			}
		}
	}
}

void fun()
{
	int id = 0;
	memset(color,-1,sizeof color);
	for(int i = 0; i < n; ++i)
		if(color[i] == -1)
			dfs(i,++id);
}

int main(void)
{
	ios::sync_with_stdio(false); 
	//cin.tie(0);
	cin>>n>>m;
	int s,e;
	for(int i = 0; i < m; ++i)
	{
		cin>>s>>e;
		v[s].push_back(e);
		v[e].push_back(s);
	}
	fun();
	cin>>q;
	while(q--)
	{
		cin>>s>>e;
		if(color[s] == color[e] && color[s]!=-1)
			cout<<"yes\n";
		else
			cout<<"no\n";
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
 
int main() {
int d[100100];
int m,n,q;
cin>>n>>m;
for(int i=0;i<n;i++){
    d[i]=i;
}
for(int l=0;l<m;l++){
    int a,b;
    cin>>a>>b;
    int j ,h;
    j=a,h=b;
while(d[j]!=j ||d[h]!=h){
    j=d[j];
    h=d[h];
}

    d[j]=h;
    d[a]=h;
    d[b]=h;
d[d[a]]=h;
d[d[b]]=h;

}

cin>>q;
for(int y=0;y<q;y++){
    int a,b;
    cin>>a>>b;
        int j ,h;
    j=a,h=b;
while(d[j]!=j ||d[h]!=h){
    j=d[j];
    h=d[h];
}
    if(j==h){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<endl;
    }
}
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

#define N 100000
#define NIL -1

int n, m;
vector<int> G[N];
int color[N];

queue<int> Q;

void bfs(int x, int c){
    if(color[x] != NIL) return;
    Q.push(x);
    color[x] = c;

    while(!Q.empty()){
        int u = Q.front(); Q.pop();
        for(int i = 0; i < G[u].size(); i++){
                int v = G[u][i];
                if(color[v] == NIL)
                Q.push(v);
                color[v] = c;
        }
    }
}

void assignColor(){
    for(int i = 0; i < n; i++) color[i] = NIL;
    int id = 1;
    for(int i = 0; i < n; i++) bfs(i,id++);
}


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

    rep(i,m){
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    assignColor();

    int q;
    cin >> q;
    rep(i,q){
        int x, y;
        cin >> x >> y;
        if(color[x] == color[y]) cout << "yes" << endl;
        else cout << "no" << endl;
    }

    return 0;

}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n) ; i++)
constexpr auto INF = 2147483647;
typedef long long ll;
constexpr int MAX = 100000;

vector<int> Group_list[MAX];
const int NIL = -1;
int Check_list[MAX];
int n;

void Checking(){
  //init
  rep(i,n) Check_list[i] = NIL;
  stack<int> stk;
  int check_cnt = 0;
  //solve
  rep(i,n){
    if(Check_list[i] == NIL) {
      stk.push(i);
      check_cnt++;
      Check_list[i] = check_cnt;
    }
    int now, next;
    while(!stk.empty()){
        now = stk.top();
        stk.pop();
        rep(j, Group_list[now].size()){
          next = Group_list[now][j];
          if(Check_list[next] == NIL) {
            Check_list[next] = check_cnt;
            stk.push(next);
          }
        }
    }
  }
}

int main(){
  int m,q;
  int from, to;
  cin >> n >> m;
  rep(i,m){
    cin >> from >> to;
    Group_list[from].push_back(to);
    Group_list[to].push_back(from);
  }

  Checking();

  cin >> q;
  rep(i,q){
    cin >> from >> to;
    if(Check_list[from] == Check_list[to] && Check_list[from] != NIL){
      cout << "yes" << endl;;
    }else{
      cout << "no" << endl;
    }
  }
  

  return 0;
}