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 <vector>
#include <algorithm>
#include <stack>
#include <queue>

using namespace std;

static const int N = 100000;
static const int NIL = -1;

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

void bfs(int u)
{
	queue<int> q;
	q.push(u);
	color[u] = c;

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

void DrawColor()
{
	for (int i = 0; i < n; ++i) color[i] = -1;
	for (int i = 0; i < n; ++i)
	{
		if (color[i] == -1) bfs(i);
		c++;
	}
}

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

	DrawColor();

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

	return 0;
}

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

bool DFS(vector<vector<int> >& G, int a, int b) {
    if (G[a].size() == 0) return false;

    stack<pair<int, int> > S;
    vector<bool> is_visited(G.size(), false);
    int idx = 0;
    do {
        if (idx >= G[a].size()) {
            a   = S.top().first;
            idx = S.top().second;
            S.pop();
            idx++;
            continue;
        }
        if (G[a][idx] == b) return true;
        if (!is_visited[G[a][idx]]) {
            is_visited[G[a][idx]] = true;
            S.push(make_pair(a, idx));
            a   = G[a][idx];
            idx = 0;
            continue;
        }
        idx++;
    } while (!S.empty());
    return false;
}


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

    vector<vector<int> > G(n);
vector<vector<bool> > D(100000, vector<bool>(100000));

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

// for (int i = 0; i < n; i++) {
//     cout << i << " : ";
//     for (set<int>::iterator it = G[i].begin(); it != G[i].end(); it++){
//         cout << *it << " ";
//     }
//     cout << endl;
// }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        if (DFS(G, a, b)) {
           cout << "yes" << endl;
        } else {
           cout << "no"  << endl;
        }
    }
    return 0;
}

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

using namespace std;
static const int MAX=100;
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_color()
{
    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);
    }
    assign_color();
    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<cstdio>
#include<vector>
#define N 100000
using namespace std;

vector<int> G[N];
/*
??£??\??????????????????????????°???????????¨??????????????£????????????????????????DFS??§????????????

 */
int labels[N], c=0;

void dfs_init(){
    int i;
    for(i=0;i<N; i++)labels[i]=0;
}

/*
  ?????°?????¨??????????£?
  ??§???????????????????????????
  stack???????????°??§???????????????
 */
bool dfs(int u, int v){
    if (u == v) return true;
    labels[u] = 1;
    int i,x;
    for(i=0; i<G[u].size(); i++){
	x = G[u][i];
	dfs(x,v);
    }
    return false;
}

void dfs2(int u){
    labels[u]=c;
    int i;
    for(i=0; i<G[u].size(); i++){
	if (labels[G[u][i]]==0) dfs2(G[u][i]);
    }
}


int main(void) {
    int n,m,i,q, u,v;
    scanf("%d %d",&n,&m);
    for(i=0;i<m;i++){
	scanf("%d %d",&u,&v);
	G[u].push_back(v);
	G[v].push_back(u);
    }
    dfs_init();
    /*
      ????????§?????¨???????????????
     */
    for(i=0; i<n; i++){
	if(labels[i]==0){
	    c++;
	    dfs2(i);
	}
    }

    scanf("%d",&q);
    for(i=0;i<q;i++){
	scanf("%d %d", &u, &v);
	if(labels[u]>0 && labels[u]==labels[v])printf("yes\n");
	else printf("no\n");
    }
    
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int d[100001];
int main(void){
	int	n,m;
	int s,t;
	int q;
	int i,j;
	int x,y;
	int cnt;
	vector<int>v[100001];
	queue<int>que;
	cin>>n>>m;
	for(i=0;i<m;i++){
		cin>>s>>t;
		v[s].push_back(t);
		v[t].push_back(s);
	}
	for(i=0;i<n;i++)	d[i]=0;
	cnt=0;
	for(i=0;i<n;i++){
		if(d[i]==0){
			cnt++;
			d[i]=cnt;
			que.push(i);
			while(!que.empty()){
				x=que.front();
				que.pop();
				for(j=0;j<v[x].size();j++){
					y=v[x][j];
					if(d[y]==0){
						d[y]=cnt;
						que.push(y);
					}
				}
			}
		}
	}
	cin>>q;
	for(i=0;i<q;i++){
		cin>>s>>t;
		cout<<((d[s]==d[t])?"yes":"no")<<endl;
	}
	
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

#define MAX_N 100000
#define MAX_M 100000
#define WHITE 1
#define GREY 2
#define BLACK 3

vector<int> G[MAX_N];
int V[MAX_N];
int V_num[MAX_N];

void init(void){
    for(int i = 0; i < MAX_N; i++){
        V[i] = WHITE;
        V_num[i] = i;
    }
}

void labeling(int target){
    queue<int> Q;
    Q.push(target);
    while(!Q.empty()){
        int num = Q.front(); Q.pop();
        for(int i = 0; i < G[num].size(); i++){
            if(V[G[num][i]] == WHITE){
                Q.push(G[num][i]);
                V[G[num][i]] = GREY;
                V_num[G[num][i]] = target;
            }
        }
        V[num] = BLACK;
    }
}

int main(void){
    init();
    int n, m;
    scanf("%d %d\n", &n, &m);
    for(int i = 0; i < m; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        G[a].push_back(b);
        G[b].push_back(a);
    }

    for(int i = 0; i < n; i++){
        if(V[i] != WHITE){
            continue;
        }
        labeling(i);
    }

    int q;
    scanf("%d", &q);
    for(int i = 0; i < q; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        if(V_num[a] == V_num[b]){
            printf("yes\n");
        }else{
            printf("no\n");
        }
    }

    /*
    for(int i = 0; i < n; i++){
        printf("%d %d\n", i, V_num[i]);
    }
    */
}