Go: package main

import (
	"fmt"
)

var n, m int
var g [][]int
var color []int

func main() {
	fmt.Scan(&n, &m)
	g = make([][]int, n) // SNSの関係を表すグラフ
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		g[a] = append(g[a], b) // 無向重みなしグラフ
		g[b] = append(g[b], a)
	}
	color = make([]int, n) // 各ノードにつける色
	for i := 0; i < len(color); i++ {
		color[i] = -1 // 初期化しておく
	}
	type query struct {
		s, t int
	}
	var Q int
	fmt.Scan(&Q)
	qs := make([]query, Q)
	for i := 0; i < Q; i++ {
		fmt.Scan(&qs[i].s, &qs[i].t)
	}
	// fmt.Println("n", n, "m", m, "g", g, "Q", Q, "qs", qs)
	// 全ての頂点からdfsする
	id := 1
	for u := 0; u < n; u++ {
		if color[u] == -1 {
			dfs(u, id)
			id++
		}
	}
	// fmt.Println("color", color)
	for i := 0; i < Q; i++ {
		if color[qs[i].s] == color[qs[i].t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func dfs(v, c int) {
	// fmt.Printf("dfs(v %d, c %d) en:\n", v, c)
	color[v] = c
	for _, vv := range g[v] {
		// fmt.Printf("  v %d, g[v] %v, vv %d\n", v, g[v], vv)
		if color[vv] == -1 {
			dfs(vv, c)
		}
	}
}

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

#define ll                  long long
#define scl(n)              scanf("%lld", &n)
#define fr(i,n)             for (ll i=0;i<n;i++)
#define fr1(i,n)            for(ll i=1;i<=n;i++)
#define pfl(x)              printf("%lld\n",x)
#define endl 	            "\n"
#define pb                  push_back
#define l(s)                s.size()
#define asort(a)            sort(a,a+n)
#define dsort(a)            sort(a,a+n,greater<int>())
#define vasort(v)           sort(v.begin(), v.end());
#define vdsort(v)           sort(v.begin(), v.end(),greater<int>());
#define pn                  printf("\n")
#define md                  10000007

ll n;
vector<ll>v[1000005];
ll cnt=1;
bool vis[1000005];
ll clr[1000005];
queue<ll>q;
void bfs(ll s, ll d)
{
    q.push(s);
    clr[s]=d;
    while(!q.empty())
    {
         ll p=q.front(); q.pop();
         for(ll i=0;i<v[p].size();i++)
         {
             ll u=v[p][i];
             if(clr[u]==-1)
             {
                 q.push(u);
                 clr[u]=d;
             }
         }
    }
}

void colour( )
{
    fr(i,1000000)clr[i]=-1;

    fr(i,n)if(clr[i]==-1)bfs(i, cnt++);
}
int main()
{
    ll m,t,b,c,d,i,j,k,x,y,z,l,q,r;

      ll cnt=0,ans=0;
      scl(n);scl(m);

      fr(i,m)
      {
          cin>>x>>y;
          v[x].pb(y);
          v[y].pb(x);
      }
      colour();

      cin>>q;
      while(q--)
      {
          cin>>x>>y;
          if(clr[x]==clr[y])cout<<"yes"<<endl;
          else cout<<"no"<<endl;
      }
return 0;
}

C++: #include <bits/stdc++.h>


class UNF {
    public:
        std::vector<int> p, rank;
        UNF(int s) {
            p.resize(s, 0);
            rank.resize(s, 0);
            for (int i = 0; i < s; i++) {
                makeset(i);
            }
        }
        void makeset(int x) {
            p[x] = x;
            rank[x] = 0;
        }
        int findset(int x) {
            if (x != p[x]) {
                p[x] = findset(p[x]);
            }
            return p[x];
        }
        
        void link(int x, int y) {
            if (rank[x] > rank[y]) {
                p[y] = x;
            }
            else {
                p[x] = y;
            }
            if (rank[y] == rank[x]) {
                rank[y]++;
            }
        }
        
        void unite(int x, int y) {
            link(findset(x), findset(y));
        }

};

int main() {
    int n, m, q;
    std::cin >> n >> m;
    UNF unf = UNF(n);
    for (int i = 0; i < n; i++) {
        unf.makeset(i);
    }
    int a, b;
    for (int j = 0; j < m; j++) {
        std::cin >> a >> b;
        unf.unite(a, b);
    }
    std::cin >> q;
    for (int p = 0; p < q; p++) {
        std::cin >> a >> b;
        if (unf.findset(a) == unf.findset(b)) {
            std::cout << "yes" << std::endl;
        }
        else {
            std::cout << "no" << std::endl;
        }
    }
}

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++: import sys
sys.setrecursionlimit(50000)
def dfs(s):
    global t
    if s == t:
        return True
    DP[s]=1
    for i in F[s]:
        if not DP[i]:
            if dfs(i):
                return True
    return False
    
n,m = [int(i) for i in input().split()]
F = [{} for i in range(n)]
for _ in range(m):
    i,j = [int(i) for i in input().split()]
    F[i][j] = 1
    F[j][i] = 1
q = int(input())
for i in range(q):
    DP = [0]*n
    s,t = [int(i) for i in input().split()]
    if dfs(s):
        print("yes")
    else:
        print("no")

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
const int num = 100001;
vector<int> v[num];
int n;
int co[num];
inline void dfs(int t, int c) {
	stack<int> st;
	st.push(t);
	co[t] = c;
	while (!st.empty()) {
		int u = st.top(); st.pop();
		for (int i = 0; i < v[u].size(); i++) {
			int g = v[u][i];
			if (co[g] == 0) {
				co[g] = c;
				st.push(g);
			}
		}
	}
}
int main() {
	int m, s, t, q, id = 0, a, b;
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		co[i] = 0;
	for (int i = 0; i < m; i++) {
		cin >> s >> t;
		v[s].push_back(t);
		v[t].push_back(s);
	}
	for (int i = 0; i < n; i++) {
		if (co[i] == 0) {
			id++;
			dfs(i, id);
		}
	}
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> a >> b;
		if (co[a] == co[b])
			cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}

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;
}