Go: package main

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

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = nil
	}
	for u := 0; u < n; u++ {
		if color[u] == nil {
			id++
			dfs(u, id)
		}
	}
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

	for i := 0; i < r; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	assignColor()
	sc.Scan()
	qNum, _ := strconv.Atoi(sc.Text())
	for i := 0; i < qNum; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 0
}

C++: #include<bits/stdc++.h>
using namespace std;
int n,m,i,s,t,p[1<<17],q;
int root(int u){
  return p[u]==u?u:p[u]=root(p[u]);
}
int main(void){
  for(i=0;i<1<<17;i++)p[i]=i;
  cin>>n>>m;
  while(m--){
    cin>>s>>t;
    s=root(s);
    t=root(t);
    // if(s>t)swap(s,t);
    if(s!=t)p[t]=s;
  }
  cin>>q;
  while(q--){
    cin>>s>>t;
    cout<<(root(s)==root(t)?"yes":"no")<<endl;
  }
  return 0;
}

C++: #include <stdio.h>

int m[100000] = {0};

void init() {
	int i;
	for ( i = 0; i < 100000; ++i) {
		m[i] = i;
	}
}

int find(int a) {
	if ( a == m[a]) {
		return a;
	}
	return m[a] = find(m[a]);
}

int friend(int x, int y) {
	if ( find(x) == find(y)) {
		return 1;
	} else {
		return 0;
	}
}

void unit (int x, int y) {
	x = find(x);
	y = find(y);
	
	if ( x == y ) {
		return ;
	}
	m[y] = x;
}

int main() {
	int n, s, t, u, v, i;
	init();
	scanf("%d", &n);
	scanf("%d", &s);
	
	for ( i = 0; i < s; ++i) {
		scanf("%d", &u);
		scanf("%d", &v);
		unit( u, v);
	}
	
	scanf("%d", &t);
	
	for ( i = 0; i < s; i++) {
		scanf("%d", &u);
		scanf("%d", &v);
		if ( friend( u, v)) {
			printf("yes\n");
		} else {
			printf("no\n");
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int flag[100000];
vector<int> G[100000];
int color = 0;

void dfs(int f)
{
    for (int i = 0; i < G[f].size(); i++)
    {
        if (flag[G[f][i]] == -1)
        {
            flag[G[f][i]] = color;
            dfs(G[f][i]);
        }
    }
}

int main()
{
    int n, m;
    cin >> n >> m;
    int f, g;
    for (int i = 0; i < m; i++)
    {

        cin >> f >> g;
        G[f].push_back(g);
        G[g].push_back(f);
    }

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

    for (int i = 0; i < n; i++)
    {
        if (flag[i] == -1)
        {
            color = i;
            flag[i] = color;
            dfs(i);
        }
    }
    int q;
    cin >> q;
    while (q--)
    {
        cin >> f >> g;
        if (flag[f] == flag[g])
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
}

C++: #include<iostream>
#include<list>
#define MAX 100000
using namespace std;

class person
{
public:
	bool Isdone = false;
	list <person*> lis;
	bool serch(person* Per)
	{
		Isdone = true;
		while (!lis.empty())
		{
			person* temp = lis.front();
			lis.pop_front();
			if (temp == Per)return true;
			else if (temp->Isdone == true)continue;
			else
			{
				if (temp->serch(Per))return true;
			}
		}
		return false;
	}
};

int main()
{
	person *P;
	P = new person[MAX + 1];
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int p1, p2;
		cin >> 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;
		cin >> 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;
	}
	delete[] P;
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
int n,m;
static const int MAX = 100000;
static const int NIL = -1; 
vector<int> graph[MAX];
int color[MAX];
void dfs(int s,int t)
{
	
	stack<int> ss;
	ss.push(s);
	color[s]=t;
	while(!ss.empty())
	{
		int u=ss.top();
		ss.pop();
		for(int i=0;i<graph[u].size();i++)
		{
			int v=graph[u][i];
			if(color[v]==NIL)
			{
				color[v]=t;
				ss.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()
{
	cin>>n>>m;
	
	for(int i=0;i<m;i++)
	{
		int s,t;
		cin>>s>>t;
		graph[s].push_back(t);
		graph[t].push_back(s);
	}
	assigncolor();
	int q;
	cin>>q;
	while(q--)
	{
		int s,t;
		cin>>s>>t;
		if(color[s]==color[t])
			cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
 }

C++: #include <stdio.h>
#include <stack>
#include <iostream>
using namespace std;
int G[100000][100000];
int vis[100000];
int n;

bool dfs(int s, int t)
{
    if (G[s][t] == 1 || G[t][s] == 1) return true;
    vis[s] = 1;
    for (int i = 0; i < n; i++)
    {
        if (G[s][i] == 1 && vis[i] == 0)
            {
                bool a = (dfs(i,t)); 
                if(a) 
                {
                    vis[s] = 0;
                    return a;
                }
            }

    } 
    vis[s] = 0;
    return false;
}

int main()
{
    //freopen("in3.txt","r",stdin);
    int m;
    scanf("%d %d",&n, &m);
    for (int i = 0; i < n; i++)
    {
        vis[i] = 0; 
    } 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            G[i][j] = 0; 
        }  
    } 
    for (int i = 0; i < m; i++)
    {
        int s, t;
        scanf("%d %d",&s,&t); 
        G[s][t] = 1;
        G[t][s] = 1;
    } 
    int q,s,t;
    scanf("%d",&q);
    for (int i = 0; i < q; i++)
    {
        scanf("%d %d",&s,&t);
        if(dfs(s,t)||dfs(t,s)) printf("yes\n");
        else printf("no\n");
    } 
    return 0;
}

C++: #include<stdio.h>
#include<vector>
#include<string>
#include<iostream>
using namespace std;

vector<vector<int> >g;
int n,m,q,s,t;

vector<bool>visited;
vector<int>color;
void dfs(int s){
    for(int i=0;i<g[s].size();i++){
        if(color[g[s][i]]==-1){
            color[g[s][i]]=color[s];
            dfs(g[s][i]);
        }
    }
}
bool dfs2(int s,int t){
    if(s==t)return true;
    visited[s]=true;
    for(int i=0;i<g[s].size();i++){
        if(!visited[g[s][i]]){
            if(dfs2(g[s][i],t)){
                return true;
            }
        }
    }
    return false;
}
int main() {
    scanf("%d %d",&n,&m);
    g.resize(n);
    color.resize(n,-1);
    for(int i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        g[s].push_back(t);
        g[t].push_back(s);
    }
    for(int i=0;i<n;i++){
        if(color[i]==-1){
            color[i]=i;
            dfs(i);
        }
    }
    scanf("%d",&q);
    visited.resize(n);
    for(int i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        for(int j=0;j<n;j++)visited[j]=false;
        string s1=(dfs2(s,t)?"yes":"no");
        string s2=(color[s]==color[t]?"yes":"no");
        //if(s1!=s2&&s1=="yes")return 1;
        cout<<s1<<endl;
    }
    return 0;
}