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 <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

struct node {
	int id;
	vector<int> rel;
};

int n, m, q;
node* nodes;

/* DFS * TLE! *
bool canReach(int s, int d, bool* h){
	h[s] = true;
	for(int i = 0; i < nodes[s].rel.size(); i++){
		if(nodes[s].rel[i] == d) return true;
		else if(!h[nodes[s].rel[i]]){
			if(canReach(nodes[s].rel[i], d, h)) return true;
		}
	}
	return false;
}
*/

int main() {
	scanf("%d %d", &n, &m);
	
	nodes = new node[n];
	for(int i = 0; i < n; i++){
		nodes[i].id = i;
	}
	while(m--){
		int l, r;
		scanf("%d %d", &l, &r);
		nodes[l].rel.push_back(r);
		nodes[r].rel.push_back(l);
	}
	
	scanf("%d", &q);
	while(q--){
		int l, r;
		scanf("%d %d", &l, &r);
		
		bool *h = new bool[n];
		for(int i = 0; i < n; i++){
			h[i] = false;
		}
		
		queue<int> qu;
		qu.push(l);
		while(qu.size()){
			h[qu.front()] = true;
			for(int i = 0; i < nodes[qu.front()].rel.size(); i++){
				if(nodes[qu.front()].rel[i] == r){
					puts("yes");
					goto BREAK;
				}else if(!h[nodes[qu.front()].rel[i]]){
					qu.push(nodes[qu.front()].rel[i]);
				}
			}
			qu.pop();
		}
		puts("no");
		BREAK:;
		delete[] h;
	}
	return 0;
}

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

const int INF=99999999;

vector<int> A[100000];

void get_network(int N,int M){
    int s,t;
    for(int i=0;i<M;i++){
        cin>>s>>t;
        A[s].push_back(t);
        A[t].push_back(s);
    }
}
void bfs(int n,int v,vector<int>& flag,int mark){
        queue<int> Q;
        flag[v]=mark;
        for(int i=0;i<A[v].size();i++){
                Q.push(A[v][i]);
        }
        while(!Q.empty()){
                int u=Q.front();
                flag[u]=mark;
                for(int i=0;i<A[u].size();i++){
                        if(flag[A[u][i]]==-1){
                                Q.push(A[u][i]);
                        }
                }
                Q.pop();
        }
}

int main(){
        int n,m;
        scanf("%d %d\n",&n,&m);
        get_network(n,m);

        vector<int> flag(n,-1);
        int colored=-1;
        int v=0;
        int mark=0;
        while(colored!=0){
                mark++;
                bfs(n,v,flag,mark);
                colored=0;
                for(int i=0;i<flag.size();i++){
                    if(flag[i]==-1){
                        v=i;
                        colored=1;
                        break;
                    }
                }
        }

        int q,s,t;
        scanf("%d\n",&q);

        for(int i=0;i<q;i++){
                scanf("%d %d\n",&s,&t);
                if(flag[s]==flag[t]){
                        printf("yes\n");
                }else{
                        printf("no\n");
                }
        }
        return 0;
}

C++: #include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#define N 100000
using namespace std;


int C[N];
queue<int> k;
vector<int> v[N];
void color(int);
bool search(int,int);

int main(){
	int n,m,a,b,q;
	cin >> n >>m;
	for(int i=0;i<m;i++){
		cin >> a >> b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	cin >> q;
	for(int i=0;i<n;i++){
		C[i]=-1; //?????????
	}
	for(int i=0;i<n;i++){
		if(C[i]==-1)color(i); //i????§??????¨????????±???
	}
	for(int i=0;i<q;i++){
		cin>>a>>b;
		if(search(a,b)) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}

bool search(int a,int b){
	if(C[a]==C[b]) return true;
	else return false;
}

void color(int a){
	static int c=0;
	c++;
	C[a]=c;
	k.push(a);
	while(!k.empty()){
		int m = k.front();
		k.pop();
		for(int i=0;i<v[m].size();i++){
			int Z= v[m].at(i);
			if(C[Z]==-1){
				C[Z]=c;
				k.push(Z);
			}
		}
	}
}

C++: #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
using namespace std;

int n, m;
int d[100000];
int t = 0;
int c[100000];
queue<int> q;
vector<int> v[100000];
int ans = 2;

void bfs(int u){
  q.push(u);
  c[u] = 1;

  while(q.empty() == 0){
    u = q.front();
    q.pop();
    for(int i = 0; i < v[u].size(); i++){
      if(c[v[u][i]] == 0){
        q.push(v[u][i]);
        c[v[u][i]] = 1;
      }

    }
    c[u] = ans;
  }

}


int main(){

  cin >> n;

  for(int i = 0; i < n; i++){
    c[i] = 0;
  }

  cin >> m;

  for(int i = 0; i < m; i++){
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }

  for(int i = 0; i < n; i++){
    if(c[i] == 0){
      bfs(i);
      ans++;
    }
  }

  int q;
  cin >> q;

  for(int i = 0; i < q; i++){
    int x, y;
    cin >> x >> y;
    if(c[x] == c[y]) cout << "yes" << endl;
    else cout << "no" << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <vector>
#define N 100000
#define white 0
using namespace std;

int color[N], n, S[N];
vector<int> c[N];



void tansaku_2(int a, int b) {
    int i, t, co, u;
    S[0] = a;
    t = 0;
    co = 1;
    while (1) {
        u = S[t];
        for (i = 0; i < c[u].size(); ++i) {
            if (color[c[u][i]] == white){
                color[c[u][i]] = b;
                S[co++] = c[u][i];
            }
        }
        S[t++] = 0;
        if (t == co) break;
    }
}

void tansaku() {
    int i, d;
    d = 1;
    
    for (i = 0; i < n; ++i) {
        if (color[i] == white) {
            color[i] = d;
            tansaku_2(i , d++);
        }
    }
}

int main() {
    int m, q, i, a, b;
    
    cin >> n >> m;
    for (i = 0; i < n; ++i) {
        color[i] = white;
    }
    for (i = 0; i < m; ++i) {
        scanf("%d %d", &a, &b);
        c[a].push_back(b);
        c[b].push_back(a);
    }
    tansaku();
    
    cin >> q;
    
    for (i = 0; i < q; ++i) {
        scanf("%d %d", &a, &b);
        if (color[a] == color[b]) {
            cout << "yes" << endl;
        }
        else{
            cout << "no" << endl;
        }
    }
    return 0;
}