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>
using namespace std;
#include<vector>
#include<stack>
#define MAX 100000
int n,m;
vector<int>G[MAX];
stack<int>mp;
int color[MAX];

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

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

int main()
{
	cin >> n >> m;
	int x,y; 
	for (int i = 0; i < m ;i++){
		cin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	assignColor();
	cin >> m;
	for (int i = 0 ;i < m;i++){
		cin >> x >> y;
		if (color[x] == color[y]){
			cout << "yes" << endl;
		}
		else{
			cout << "no" << endl;
		}
	}
	return 0;
}

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
 
struct node {
	int id;
	vector<int> rel;
};

node* nodes;

void dfs(int start, int cur, int* h){
	if(h[cur] == -1){
		h[cur] = start;
		for(int i = 0; i < nodes[cur].rel.size(); i++){
			dfs(start, nodes[cur].rel[i], h);
		}
	}
}

int main() {
	int n, m, q;
	scanf("%d %d", &n, &m);
	 
	nodes = new node[n];
	for(int i = 0; i < n; i++){
		nodes[i].id = i;
	}
	while(m--){
		int l, r;
		scanf("%d %d", &l, &r);
		nodes[l].rel.push_back(r);
		nodes[r].rel.push_back(l);
	}
	
	int *h = new int[n];
	for(int i = 0; i < n; i++){
		h[i] = -1;
	}
	for(int i = 0; i < n; i++){
		dfs(i, i, h);
	}
	
	scanf("%d", &q);
	while(q--){
		int l, r;
		scanf("%d %d", &l, &r);
		printf("%s\n", h[l]==h[r] ? "yes" : "no");
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#define MAX_NODE 100000
#define MAX_EDGE 100000

using namespace std;

int n;//num of nodes
int e;//num of edges
int group[MAX_NODE+1];//0オリジン
int q;

bool neighbor_a[MAX_NODE+1];
vector<int> neighbor_v[MAX_NODE+1];//連接リスト

void addNeighbor(int num,int ori){//oriを含む連結グラフをつくる
	int ngb;
	
	for(int i = 0;i < neighbor_v[num].size();i++){//深さ優先探索
		if(neighbor_a[neighbor_v[num][i]] == false){
			neighbor_a[neighbor_v[num][i]] = true;
			neighbor_v[ori].push_back(neighbor_v[num][i]);
			ngb = neighbor_v[num][i];
			addNeighbor(ngb,ori);
		}
	}
}


int main(){
	int v_1,v_2;
	scanf("%d %d",&n,&e);

	for(int i = 0;i < e;i++){
		scanf("%d %d",&v_1,&v_2);
		neighbor_v[v_1].push_back(v_2);
		neighbor_v[v_2].push_back(v_1);
	}

	for(int i  = 0;i < n;i++) group[i] = -1;

	for(int i = 0;i < n;i++){
		if(group[i] != -1) continue;

		for(int j = 0;j < n;j++) neighbor_a[j] = false;//iの連接配列
		neighbor_a[i] = true;
		neighbor_v[i].push_back(i);

		addNeighbor(i,i);

		for(int j = 0;j < neighbor_v[i].size();j++) group[neighbor_v[i][j]] = i;

	}
	//for(int i = 0;i < n;i++) cout <<group[i] <<" ";
	//for(int i = 0;i < neighbor_v[5].size();i++) cout <<neighbor_v[5][i] <<" ";

	scanf("%d",&q);

	for(int i = 0;i < q;i++){
		scanf("%d %d",&v_1,&v_2);
		if(group[v_1] == group[v_2]) cout <<"yes" <<endl;
		else cout <<"no" <<endl;
	}
	return 0;
}

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

int n;
int parent[100000];
int r[100000]; //rank

//Union-find tree
void init(int n){
  for(int i = 0;i < n;i++){
    parent[i] = i;
    r[i] = 0;
  }
}

int find(int x){
  if(parent[x] == x) return x;
  else return parent[x] = find(parent[x]);
}

void unite(int x, int y){
  x = find(x);
  y = find(y);
  
  if(x == y) return ;
  
  if(r[x] < r[y]) parent[x] = y;
  else{
    parent[y] = x;
    if(r[x] == r[y]) r[x]++;
  }
}

bool same(int x, int y){
  return find(x) == find(y);
}

int main(){
  
  int m, q, s, t;

  //input
  cin >> n >> m;
  init(n);
  for(int i = 0;i < m;i++){
    cin >> s >> t;
    unite(s, t);
  }

  //ouptut
  cin >> q;
  for(int i = 0;i < q;i++){
    cin >> s >> t;
    if(same(s, t)) cout << "yes" << endl;
    else cout << "no" << endl;
  }
  
  return 0;
}

C++: #ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var) \
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

struct Solver {
  explicit Solver(int n) {
    parent_index_.reserve(n);
    for (int i = 0; i < n; ++i) {
      parent_index_.push_back(i);
    }
  }
  void register_friend(int s, int t) {
    auto r1 = get_root(s);
    auto r2 = get_root(t);
    if (r1 != r2) {
      parent_index_[r2] = r1;
    }
  }
  bool can_reach(int s, int t) { return get_root(s) == get_root(t); }

 private:
  int get_root(int i) {
    for (auto cur = i;;) {
      auto next = parent_index_[cur];
      if (next == cur) {
        return cur;
      }
      parent_index_[cur] = parent_index_[next];
      cur = next;
    }
  }

  vector<int> parent_index_;
};

int main() {
  int n, m;
  cin >> n >> m;
  Solver sol(n);
  for (int i = 0; i < m; ++i) {
    int s, t;
    cin >> s >> t;
    sol.register_friend(s, t);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int s, t;
    cin >> s >> t;
    auto ok = sol.can_reach(s, t);
    cout << (ok ? "yes" : "no") << endl;
  }
}