Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	nConns := scanInt(sc)

	graph := make([][]int, nNodes)
	for i := 0; i < nConns; i++ {
		a, b := scanInt(sc), scanInt(sc)
		graph[a] = append(graph[a], b)
		graph[b] = append(graph[b], a)
	}

	groups := make([]int, nNodes)

	var setGroups func(int, int)
	setGroups = func(u int, groupId int) {
		if groups[u] > 0 {
			return
		}
		groups[u] = groupId

		for _, v := range graph[u] {
			setGroups(v, groupId)
		}
	}
	for i := 0; i < nNodes; i++ {
		setGroups(i, i+1)
	}

	nQuestions := scanInt(sc)

	w := bufio.NewWriter(os.Stdout)
	var ans string
	for i := 0; i < nQuestions; i++ {
		a, b := scanInt(sc), scanInt(sc)
		if groups[a] == groups[b] {
			ans = "yes"
		} else {
			ans = "no"
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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();
    if(k==t){return true;}
    for(int i=0;i<list[k].size();i++){
      int temp=list[k][i];
      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<list>
#include<iterator>
using namespace std;

class person
{
public:
	int id;
	bool Isdone = false;
	list <person*> lis;
	bool serch(person* Per)
	{
		Isdone = true;
		for(list<person*>::iterator temp=lis.begin();temp!=lis.end();++temp)
		{
			if ((*temp) == Per)return true;
			else if ((*temp)->Isdone == true)continue;
			else
			{
				if ((*temp)->serch(Per))return true;
			}
		}
		return false;
	}
};

person P[100001];

int main()
{
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++)P[i].id = i;
	for (int i = 0; i < m; i++)
	{
		int p1, p2;
		scanf("%d%d",&p1, &p2);
		P[p1].lis.push_back(&P[p2]);
		P[p2].lis.push_back(&P[p1]);
	}			 
	int q;
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		int p1, p2;
		scanf("%d%d", &p1, &p2);
		if (P[p1].serch(&P[p2]))
		{
			cout << "yes" << endl;
		}
		else
		{
			cout << "no" << endl;
		}
		for (int l = 0; l < n; l++)P[l].Isdone = false;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int n;
vector<int> G[100005];
vector<int> color(100005, -1);
vector<bool> seen(100005, false);

void dfs(int now, int id){
    seen[now] = true;
    color[now] = id;

    for(int i=0; i<G[now].size(); ++i){
        int to = G[now][i];
        if(!seen[to]) dfs(to, id);
    }
}

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

    int id = 0;
    for(int i=0; i<n; ++i){
        if(!seen[i]){
            ++id;
            dfs(i, id);
        }
    }

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

    return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<stack>
#include<cstring>
#include<vector>
using namespace std;
const int MAXN = 1e5+10 ;

vector<int> s[MAXN];
int color[MAXN],n,m;

void dfs(int x,int id)
{
    stack<int> st;
    color[x] = id;
    st.push(x);
    while(!st.empty())
    {
        int u = st.top();   st.pop();
        for(int i = 0; i < s[u].size(); ++i)
        {
            int v = s[u][i];
            if(color[v] == -1)
            {
                color[v] = id;
                st.push(v);
            }
        }
    }
}
void init()
{
    int id = 0;
    memset(color,-1,sizeof color);
    for(int i = 0; i < n; ++i)
    {
        if(color[i] == -1)
            dfs(i,++id);
    }
}

int main(void)
{
    cin>>n>>m;
    int u,v;
    for(int i = 1; i <= m; ++i)
    {
        cin>>u>>v;
        s[u].push_back(v);
        s[v].push_back(u);
    }
    init();
    int q;
    cin>>q;
    while(q--)
    {
        cin>>u>>v;
        if(color[u] == color[v])
            cout<<"yes\n";
        else
            cout<<"no\n";
    }
}

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]){
      cout<<"yes\n";
    }else{
      cout<<"no\n";
    }
  }

}

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;

  string echo[q];

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

  return 0;
}