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>

using namespace std;
using Relations = vector<vector<int>>;

const int no_group = -1;
const int no_user_id = -1;

void depth_first_search(Relations& list_graph, int started_user_id, vector<int>& connected_group, int group_id) {
  stack<int> visited_path;
  visited_path.push(started_user_id);
  connected_group[started_user_id] = group_id;

  while(!visited_path.empty()) {
    int current_user_id = visited_path.top();

    int unvisited_adjacency_user_id = no_user_id;
    for(int i = 0; i < list_graph[current_user_id].size(); i++) {
      int adjacency_user_id = list_graph[current_user_id][i];
      if(connected_group[adjacency_user_id] == no_group) {
        unvisited_adjacency_user_id = adjacency_user_id;
        break;
      }
    }

    if(unvisited_adjacency_user_id != no_user_id) {
      connected_group[unvisited_adjacency_user_id] = group_id;
      visited_path.push(unvisited_adjacency_user_id);
    } else {
      visited_path.pop();
    }
  }
}


int main() {
  int user_num;
  int relation_num;
  Relations list_graph;

  // Init
  cin >> user_num >> relation_num;
  list_graph.resize(user_num);

  // Construct list graph
  for(int i = 0; i < relation_num; i++) {
    int user1_id, user2_id;
    cin >> user1_id >> user2_id;
    list_graph[user1_id].push_back(user2_id);
    list_graph[user2_id].push_back(user1_id);
  }

  // Group connected component
  vector<int> connected_group(user_num, no_group);

  int group_id = 0;
  for(int i =  0; i < user_num; i++) {
    if(connected_group[i] == no_group) {
      depth_first_search(list_graph, i, connected_group, group_id++);
    }
  }


  // Answer queries
  int query_num;
  cin >> query_num;
  for(int i = 0; i < query_num; i++) {
    int user1_id, user2_id;
    cin >> user1_id >> user2_id;

    if(connected_group[user1_id] == connected_group[user2_id]) {
      cout << "yes";
    } else {
      cout << "no";
    }
    cout << endl;
  }
}

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

using namespace std;

static const int MAX = 100000;

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

void dfs (int r, int c) {

    int i, u, v;

    stack<int> S;
    S.push(r);
    color[r] = c;

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

}

void assignColor () {

    int id = 1, i, u;

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

}

int main () { 

    int s, t, m, q, i;

    cin >> n >> m;

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

    assignColor ();

    cin >> q;

    for (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<queue>

using namespace std;

static const int N = 100000;

int m,n;

queue<int> Q[N];
int color[N];

void visit(int a,int c){
    color[a] = c;
   while( !(Q[a].size() == 0) ){
        int u = Q[a].front();
        if(color[u] == 0) visit(u,c);
        Q[a].pop();
    }
}

int main(){
    scanf("%d",&n);
    scanf("%d",&m);

    for(int i=0; i<n; i++) color[i] = 0;

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

    for(int i=0; i<n; i++){
        if(color[i] == 0){
            visit(i,cc);
            cc++;
        }
    }

    int l; 
    scanf("%d",&l);
    for(int i =0 ;i<l;i++){
        int x,y;
        scanf("%d",&x);
        scanf("%d",&y);

        if(color[x] == color[y]) printf("yes\n");
        else printf("no\n");

    }

}

C++: #include<iostream> 
#include <list> 
#define N 100001
using namespace std; 
int n, loop;
int color=0;

int flag[N];

bool *visited = new bool[N];

class Graph{ 
    int V; 
    list<int> *adj; 
  
public: 
    void DFSUtil(int v); 
    Graph(int V);  
    void addEdge(int v, int w); 
    void connectedComponents(); 
}; 
  
void Graph::connectedComponents() { 
    for(int v = 0; v < V; v++){
        visited[v] = false; 
	}
  
    for (int v=0; v<V; v++) { 
        if (visited[v] == false) {
        	color++;
            DFSUtil(v); 
        } 
    } 
}
  
void Graph::DFSUtil(int v) 
{ 
    visited[v] = true;
    flag[v]=color;
    list<int>::iterator i; 
    for(i = adj[v].begin(); i != adj[v].end(); ++i){
        if(!visited[*i]){
            DFSUtil(*i); 
		}
	}
} 
  
Graph::Graph(int V) { 
    this->V = V; 
    adj = new list<int>[V]; 
} 
  
void Graph::addEdge(int v, int w) { 
    adj[v].push_back(w); 
    adj[w].push_back(v); 
} 

int main() {
  	int i, x,y,uji,j;
  	cin>>n>>loop;
	Graph g(n);
	 
	
	
  	for(i=0; i<loop; i++){
		cin>>x>>y;
		g.addEdge(x,y);
	}
	
	for(j= 0; j < n; j++){
	   visited[j] = false; 
	   flag[j]=0;
	}
	g.connectedComponents();
	
	cin>>uji;
	int prev=-1;
	for(i=0; i<uji; i++){
		cin>>x>>y;
		
		if(flag[y]==flag[x]){
			cout<<"yes"<<endl;
		}else{
			cout<<"no"<<endl;
		}
		prev=x;
	}
    return 0; 
}