Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

	for i := 0; i < r; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	assignColor()
	sc.Scan()
	qNum, _ := strconv.Atoi(sc.Text())
	for i := 0; i < qNum; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 0
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int Max = 100000
static const int NIL = 100000
int i, j, v, num, color[Max];
vector<int> list[Max];
 
void dfs(int r, int c){
 stack<int> S;
 S.push(r);
 color[r] = c;
 while( !S.empty() ){
   int u = S.top(); S.pop();
   for(i=0; i<list[u].size(); i++){
     v = list[u][i];
     if(color[v] == NIL){
       color[v] = c;
       S.push(v);
     }
   }
 }
}
  
void assigncolor(){
 int id = 1;
 for(i=0; i<num; i++){
   color[i] = NIL;
 }
 for(j=0; j<num; j++){
   if(color[j] == NIL)
     dfs(i, id++);
 }
}
  
int main(){
 int a, b, trial, start, goal, kaisuu;
 cin >> num >> kaisuu;
  
 for(i=0; i<kaisuu; i++){
   cin >> a >> b;
   list[a].push_back(b);
   list[b].push_back(a);
 }
 
 assigncolor();
 
 cin >> trial;
 
 for(i=1; i<=trial; i++){
   cin >> start >> goal;
   if(color[start] == color[goal]){  
     cout << "yes" << endl;
   } else{
     cout << "no" << endl;
   }
 }
  
 return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define MAX 100000
#define NIL -1
vector<int>G[MAX];
int color[MAX];
int n;
void dfs(int r,int c){
	color[r]=c;
	stack<int>s;
	s.push(r);
	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(){
	memset(color,-1,sizeof(color));
	int id=1;
	for(int u=0;u<n;u++){
		if(color[u]==NIL)dfs(u,id++);
	}
	 
}


int main(){
	int m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int s,t;
		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++){
		int x,y;
		cin>>x>>y;
		if(color[x]==color[y])cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX=10000;
static const int NIL=-1;

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

void dfs(int r,int c){
  stack<int> S;
  int u,i,v;
  
  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]==NIL){
	color[v]=c;
	S.push(v);
      }
    }
  }
}

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

int main(){
  int s,t,m,q;
  int 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 <iostream>
#include <vector>

using namespace std;

vector<int> G[100000], color(100000);
int c;

void dfs(int v) {
	color[v] = c;
	for (int i = 0; i < G[v].size(); ++i) {
		int e = G[v][i];
		if (!color[e]) dfs(e);
	}
}

int main() {
	int n, m; cin >> n >> m;
	while(m--) {
		int s, t; cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}
	for (int i = 0; i < n; ++i) {
		if (!color[i]) {
			c++;
			dfs(i);
		}
	}
	int q; cin >> q;
	while (q--) {
		int s, t; cin >> s >> t;
		if (color[s] == color[t]) cout << "yes" << endl;
		else cout << "no" << endl;
	}

	return 0;
}

C++: #include<cstdio>
#include<vector>
#define N 100000
using namespace std;

vector<int> G[N];
/*
??£??\??????????????????????????°???????????¨??????????????£????????????????????????DFS??§????????????

 */
int labels[N];

void dfs_init(){
    int i;
    for(i=0;i<N; i++)labels[i]=0;
}

/*
  ?????°?????¨??????????£?
  ??§???????????????????????????
  stack???????????°??§???????????????
 */
bool dfs(int u, int v){
    if (u == v) return true;
    labels[u] = 1;
    int i,x;
    for(i=0; i<G[u].size(); i++){
	x = G[u][i];
	dfs(x,v);
    }
    return false;
}

void dfs2(int u){
    labels[u]=1;
    int i;
    for(i=0; i<G[u].size(); i++){
	if (labels[G[u][i]]==0) dfs2(G[u][i]);
    }
}


int main(void) {
    int n,m,i,q, u,v;
    scanf("%d %d",&n,&m);
    for(i=0;i<m;i++){
	scanf("%d %d",&u,&v);
	G[u].push_back(v);
	G[v].push_back(u);
    }

    
    scanf("%d",&q);
    for(i=0;i<q;i++){
	scanf("%d %d", &u, &v);
	dfs_init();
	dfs2(u);
	if(labels[u]>0 && labels[u]==labels[v])printf("yes\n");
	else printf("no\n");
    }
    
    return 0;
}

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

using namespace std;

const int N = 100000;
int n;
int color[N];
vector<int> v[N];

bool isFriend(int s, int t){
  int color[N] = {0};
  queue<int> q;
  int x;
  q.push(s);
  while(!q.empty()){
    x = q.front(); q.pop();
    for(int i=0;i<n;i++){
      if(v[x][i] == 0) continue;
      if(color[i] == 0){
        if(i == t) return true;
        color[i] = 1;
        q.push(i);
      }
    }
  }
  return false;
}

int main(){
  int m;
  cin >> n >> m;
  int s, t;
  for(int i=0;i<m;i++){
    cin >> s >> t;
      v[s].push_back(t);
      v[t].push_back(s);
  }
  int q; cin >> q;
  for(int i=0;i<q;i++){
    cin >> s >> t;
    if(isFriend(s, t)) cout << "yes" << endl;
    else cout << "no" << endl;
  }
  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 [MAX];

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

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

  cin >> n >> m;

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

  assignColor();

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