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<bits/stdc++.h>
#include<limits.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)

#define MAX 100000

vector<vector<int>> adj_list(MAX);
vector<int> visited(MAX, -1);
vector<int> init_visited(MAX, -1);

int group = 0;

void dfs(int node){
    // cout << "init node " << node << endl;
    visited.at(node) = group;
    
    for(int i = 0; ; i++){
        int next_node = adj_list.at(node).at(i);
        // cout << next_node << endl;

        if(next_node != -1 && visited.at(next_node) == -1){
            // cout << "called dfs with " << next_node << endl;
            dfs(next_node);
        }
        if(next_node == -1){
            break;
        }
    }
    // cout << "returned from " << node << endl;
}

int main(){
    int n, m; cin >> n >> m;


    //　隣接リストの作成
    rep(i, m){
        int from, to;
        cin >> from >> to;
        adj_list.at(from).push_back(to);
        adj_list.at(to).push_back(from);
    }
    // 番兵の挿入
    rep(i, n){
        adj_list.at(i).push_back(-1);
    }

    for(int i = 0; i < n; i++){
        if(visited.at(i) == -1){
            dfs(i);
            group++;
        }
    }

    int q; cin >> q;
    rep(i, q){
        int from, to;
        cin >> from >> to;
        if(visited.at(from) == visited.at(to)){
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }

    // cout << "=================" << endl;

    // for(int i = 0; i < m; i++){
    //     cout << "node " << i << " ";
    //     for(int j = 0; j < adj_list.at(i).size(); j++){
    //         cout << adj_list.at(i).at(j) << " ";
    //     }
    //     cout << endl;
    // }

    // rep(i, n){
    //     cout << "node " << i << " " << visited.at(i) << endl;
    // }
}

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

const long MAX = 100000;
const long NIL = -1;

long n;
vector<long> G[MAX];
long color[MAX];

void dfs(long r, long c){
	stack<long> S;
	S.push(r);
	color[r] = c;
	while (!S.empty()){
		long u = S.top();
		S.pop();
		for (long i = 0; i < G[u].size(); i++){
			long v = G[u][i];
			if (color[v] == NIL){
				color[v] = c;
				S.push(v);
			}
		}
	}
}

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

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

	assignColor();

	cin >> q;
	for (long i = 0; i < q; i++){
		cin >> s >> t;
		if (color[s] == color[t]) 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 w,c=1;
stack<int> S;

void dfs(int u){
	color[u]=c;
	for(int i=0;i<A[u].size();i++){
		if(color[i]==-1){
			S.push(i);
			dfs(i);
		}
	}
	int x=S.top();
	S.pop();
	return ;
}

void dye(){
	S.push(0);
	color[0]=c;
L1:	while(S.empty()==0){
		int x=S.top();
		dfs(x);
	}
	for(int i=0;i<w;i++){
		if(color[i]==-1){
			S.push(i);color[i]=c++;goto L1;
		}
	}
	return ;
}

int main(){
	int q,a,b;
	cin>>q>>w;
	for(int i=0;i<=maxx;i++) color[i]=-1;
	while(--q){
		cin>>a>>b;
		A[a].push_back(b);
		A[b].push_back(a);
	}
	dye();
	cin>>q;
	while(q--){
		cin>>a>>b;
		if(color[a]==color[b]) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}

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

struct Edge
{
  int from;
  int to;

  friend bool operator==(const Edge &l, const Edge &r)
  {
    return l.from == r.from && l.to == r.to;
  }
};

int main()
{
  int n, m, v1, v2;
  scanf("%d%d", &n, &m);
  vector<Edge> v;

  for (int i = 0; i < m; i++)
  {
    scanf("%d%d", &v1, &v2);
    v.push_back({v1, v2});
  }

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

  int id = 0;
  while (!v.empty())
  {
    queue<Edge> q;
    q.push(v.front());
    while (!q.empty())
    {
      Edge edge = q.front();
      q.pop();
      group[edge.from] = id;
      group[edge.to] = id;
      v.erase(remove(v.begin(), v.end(), edge), v.end());
      // auto itr = find(v.begin(), v.end(), edge);
      // if (itr != v.end())
      //   v.erase(itr);
      for (auto const &e : v)
        if (e.from == edge.from || e.to == edge.from || e.from == edge.to || e.to == edge.to)
          q.push(e);
    }
    id++;
  }

  int q;
  scanf("%d", &q);

  for (int i = 0; i < q; i++)
  {
    scanf("%d%d", &v1, &v2);
    if (group[v1] != -1 && group[v1] == group[v2])
      printf("yes\n");
    else
      printf("no\n");
  }

  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <stack>
using namespace std;
#define maxn 10005
#define NTL -1
int color[maxn];
vector<int> g[maxn];
stack<int> s;
int n,m,q;
void tarjan(int r,int c)
{
    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]==NTL)
            {
                color[v]=c;
                s.push(v);
            }
        }
    }
}
void assigncolor()
{
    int id=0;
    for(int i=0;i<n;i++)
        color[i]=NTL;
    for(int u=0;u<n;u++)
    {
        if(color[u]==NTL)
            tarjan(u,id++);
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    assigncolor();
    scanf("%d",&q);
    for(int i=0;i<q;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        if(color[u]==color[v])
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}