Go: package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX = 100000
	NIL = -1
)

var (
	n, m, q, s, t int
	G             [MAX][]int //連結リスト
	color         [MAX]int
)

func dfs(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

//幅優先探索で、頂点を色付け
func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			dfs(i, id)
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n, m = nextInt(), nextInt()
	for i := 0; i < m; i++ {
		s, t = nextInt(), nextInt()
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)

	}
	assignColor()
	q = nextInt()
	for i := 0; i < q; i++ {
		s, t = nextInt(), nextInt()
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}

}

C++: // 連結成分
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define NIL -1

vector<int> G[100005];
int color[100005];
int n,col;

void dfs(int r, int c);
void assign_number();

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

    col = 0;
    assign_number();
    
    int q;
    cin >> q;
    
    for(int i = 0; i < q; i++){
        int s,t; cin >> s >> t;
        cout << (color[s] == color[t] ? "yes" : "no") << endl;
    }
    return 0;
}

void assign_number(){
    // 未訪問の頂点を選んで探索していく
    for(int i = 0; i < n; i++){
        color[i] = NIL;
    }

    for(int i = 0; i < n; i++){
        if(color[i] == NIL){
            dfs(i,col); col++;
        }
    }
}

void dfs(int r, int c){
    // 頂点rから探索しそのグループにcの番号をふる
    stack<int> s;
    s.push(r);
    color[r] = c;

    while(!s.empty()){
        int top = s.top();
        s.pop();
        int size = G[top].size();
        for(int i = 0; i < size; i++){
            int v = G[top][i];
            if(color[v] == NIL){
                // まだ探索されていない頂点
                color[v] = c;
                s.push(v);
            }
        }
    }

}

C++: #include <cstdio>
#include <vector>
using namespace std;
const int MAXN = 1e5 + 7;
int col = 1;
int idx[MAXN];
vector<int> G[MAXN];
void dfs(int cur) {
	idx[cur] = col;
	for (int i = 0; i < G[cur].size(); i++) {
		if (!idx[G[cur][i]]) dfs(G[cur][i]);
	}
}
int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    while (m--) {
    	int s, t;
    	scanf("%d%d", &s, &t);
    	G[s].push_back(t);
    	G[t].push_back(s);
	}
	for (int i = 0; i < n; i++) {
		if (!idx[i]) {
			dfs(i);
			col++;
		}
	}
	int q;
	scanf("%d", &q);
	for (int i = 0; i < q; i++) {
		int s, t;
		scanf("%d%d", &s, &t);
		if (idx[s] == idx[t]) printf("yes\n");
		else printf("no\n");
	}
	return 0;
}

C++: #include<stack>
#include<queue>
#include<conio.h>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
queue<int> Q;
vector<int> G[100000];
bool flag = false;
int s, t;
int color[100000];

void a(){
	while (!Q.empty()){
		Q.pop();
		for (int i = 0; i < G[s].size(); i++){
			//_getch();
			//cout << G[s][i] << endl;
			if (G[s][i] == t){
				flag = true;
				while (!Q.empty()) Q.pop();
				return;
			}
			else if (color[i] == 0){
				Q.push(G[s][i]);
				color[i] = 1;
			}
		}
		if (!Q.empty())s = Q.front();
	}
}

int main(){
	int n, 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);
	}
	cin >> q; fill(color, color + 100000, 0);
	for (int i = 0; i < q; i++){
		cin >> s >> t;
		Q.push(s);
		a();
		if (flag) cout << "yes" << endl;
		else cout << "no" << endl;
		flag = false;
		fill(color, color + 100000, 0);
	}

	return 0;
}

C++: /* ALDS1_11_D: Connected Components */

#include <iostream>
#include <vector>
#include <stack>
using std::vector;
using std::stack;

void grouping(int id, int group_id, int group_ids[], vector<int> sns[]) {
    stack<int> S;
    S.push(id);
    group_ids[id] = group_id;

    while(! S.empty()) {
        id = S.top();
        S.pop();
        vector<int> friends = sns[id];
        for (int i = 0; i < friends.size(); i++) {
            int friend_id = friends[i];
            if (group_ids[friend_id] < 0) {
                group_ids[friend_id] = group_id;
                S.push(friend_id);
            }
        }
    }
}

void assign_groups(int n_users, vector<int> sns[], int group_ids[]) {
    int group_id = 0;
    std::fill_n(group_ids, n_users, -1);
    for (int id = 0; id < n_users; id++) {
        if (group_ids[id] < 0) {
            grouping(id, group_id, group_ids, sns);
            group_id++;
        }
    }
}

int main() {
    std::ios::sync_with_stdio(false);

    int n_users, n_relations;
    std::cin >> n_users >> n_relations;

    int id, friend_id, n_friends;
    vector<int> *sns = new vector<int>[n_users];
    for (int i = 0; i < n_relations; i++) {
        std::cin >> id >> friend_id;
        sns[id].push_back(friend_id);
        sns[friend_id].push_back(id);
    }

    int group_ids[n_users];
    assign_groups(n_users, sns, group_ids);

    int n_questions;
    std::cin >> n_questions;

    for (int i = 0; i < n_questions; i++) {
        std::cin >> id >> friend_id;
        bool same_group = group_ids[id] == group_ids[friend_id];
        std::cout << ( same_group ? "yes" : "no" ) << "\n";
    }
    return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;

const int maxn = 100000 + 5;
int color[maxn];
bool vis[maxn];
vector <int> G[maxn];

void dfs(int u, int c)
{
    vis[u] = true;
    color[u] = c;

    for (int i = 0; i < G[u].size(); i++)
        if (!vis[G[u][i]])
        dfs(G[u][i], c);
}

int main()
{
    int n, m;
    while (scanf("%d %d", &n, &m) != EOF)
    {
        for (int i = 0; i < n; i++)
            G[i].clear();

        int u, v;
        for (int i = 0; i < m; i++)
        {
            scanf("%d %d", &u, &v);
            G[u].push_back(v);
            G[v].push_back(u);
        }

        memset(color, 0, sizeof(color));
        memset(vis, false, sizeof(vis));

        int c = 0;
        for (int i = 0; i < n; i++)
        {
            if (!vis[i])
            {
                ++c;
                dfs(i, c);
            }
        }

        int q;
        scanf("%d", &q);
        while (q--)
        {
            int u, v;
            scanf("%d %d", &u, &v);
            if (color[u] == color[v])
                printf("yes\n");
            else
                printf("no\n");
        }
    }
    return 0;
}