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>

using namespace std;

int main(){
	int n, m, s, t, q, i, j;
	int id, color = 0;
	bool answer_yes = false;

	// enter
	cin >> n >> m;
	vector< vector<int> > Adj(n);
	vector<int> Stack;
	vector<int> Check(n, 0);
	vector<int>::iterator it, itend;
	for (i = 0; i < m; i++) {
		cin >> s >> t;
		Adj[s].push_back(t);
		Adj[t].push_back(s);
	}

	// calculate
	for (i = 0; i < n; i++) {
		if (Check[i] == 0) {
			color++;
			Stack.push_back(i);
			Check[i] = color;
			while (!Stack.empty()) {
				id = Stack.back();
				itend = Adj[id].end();
				for (it = Adj[id].begin(); it != itend; it++) {
					if (!Check[*it]) {
						Stack.push_back(*it);
						Check[*it] = color;
						break;
					}
				}
				if (it == itend) {
					Stack.pop_back();
				}
			}
		}
	}

	//find
	cin >> q;
	for (i = 0; i < q; i++) {
		cin >> s >> t;		
		if (Check[s] == Check[t]) {
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
		}
	}
	
	return 0;
}

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

#define MAX 100000

int n;
vector<int> elist[MAX];
int group[MAX]; //or bool visited

void dfs(int v,int groupParent){
    group[v] = groupParent;
    for (int i :elist[v]){
        if(group[i] == -1)dfs(i,groupParent);
    }
}

void assignGroup(){
    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 main(){
    freopen("in", "r", stdin);
    int m;
    cin >> n >> m;
    //NOTE : vertex id is 0 ~ n-1
    for (int i = 0; i < m; ++i){
        int s,t;
        cin >> s >> t;
        elist[s].push_back(t);
        elist[t].push_back(s);
    }
    assignGroup();
    int q;
    cin >> q;
    for (int j = 0; j < q; ++j) {
        int s,t;
        cin >> s >> t;
        string result = group[s] == group[t] ? "yes" : "no";
        cout << result << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <vector>
#include <stack>
static const int MAX = 100000;
static const int NIL = -1;

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

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 j = 0; j < n; j++) {
        if (color[j] == NIL) dfs(j, id++);
    }
}

int main() {
    int m, s, t, q;
    
    scanf("%d %d", &n, &m);
    for (int i = 0; i < m; i++) {
        scanf("%d %d", &s, &t);
        G[t].push_back(s);
        G[s].push_back(t);
    }
    
    assignColor();
    
    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<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<string.h>
using namespace std;
#define NIL -2000000001
#define MAX 100000

int n, m;
int color[MAX];
int d[MAX];
int colorId = 0;
vector<int> G[MAX];
queue<int> queueBfs;


void assignColor() {
  queueBfs.push(0);
  color[0] = colorId;

  while (!queueBfs.empty()) {
    int front = queueBfs.front();

    int next = NIL;
    for (int i = 0; i < G[front].size(); i++) {
      if (color[G[front][i]] == NIL) {
        next = G[front][i];
        queueBfs.push(G[front][i]);
        color[G[front][i]] = colorId;
      }
    }
    queueBfs.pop();

    // NILになっている島を探す
    if (queueBfs.empty()) {
      for (int i = 0; i < n; i++) {
        if (color[i] == NIL) {
          queueBfs.push(i);
          colorId++;
          color[i] = colorId;
          break;
        }
      }
    }

  }
}

int main() {
  scanf("%d %d", &n, &m);
  for ( int i = 0; i < n; i++ ) color[i] = NIL;
  for ( int i = 0; i < m; i++ ) {
    int v1, v2;
    scanf("%d %d", &v1, &v2);
    G[v1].push_back(v2);
    G[v2].push_back(v1);
  }

  // for ( int i = 0; i < n; i++ ) {
  //   for ( int j = 0; j < G[i].size(); j++ ) printf("%d ", G[i][j]);
  //   printf("\n");
  // }

  assignColor();

  int q;
  scanf("%d", &q);
  int v1[q], v2[q];
  for ( int i = 0; i < q; i++ ) {
    scanf("%d %d", &v1[i], &v2[i]);
  }

  for ( int i = 0; i < q; i++ ) {
    if (color[v1[i]] == color[v2[i]]) {
      printf("yes\n");
    } else {
      printf("no\n");
    }
  }

  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
										//can this add const??
void dfs(int root, vector<int> &color, vector<vector<int> > &G, int color_id){
	//unordered_map<int, bool> visited; 
	color[root] = color_id;
	int v_size = G[root].size();
	for(int i = 0; i < v_size; i++){
		if(color[G[root][i]] == -1) dfs(G[root][i],  color, G, color_id);
	}
	//cout << "DEBUG2" << color_id << endl;
}


int main(){
	int n, m;
	cin >> n >> m;
	vector<vector<int> > G(n);//adj list
	for(int i = 0; i < m; i++){
		int u, v;
		cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	//cout << "DEBUG" << endl;
	//assign color
	vector<int> color(n, -1);
	int color_id = 0;
	for(int u = 0; u < n; u++){
		if(color[u] == -1) dfs(u, color, G, color_id++);
	}
	//cout << "DEBUG1" << endl;
	int q;
	cin >> q;
	for(int i = 0; i < q; i++){
		int u, v;
		cin >> u >> v;
		if(color[u] == color[v]) cout << "yes" << endl;
		else cout << "no" << endl;
	}
	
	return 0;
}

C++: #include <iostream>
using namespace std;
 
int main() {
int d[100100];
int m,n,q;
cin>>n>>m;
for(int i=0;i<n;i++){
    d[i]=i;
}
for(int l=0;l<m;l++){
    int a,b;
    cin>>a>>b;
    int j ,h;
    j=a,h=b;
while(d[j]!=j ){
    j=d[j];
}
while(d[h]!=h ){
    h=d[h];
}
if(j!=h){
    d[j]=h;
    d[a]=h;
    d[b]=h;
}
}
cin>>q;
for(int y=0;y<q;y++){
    int a,b;
    cin>>a>>b;
        int j ,h;
    j=a,h=b;
while(d[j]!=j ){
    j=d[j];
}
while(d[h]!=h ){
    h=d[h];
}
    if(j==h){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<endl;
    }
}
    return 0;
}