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 <stdio.h>
#include <iostream>
#include <queue>
using namespace std;

#define NODE_NUM 100000

queue<int> que;

class Node
{
private:
	int next_num;
	int next_node_list[NODE_NUM];
public:
	Node();
	void AddNextNode(int id);
	bool Search(int st, int ed);
	bool check[NODE_NUM];
	bool link[NODE_NUM];
};

Node::Node()
{
	next_num = 0;
	for (int i = 0; i < NODE_NUM; i++) {
		check[i] = false;
	}
	for (int i = 0; i < NODE_NUM; i++) {
		link[i] = false;
	}
}

void Node::AddNextNode(int id)
{
	next_node_list[next_num] = id;
	next_num++;
}

Node *Node_list;

bool Node::Search(int st, int ed)
{
	if (link[ed]) return true;

	for (int k = 0; k < next_num; k++) {
		Node_list[st].link[next_node_list[k]] = true;
		Node_list[next_node_list[k]].link[st] = true;
	}

	for (int i = 0; i < next_num; i++) {
		if (next_node_list[i] == ed) return true;
	}
	
	for (int j = 0; j < next_num; j++) {
		if (Node_list[next_node_list[j]].check[ed] == 0) {
			Node_list[next_node_list[j]].check[ed] = 1;
			que.push(next_node_list[j]);
		}
	}
	
	return false;
}

int main(void)
{
	//input
	int n;
	cin >> n;
	Node_list = new Node[n]();
	int m;
	cin >> m;
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		Node_list[s].AddNextNode(t);
		Node_list[t].AddNextNode(s);
	}
	
	//output
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t;
		Node_list[s].check[t] = true;
		bool ret = Node_list[s].Search(s, t);
		while (!ret && !que.empty()) {
			ret = Node_list[que.front()].Search(s, t);
			que.pop();
		};
		while (!que.empty()) {
			que.pop();
		}
		for (int j = 0; j < n; j++) {
			Node_list[j].check[t] = false;
		}

		if (ret) {
			cout << "yes" << "\n";
		}
		else {
			cout << "no" << "\n";
		}
	}
	
	return 0;
}

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

bool is_friend(const vector<vector<int> >& u, int s, int t, vector<int>& searched){
    if (s == t) return true;
    for (int i = 0; i < u[s].size(); i++) {
        if (!searched[u[s][i]]) {
            searched[u[s][i]] = 1;
            if (is_friend(u, u[s][i], t, searched)) return true;
        }
    }
    return false;
}
int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<int> > u(n);
    for (int i = 0; i < m; i++) {
        int s, t;
        cin >> s >> t;
        u[s].push_back(t);
        u[t].push_back(s);
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int s, t;
        cin >> s >> t;
        vector<int> searched(n, 0);
        searched[s] = 1;
        if (is_friend(u, s, t, searched)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}

C++: #include <stdio.h>
#include <stack>
#include <vector>

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

static int n;
static std::vector<int> G[MAX];
static int color[MAX];

static 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);
            }
        }
    }
}

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

int main()
{
    int m, q;

    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();

    scanf("%d", &q);

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

    return 0;
}

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

const int Max = 100005;
int color[Max], nt[Max];
vector<int> G[Max];
int n;
/*
int next(int u)
{
    nt[u]++;
    if(nt[u]<G[u].size())
        return G[u][nt[u]];
    else
        return -1;
}
*/
void dfs(int s, int t)
{

     //memset(nt, -1, sizeof(nt));
     stack<int> S;
     S.push(s);
     color[s] = t;
     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]==-1)
             {
                 color[v] = t;
                 S.push(v);
             }
         }
     }
     /*
     while(!S.empty())
     {
         int u = S.top();
         int v = next(u);
         if(v!=-1)
         {
             if(v==t) return 1;
             if(color[v]==-1)
             {
                 color[v] = 0;
                 S.push(v);
             }
         }
         else
         {
             S.pop();
         }
     }
     */
     //return 0;
}

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

int main()
{
    int m, q, s, t;
    scanf("%d %d", &n, &m);
    for(int i=0; i<m; i++)
    {
        scanf("%d %d", &s, &t);
        G[s].push_back(t);//邻接表
        G[t].push_back(s);
    }
    scanf("%d", &q);
    assignColor();
    for(int i=0; i<q; i++)
    {
        scanf("%d %d", &s, &t);
        if(color[s]==color[t])
            printf("yes\n");
        else
            printf("no\n");
        /*
        if(dfs(s, t))
            cout << "yes" << endl;
        else
            cout << "no" << endl;
            */
    }

    return 0;
}