Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
	"bytes"
)

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std;
static const int MAX = 100000;
int n;
int color[MAX];
vector<int> v[MAX];
void b(int s, int t){
	stack<int> S;
	S.push(s);
	int tp;
	color[s] = t;
	while (!S.empty()){
		int tp = S.top(); S.pop();
		for (int i = 0; i < v[tp].size(); i++){
			if (color[v[tp][i]] == MAX){
				color[v[tp][i]] = t;
				S.push(v[tp][i]);
			}
		}
	}
}
void a(){
	for (int i = 0; i < n; i++){
		color[i] = MAX;
	}
	int id = 0;
	for (int i = 0; i < n; i++){
		id++;
		if(color[i] == MAX)b(i,id);
	}
}
int main(){
	int m, s, t;
	int q;
	cin >> n >> m;
	for (int i = 0; i < m; i++){
		cin >> s >> t;
		v[s].push_back(t);
		v[t].push_back(s);
	}
	a();
	cin >> q;
	for (int i = 0; i < q; i++){
		cin >> s >> t;
		if (color[s] == color[t]) cout << "yes" << endl;
		else cout << "no" << endl;
 	}
	for (int i = 0; i < n; i++){
		cout << color[i] << endl;
	}
	return 0;
}

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

void dfs(int r, int n, vector<int> G[], int con_id, int connection[]) {
    stack<int> S;
    S.push(r);
    connection[r] = con_id;

    while (!S.empty()) {
        int u = S.top();
        S.pop();

        for (int i = 0; i < G[u].size(); i++) {
            int v = G[u][i];
            if (connection[v] == NIL) {
                connection[v] = con_id;
                S.push(v);
            }
        }
    }
}

int main() {
    int n, s, t, m, q;
    cin >> n >> m;
    vector<int> G[n];

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

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

    cin >> q;

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

    return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
vector<int> G[100];
vector<int> A;
int isVisited[100];

bool enableConnection(int l, int o)
{
    if (isVisited[l] == 1) {
	return false;
    }
    //?¨???????????????????????????????????????????????????????
    for (int i = 0; i < G[l].size(); i++) {
	if (G[l][i] == o) {
	    return true;
	}
	if (enableConnection(G[l][i], o)) {
	    return true;
	}
    }
    return false;
}

int main()
{
    int a, c;
    cin >> a;
    cin >> c;
    //setup
    for (int i = 0; i < c; i++) {
	int n;
	int m;
	cin >> n >> m;
	G[n].push_back(m);
	isVisited[i] = 0;
    }

    //then
    int b = 0;
    cin >> b;
    for (int i = 0; i < b; i++) {
	int l;
	int o;
	cin >> l;
	cin >> o;
	if (enableConnection(l, o)) {
	    cout << "yes" << endl;
	} else {
	    cout << "no" << endl;
	}
    }
    return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int maxx=100100;
vector<int> A[maxx];
int color[maxx];
int c=1;

void dfs(int u){
	if(color[u]!=-1) return ;//当该节点已经属于某一联通分量时，不需要染色
	color[u]=c;
	int si=A[u].size();
	for(int i=0;i<si;i++){
		if(color[A[u][i]]==-1){//注意是对A[u][i]进行染色
			dfs(A[u][i]);
		}
	}
	return ;
}

int main(){
	int n,q,a,b;//这里有个问题，n如果放在global里定义的话会出现问题，只能在main里定义，不知道为什么
	cin>>n>>q;
	for(int i=0;i<=maxx;i++) color[i]=-1;
	for(int i=1;i<=q;i++){
		cin>>a>>b;
		A[a].push_back(b);
		A[b].push_back(a);
	}
	for(int i=0;i<n;i++){
		dfs(i);
		c++;
	}
	cin>>q;
	for(int i=1;i<=q;i++){
		cin>>a>>b;
		if(color[a]==color[b]) 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;
}

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) des(u,id++);
  }
 }
int main (){
  int s,t,q,m;

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

C++: #include <iostream>

using namespace std;

const int N = 100000;
int A[N][N] = {{0}};
int n;

bool dfs(int s, int t) {
    if (A[s][t] == 1) {
        return true;
    }
    for (int i = 0; i < n; ++i) {
        if (i != t && A[s][i] != 0) {
            if (dfs(i, t)) {
                return true;
            }
        }
    }
    return false;
}

int main() {
    int m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int s, t;
        cin >> s >> t;
        A[s][t] = 1;
    }

    int q;
    cin >> q;
    for (int j = 0; j < q; ++j) {
        int x, y;
        cin >> x >> y;

        if (dfs(x, y)) {
            cout << "yes";
        } else {
            cout << "no";
        }
        cout << endl;
    }

    return 0;
}