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 <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 c=1;

void dfs(int u){
	if(color[u]!=-1) return ;//当该节点已经属于某一联通分量时，不需要染色
	color[u]=c;
	int si=A[u].size();
	for(int i=0;i<si;i++){
		if(color[A[u][i]]==-1){//注意是对A[u][i]进行染色
			dfs(A[u][i]);
		}
	}
	return ;
}

int main(){
	int n,q,a,b;//这里有个问题，n如果放在global里定义的话会出现问题，只能在main里定义，不知道为什么
	cin>>n>>q;
	for(int i=0;i<=maxx;i++) color[i]=-1;
	for(int i=1;i<=q;i++){
		cin>>a>>b;
		A[a].push_back(b);
		A[b].push_back(a);
	}
	for(int i=0;i<n;i++){
		dfs(i);
		c++;
	}
	cin>>q;
	for(int i=1;i<=q;i++){
		cin>>a>>b;
		if(color[a]==color[b]) 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);
            }        
        }
    }
}

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);
    }
   }
  }
 }
  
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) des(u,id++);
  }
 }
int main (){
  int s,t,q,m;

  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>

using namespace std;

const int N = 100000;
int A[N][N] = {{0}};
int n;

bool dfs(int s, int t) {
    if (A[s][t] == 1) {
        return true;
    }
    for (int i = 0; i < n; ++i) {
        if (i != t && A[s][i] != 0) {
            if (dfs(i, t)) {
                return true;
            }
        }
    }
    return false;
}

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

    int q;
    cin >> q;
    for (int j = 0; j < q; ++j) {
        int x, y;
        cin >> x >> y;

        if (dfs(x, y)) {
            cout << "yes";
        } else {
            cout << "no";
        }
        cout << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
#include<cstdio>
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 ass()
{
  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);
    }
  ass();
  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 <cstdio>
#include <iostream>
#include <vector>

#define N 100000

using namespace std;

vector<int> G[N];
int color[N];

void dfs(int s,int c){
  color[s]=c;
  for(int i=0;i<G[s].size();i++){
    if(color[G[s][i]]!=c) dfs(G[s][i],c);
  }
}

int main(){
  int n,m;
  scanf("%d %d",&n,&m);
  for(int i=0;i<n;i++) color[i]=-1;
  for(int i=0;i<m;i++){
    int s,t;
    scanf("%d %d",&s,&t);
    G[s].push_back(t);
    G[t].push_back(s);
  }
  int q; scanf("%d",&q);
  for(int i=0;i<n;i++){
    if(color[i]==-1)
      dfs(i,i); //node_id and color_id
  }
  for(int i=0;i<q;i++){
    int s,t;
    scanf("%d %d",&s,&t);
    if(color[s]==color[t]) cout << "yes\n";
    else cout << "no\n";
  }
}

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

using namespace std;

const int kSize = 100001;

vector<int> adj[kSize];
bool isMarked[kSize];
int d[kSize];
int cc = 0;

void dfs(int v) {
    isMarked[v] = true;
    d[v] = cc;

    for (int i = 0; i < adj[v].size(); ++i) {
        int u = adj[v][i];
        if (!isMarked[u]) {
            dfs(u);
        }
    }
}

int main() {
    int n, m;
    fill(isMarked, isMarked + kSize, false);
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int from, to;
        cin >> from >> to;
        adj[from].push_back(to);
        adj[to].push_back(from);
    }

    for (int i = 0; i < n; ++i) {
        if (!isMarked[i]) {
            dfs(i);
            cc++;
        }
    }

    int q;
    cin >> q;
    for (int i = 0; i < q; ++i) {
        int from, to;
        cin >> from >> to;
        if (d[from] == d[to]) {
            cout << "yes";
        } else {
            cout << "no";
        }
        cout << endl;
    }
    return 0;
}