Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX = 100000
	NIL = -1
)

var (
	n, m, q, s, t int
	G             [MAX][]int //連結リスト
	color         [MAX]int
)

func dfs(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

//幅優先探索で、頂点を色付け
func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			dfs(i, id)
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n, m = nextInt(), nextInt()
	for i := 0; i < m; i++ {
		s, t = nextInt(), nextInt()
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)

	}
	assignColor()
	q = nextInt()
	for i := 0; i < q; i++ {
		s, t = nextInt(), nextInt()
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}

}

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

const int Max = 100000;
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){
                S.push(v);
                color[v] = c;
                // cout << color[v] << endl;
            }
        }
    }
}
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 m, u, v, q;
    cin >> n >> m;
    for(int i=0; i<m; i++){
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }

    assignColor();
    
    cin >> q;
    for(int i=0; i<q; i++){
        cin >> u >> v;
        cout << color[u] << color[v] << endl;
        if(color[u] == color[v]) cout << "yes\n";
        else cout << "no\n";
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
//#define ll long long
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
#define rep(i, n) REP(i, 0, n)
#define rep_rev(i, n) for (int (i) = (int)(n) - 1 ; (i) >= 0 ; --(i))
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
#define all(x) (x).begin(),(x).end()
ll t1,t2;
const ll mod = 1000000007;
const int INF = 1e9;
const ll INFLONG = 1e18;
void swap(ll *x,ll *y){
  ll tmp;
  tmp = *x;
  *x = *y;
  *y = tmp;
}
vector<ll> kinds(100000,-1);
vector<ll> vec[100000];
void dfs(ll k,ll num){
  if(kinds[k] != -1){
    return;
  }
  kinds[k] = num;
  for(auto itr:vec[k]){
    dfs(itr,num);
  }
}
int main(){
  ll n,m;
  cin >> n >> m;
  rep(i,m){
    cin >> t1 >> t2;
    vec[t1].push_back(t2);
    vec[t2].push_back(t1);
  }
  rep(i,n){
    dfs(i,i);
  }
  ll q;
  cin >> q;
  rep(i,q){
    cin >> t1 >> t2;
    if(kinds[t1] == kinds[t2]){
      cout << "yes" << endl;
    }else{
      cout << "no" << endl;
    }
  }
  return 0;
}

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

vector<int> list[100000];
int num[100000];
int n,m;

void bfs(int s,int sign){

  queue<int> Q;
  Q.push(s);

  while(!Q.empty()){
    int k=Q.front(); Q.pop();
    for(int i=0;i<list[k].size();i++){
      int temp=list[k][i];
      if(num[temp]==-1){ //未訪問なら
        Q.push(temp); num[temp]=sign;
      }
    }
  }
}
int main(){

  cin>>n>>m;

  for(int i=0;i<m;i++){
    int u,v; cin>>u>>v;
    list[u].push_back(v);
    list[v].push_back(u);
  }
  fill(num,num+100000,-1);
  int q; cin>>q;

  int sign=0;
  for(int i=0;i<n;i++){
    if(num[i]==-1){
      bfs(i,sign++);
    }
  }
  for(int i=0;i<q;i++){
    int s,t; cin>>s>>t;
    if(num[s]==num[t] && (num[s]!=-1 || num[t]!=-1)){
      cout<<"yes\n";
    }else{
      cout<<"no\n";
    }
  }

}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int N = 100000;
static const int NIL = -1;
int n;
vector<int> G[N];
int reach[N];
void dfs(int r, int c) {
    stack<int> S;
    S.push(r);
    reach[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 (reach[v] == NIL) {
                reach[v] = c;
                S.push(v);
            }
        }
    }
}
void assign() {
    int id = 1;
    for (int i = 0; i < n; i++) reach[i] = NIL;
    for (int u = 0; u < n; u++)
        if (reach[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);
    }
    assign();
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> s >> t;
        if (reach[s] == reach[t]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}

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

void boo(int b[], int y[], int w, int m){
	for(int i = 0; i < m; i++){
		if(b[i] == w) {
			if(y[b[i + m]] != -1)
				y[b[i + m]] = 1;
		}
	}
	for(int i = m; i < m * 2; i++){
		if(b[i] == w) {
			if(y[b[i - m]] != -1)
				y[b[i - m]] = 1;
		}
	}
}

int foo(int a, int s, int b[], int m, int n){
	int z = 0, y[100100] = {}, k = 1;
	y[a] = 1;
	while(k){
		k = 0;
		for(int i = 0; i < n; i++){
			if(y[i] == -1) continue;
			else if(y[i] == 1){ 
				if(i == s){
					z = 1;
					k = 0;
					break;
				} else{
					k = 1;
					y[i] = -1;
					boo(b, y, i, m);
				}
			}
		}
	}
	return z;
	
}

int main(){
	int b[200100];
	int n, m, q, a, s;
	int i, j, k;
	cin>>n>>m;
	for(i = 0; i < m; i++){
		cin>>b[i]>>b[m + i];
	}

	cin>>q;

	for(i = 0; i < q; i++){
		cin>>a>>s;
		if(foo(a, s, b, m, n) == 1){
			cout<<"yes"<<endl;
		} else {
			cout<<"no"<<endl;
		}
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
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 topId, int c) {
    stack<int> S;
    S.push(topId);
    color[topId] = 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;
}