Go: // AOJ ALDS1_11_D: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_D

package main

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

var nocolor = -1

type graph struct {
	graph  [][]int
	colors []int
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func newGraph(nodeNum int) *graph {
	colors := make([]int, nodeNum)
	g := make([][]int, nodeNum)
	for i := 0; i < nodeNum; i++ {
		g[i] = []int{}
		colors[i] = nocolor
	}

	return &graph{g, colors}
}

func constructGraph(sc *bufio.Scanner) *graph {
	usersNum := nextInt(sc)
	linkNum := nextInt(sc)

	g := newGraph(usersNum)
	for i := 0; i < linkNum; i++ {
		u := nextInt(sc) // node
		l := nextInt(sc) // link node
		// edge each other
		g.graph[u] = append(g.graph[u], l)
		g.graph[l] = append(g.graph[l], u)
	}
	return g
}

func (g *graph) dfs(r, color int) {
	g.colors[r] = color

	stack := list.New()
	stack.PushFront(r)

	for stack.Len() > 0 {
		v := stack.Remove(stack.Front()).(int)
		for _, u := range g.graph[v] {
			if g.colors[u] == nocolor {
				g.colors[u] = color
				stack.PushFront(u)
			}
		}
	}
}

func (g *graph) assignColor() {
	color := 1
	for u := 0; u < len(g.graph); u++ {
		if g.colors[u] == nocolor {
			g.dfs(u, color)
			color++
		}
	}
}

func (g *graph) isLink(s, t int) bool {
	return g.colors[s] == g.colors[t]
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	// construct graph and friendly colors
	g := constructGraph(sc)
	g.assignColor()

	// judge link
	q := nextInt(sc)
	for i := 0; i < q; i++ {
		s := nextInt(sc)
		t := nextInt(sc)

		if g.isLink(s, t) {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

C++: #include<iostream>
#include<vector>
#include<stack>
#define MAX 100000
#define NIL -1

// 隣接リスト
std::vector<int> G[MAX];

// 隣接する頂点に同じ色を付けて記録するデータ
int color[MAX];

// 頂点数
int n;

// 深さ探索
// 探索した頂点には隣接頂点の印としてcの色を付ける
void dfs(int r, int c)
{
	std::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 m;
	std::cin >> n >> m;

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

	assignColor();

	int q;
	std::cin >> q;
	for (int i = 0; i < q; i++)
	{
		std::cin >> s >> t;
		if (color[s] == color[t])
		{
			std::cout << "yes" << std::endl;
		}
		else
		{
			std::cout << "no" << std::endl;
		}
	}

	return 0;
}

// 参考
// ALDS1_11_B_DepthFirstSearchと同じロジックで深さ探索を実装
void dfs_2(int r, int c)
{
	std::stack<int> S;
	S.push(r);
	color[r] = c;
	while (!S.empty())
	{
		int u = S.top();
		int v = -1;
		for (int i = 0; i < G[u].size(); i++)
		{
			if (color[G[u][i]] == NIL)
			{
				v = G[u][i];
			}
		}
		if (v != -1)
		{
			S.push(v);
			color[v] = c;
		}
		else
		{
			S.pop();
		}
	}
}

C++: #include <stdio.h>
#include <stack>
#include <vector> 
#include <iostream>
#define N 100000
using namespace std;

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

void dfs(int s, int c)
{
    stack<int> st;
    color[s] = c;
    st.push(s);
    while(!st.empty())
    {
        int u = st.top();
        st.pop();
        for (int i = 0; i < G[u].size(); i++)
        {
            int v = G[u][i];
            if(color[v] == -1)
            {
                color[v] = c;
                st.push(v);
            }
        } 
    }
    return;
}

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

    return;
}

int main()
{
    //freopen("in3.txt","r",stdin);
    int m;
    scanf("%d %d",&n, &m);

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

    assignColor(); 

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

C++: #include <iostream>
#include <string>
#include <vector>
#include <stack>
using namespace std;
#define N 100000
#define WHITE 0
#define GRAY 1
#define BLACK 2

vector<int> G[100];
int color[N], num_members;
int cnt[N];

string dfs(int r, int target) {
    stack<int> S;
    S.push(r);
    color[r] = GRAY;

    while(!S.empty()) {
        int u = S.top();
        int v = G[u][cnt[u]];
        if(u==target) {
            return "yes";
        }
        cnt[u]++;

        if(v!=-1) {
            if(color[v]==WHITE) {
                color[v] = GRAY;
                S.push(v);
            }
        }
        else {
            S.pop();
            color[u] = BLACK;
        }
    }

    return "no";
}

int main() {
    int num_relations, from_id, to_id;
    int num_tests, test_from_id, test_to_id;

    cin >> num_members >> num_relations;
    for(int i=0; i<num_relations; i++) {
        cin >> from_id >> to_id;
        G[from_id].push_back(to_id);
    }
    //最後に番兵的な何かを仕込んでおく
    for(int i=0; i<num_members; i++) {
        G[i].push_back(-1);
    }

    cin >> num_tests;
    for(int i=0; i<num_tests; i++) {
        for(int j=0; j<num_members; j++) {
            color[j] = WHITE;
            cnt[j] = 0;
        }
        cin >> test_from_id >> test_to_id;
        cout << dfs(test_from_id, test_to_id) << endl;
    }
    
    return 0;
}

C++: //ALDS1_11_B:Depth First Search
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <tuple>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <random>
#include <time.h>
#include <cassert>
using namespace std;

vector<int> friends[100000];
int group[100000];

void dfs(int now, int g){
    group[now] = g;
    for(int i=0; i<friends[now].size(); i++){
        if(group[friends[now][i]] == -1) dfs(friends[now][i], g);
    }
}

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

    for(int i=0; i<n; i++){
        if(group[i] == -1){
            dfs(i, i);
        }
    }

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