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

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

  int id = 1;
  
  for ( int i = 0; i < n; ++i) {
    color[i] = NIL; //preset
  }
  
  for ( int u = 0; u < n; ++u) {
    if ( color[u] == NIL ) {
      dfs(u,id);
      id++;
    }
  }
  
  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<iostream>
#include<unordered_map>
int main(){
  return 0;
}

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

using namespace std;

class UnionFind {
public:
	vector<int> par;

	UnionFind(int n) {
		par.resize(n);
		for (int i = 0; i < n; i++) par[i] = i;
	}

	UnionFind() {};

	int root(int x) {
		if (par[x] == x)return x;
		else return par[x] = root(par[x]);
	}

	void unite(int x, int y) {
		x = root(x);
		y = root(y);
		if (x == y)return;
		par[x] = y;
	}

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

int main() {
	int n, m, s, t, q;
	cin >> n >> m;
	UnionFind uf(n);
	while (m--) cin >> s >> t, uf.unite(s, t);
	cin >> q;
	while (q--)cin >> s >> t, cout << (uf.same(s, t) ? "yes" : "no") << endl;
}

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

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

bool bfs(int s, int t){

  bool used[m];
  for(int i=0;i<m;i++) used[i]=false; //初期化

  queue<int> Q;
  Q.push(s); used[s]=true;

  while(!Q.empty()){
    int k=Q.front(); Q.pop();
    for(int i=0;i<list[k].size();i++){
      int temp=list[k][i];
      if(temp==t){return true;}
      if(!used[temp]){ //未訪問なら
        Q.push(temp); used[temp]=true;
      }
    }
  }
  return false;
}
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);
  }

  int q; cin>>q;

  for(int i=0;i<q;i++){
    int s,t; cin>>s>>t;
    if(bfs(s,t)){
      cout<<"yes\n";
    }else{
      cout<<"no\n";
    }
  }

}

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

int n;
vector<int> g[100001];
char c[100001];
int visit[100001];
int finish[100001];
int group[100001];
int m;
int main() {

    cin >> n >> m;
    for (int i1 = 1; i1 <=m; ++i1) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }

   stack<int> s;
    for (int l = 0; l < n; ++l) {
        c[l] = 'w';

    }
    int time = 0;
    int start = -1;
    int v;
    int nowg = 0;
    while(true) {
        nowg++;
        start++;
        if(start == n) break;
        if(c[start] == 'w') {
            time++;
            s.push(start);
            c[start] = 'g';
            group[start] = nowg;
            visit[start] = time;
            while (s.size() != 0) {
                time++;
                v = s.top();
                int next = -1;
                for (int i = 0; i < g[v].size() ; ++i) {
                    if(c[g[v].at(i)] == 'w') {
                        next = g[v].at(i);
                        break;
                    }
                }
                if(next == -1) {
                    c[v] = 'b';
                    finish[v] = time;
                    s.pop();
                } else {
                    c[next] = 'g';
                    group[next] = nowg;
                    visit[next] = time;
                    s.push(next);
                }
            }
        }
    }

    int q;
    cin >> q;
    for (int m = 0; m < q; ++m) {
        int a, b;
        cin >> a >> b;
        if(group[a] == group[b]) cout << "yes";
        else cout << "no";
        cout << endl; 


    }



    return 0;
}