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<unordered_map>
#include<unordered_set>

using namespace std;

int main(){
	int n, m, s, t, q, i, j;
	int id, stc = 0;
	bool answer_yes = false;
	unordered_map<int, unordered_set<int> > Adj;
	unordered_map<int, int> Stack;
	unordered_set<int> Check;
	unordered_set<int>::iterator it, itend;

	cin >> n >> m;
	for (i = 0; i < m; i++) {
		cin >> s >> t;
		Adj[s].insert(t);
		Adj[t].insert(s);
	}

	/*for (auto it1 = Adj.begin(); it1 != Adj.end(); it1++) {
		cout << "\n" << it1->first << " : " << flush;
		for (auto it2 = it1->second.begin(); it2 != it1->second.end(); it2++) {
			cout << *it2 << " " << flush;
		}
	}
	cout << endl;*/

	cin >> q;
	for (i = 0; i < q; i++) {
		cin >> s >> t;
		
		// calculate
		Stack.insert(pair<int, int>(stc, s));
		Check.insert(s);
		while (!Stack.empty()) {
			id = Stack[stc];
			//cout << "\nid : " << id << " stc : " << stc << endl;
			itend = Adj[id].end();
			for (it = Adj[id].begin(); it != itend; it++) {
				//cout << *it << " " << flush;
				if (Check.find(*it) == Check.end()) {
					if (*it == t) {
						answer_yes = true;
						Stack.clear();
						break;
					}
					stc++;
					Stack.insert(pair<int, int>(stc, *it));
					Check.insert(*it);
					break;
				}
			}
			if (it == itend) {
				Stack.erase(stc);
				stc--;
			}
		}
		if (answer_yes) {
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
			Stack.clear();
		}
		answer_yes = false;
		Check.clear();
		stc = 0;
	}
	
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
using namespace std;
#define maxn 100000
int n,color[maxn];
vector<int> G[maxn];
void dfs(int g,int c){
    stack<int> s;
    s.push(g);
    color[g] = 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]){
                s.push(v);
                color[v] = c;
            }
        }
    }
}
int main(){
    int m,a,b,q;
    scanf("%d%d",&n,&m);
    while(m--){
        scanf("%d%d",&a,&b);
        G[a].push_back(b);
        G[b].push_back(a);
    }
    int j=1;
    for(int i=0;i<n;i++)
        if(!color[i])
            dfs(i,j++);
    scanf("%d",&q);
    while(q--){
        scanf("%d%d",&a,&b);
        if(color[a]==color[b])
            printf("yes\n");
        else
            printf("no\n");
    }
}

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

queue<int> que;

class Node
{
private:
	//int next_num;
	//int *next_node_list;
	list<int> next_node_list;
public:
	Node();
	void AddNextNode(int id);
	bool Search(int st, int ed);
	bool check; //!
	//bool *check;
	//bool *link;
};

Node::Node()
{
	//next_num = 0;
	check = false;
	/*check = new bool[n];
	for (int i = 0; i < n; i++) {
		check[i] = false;
	}*/
	/*link = new bool[n];
	for (int i = 0; i < n; i++) {
		link[i] = false;
	}*/
}

void Node::AddNextNode(int id)
{
	next_node_list.push_back(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;
	}*/
	list<int>::iterator itr;
	for(itr = next_node_list.begin(); itr != next_node_list.end(); itr++){
		if (*itr == ed) return true;
	}
	
	/*for (int j = 0; j < next_num; j++) {
		//if (Node_list[next_node_list[j]].check[ed] == 0) {
		if (Node_list[next_node_list[j]].check == 0) { //!
		//	Node_list[next_node_list[j]].check[ed] = 1;
			Node_list[next_node_list[j]].check = 1; //!
			que.push(next_node_list[j]);
		}
	}*/
	for(itr = next_node_list.begin(); itr != next_node_list.end(); itr++){
		if (Node_list[*itr].check == 0) {
			Node_list[*itr].check = 1;
			que.push(*itr);
		}
	}
	
	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;
		Node_list[s].check = 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;
			Node_list[j].check = false; //!
		}

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

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

using namespace std;

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

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

void dfs(int r, int c) {
  stack<int> S;
  S.push(s);
  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, s, t;

  cin >> n >> m;

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

  assignColor();

  int q;
  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;
}