Go: package main

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

const (
	MAX = 100000
	NIL = -1
)

var (
	n     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() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		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()
	scanner.Scan()
	q, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < q; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

C++: //By Vlgd
//By Vlgd
#include<bits/stdc++.h>
using namespace std;
const int N=100000;
int n,m,d[N],t=0;
vector<int>G[N];
stack<int>S;
void dfs(int r,int cnt){
	S.push(r);
	d[r]=cnt;
	while(!S.empty()){
		int u=S.top();S.pop();
		for (int i=0;i<G[u].size();i++){
			int v=G[u][i];
			if (d[v]==-1){
				d[v]=cnt;
				S.push(v);
			}
		}
	}
}
int main(){
	scanf("%d%d",&n,&m);
	int u,v;
	for (int i=0;i<m;i++){
		scanf("%d%d",&u,&v);
		G[u].push_back(v);
		G[v].push_back(u); 
	}
	memset(d,-1,sizeof(d));
	for (int i=0;i<n;i++) if (d[i]==-1) dfs(i,t++);
	int q;
	scanf("%d",&q);
	for (int i=0;i<q;i++){
		scanf("%d%d",&u,&v);
		if (d[u]==d[v]) printf("yes\n");
		else printf("no\n");
	} 
}

C++: // ALDS1_11_D.cpp
// Graph I - Connected Components

#include <iostream>
#include <vector>
using namespace std;

bool isFriend(vector<vector<int>> &sns, vector<int> &arrived, int x, int y)
{
    if (sns[x][y] == 1) return true;
    bool res = false;
    arrived[x] = 1;
    for (int i = 0; i < sns.size(); i++) {
        if (sns[x][i] == 1 && !arrived[i]) {
            bool tmp = isFriend(sns, arrived, i, y);
            if (tmp) {
                res = true;
                break;
            }
        }
    }
    return res;
}

int main()
{
    int m, n, q;
    cin >> n >> m;
    vector<vector<int> > sns(n, vector<int>(n, 0));
    int x, y;
    for (int i = 0; i < m; i++) {
        cin >> x >> y;
        sns[x][y] = 1;
        sns[y][x] = 1;
    }

    cin >> q;
    while (q--) {
        vector<int> arrived(n);
        cin >> x >> y;
        bool res = isFriend(sns, arrived, x, y);
        if (res) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}

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

int main() {
	int n,m,q,k;
	int s,t;
	int i,j;
	queue<int> nq;
	vector<vector<int> > node;
	vector<int> ngrp;

	scanf("%d%d",&n,&m);
	node.resize(n);
	ngrp.resize(n);

	for(i=0;i<m;++i) {
		scanf("%d%d",&s,&t);
		node[s].push_back(t);
		node[t].push_back(s);
	}

	for(i=0;i<n;++i) {
		ngrp[i]=-1;
	}

	for(i=0; i<n; ++i) {
		if(ngrp[i]==-1) {
			nq.push(i);
			ngrp[i]=i;
			while(!nq.empty()) {
				k=nq.front();
				for(j=0;j<(int)node[k].size();++j) {
					if(ngrp[node[k][j]]==-1) {
						ngrp[node[k][j]]=i;
						nq.push(node[k][j]);
					}
				}
				nq.pop();
			}
		}
	}
	scanf("%d",&q);
	for(i=0;i<q;++i) {
		scanf("%d%d",&s,&t);
		printf((ngrp[s]==ngrp[t])?"yes\n":"no\n");
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
const int MAX=100000,NIL=-1;
int n,color[MAX];
vector<int> G[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 assign(){
    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(void){
    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(color[s]==color[t]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
}

C++: //
//
//  ad-algoD.cpp - created by us162022
//
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
  int n,m,join[100000],num[100000],s,t;
  cin >> n >> m;

  for(int i=0; i<n; i++){
    join[i] = i;
    num[i] = 1;
  }

  for(int i=0; i<m; i++){
    cin >> s >> t;

    if(join[t] != join[s]){
      int js = join[s];
      int jt = join[t];

      join[t] = join[s]; 
      num[js] += 1;
      num[jt] -= 1;
      
      
      for(int a=0; 0<num[jt]; a++){
	cout << "^^ ";
	if(join[a]==jt && jt != a){
	  join[a] = js;
	  num[js] += 1;
	  num[jt] -= 1;
	}
      }
    }
  }

  int q;
  cin >> q;
  bool answer[10000];

  for(int i=0; i<q; i++){
    answer[i] = false;
    cin >> s >> t;

    if(join[s]==join[t])
      answer[i] = true;
  }

  for(int i=0; i<q; i++){
    if(answer[i])
      cout << "yes\n";

    else
      cout << "no\n";
  }
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#define N 100000

using namespace std;
vector<int> v[N];
int group[N]={};
int g=0;

bool search(int s,int t){
  if(group[s]==group[t]&&group[s]>0) return true;
  if(group[s]>0) return false;
  group[s]=++g;
  queue<int> q;
  q.push(s);
  while(!q.empty()){
    int a=q.front();
    for(auto i=v[a].begin();i!=v[a].end();i++){
      if(group[*i]==0){
        group[*i]=g;
        q.push(*i);
      }
    }
    q.pop();
  }
  if(group[s]==group[t]&&group[s]>0){ 
    return true;
  }else{
    return false;
  }
}

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

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

}

C++: #include <iostream>
#include <cstdio>
#define MAX 100000

using namespace std;

int main(){
  int a[MAX]={};
  int n,m; cin>>n>>m;
  int s,t;
  int group=1;
  for(int i=0;i<m;i++){
    cin>>s>>t;
    if(a[s]==0 && a[t]==0){
      a[s]=a[t]=group;
      group++;
    }else if(a[s]==0){
      a[s]=a[t];
    }else if(a[t]==0){
      a[t]=a[s];
    }else{
      int tmp=a[t];
      a[t]=a[s];
      for(int j=0;j<i;j++){
        if(a[j]==tmp) a[j]=a[s];
      }
    }
  }

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

}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
const int maxn = 100001;

vector<int> G[maxn];
int vis[maxn];

void DFS(int u, int k)
{
    vis[u] = k;
    for(vector<int>::iterator it = G[u].begin(); it != G[u].end(); it++)
        if(vis[*it] == -1)
            DFS(*it, k);
}

int main()
{
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < m; i++)
    {
        int x, y;
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for(int j = 0; j < n; j++)
            vis[j] = -1;
    for(int i = 0; i < n; i++)
        if(vis[i] == -1)
            DFS(i, i);
    int q;
    cin >> q;
    for(int i = 0; i < q; i++)
    {
        int x, y;
        cin >> x >> y;
        if(vis[y] == vis[x])
            cout << "yes" <<endl;
        else
            cout << "no" <<endl;
    }
    return 0;
}