Go: // AOJ ALDS1_11_D: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_D

package main

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

var nocolor = -1

type graph struct {
	graph  [][]int
	colors []int
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func newGraph(nodeNum int) *graph {
	colors := make([]int, nodeNum)
	g := make([][]int, nodeNum)
	for i := 0; i < nodeNum; i++ {
		g[i] = []int{}
		colors[i] = nocolor
	}

	return &graph{g, colors}
}

func constructGraph(sc *bufio.Scanner) *graph {
	usersNum := nextInt(sc)
	linkNum := nextInt(sc)

	g := newGraph(usersNum)
	for i := 0; i < linkNum; i++ {
		u := nextInt(sc) // node
		l := nextInt(sc) // link node
		// edge each other
		g.graph[u] = append(g.graph[u], l)
		g.graph[l] = append(g.graph[l], u)
	}
	return g
}

func (g *graph) dfs(r, color int) {
	g.colors[r] = color

	stack := list.New()
	stack.PushFront(r)

	for stack.Len() > 0 {
		v := stack.Remove(stack.Front()).(int)
		for _, u := range g.graph[v] {
			if g.colors[u] == nocolor {
				g.colors[u] = color
				stack.PushFront(u)
			}
		}
	}
}

func (g *graph) assignColor() {
	color := 1
	for u := 0; u < len(g.graph); u++ {
		if g.colors[u] == nocolor {
			g.dfs(u, color)
			color++
		}
	}
}

func (g *graph) isLink(s, t int) bool {
	return g.colors[s] == g.colors[t]
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	// construct graph and friendly colors
	g := constructGraph(sc)
	g.assignColor()

	// judge link
	q := nextInt(sc)
	for i := 0; i < q; i++ {
		s := nextInt(sc)
		t := nextInt(sc)

		if g.isLink(s, t) {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

C++: #include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int MAXN = (int)1e5 + 10;

int m,n;
int p[MAXN];

int Find(int x){
    return x==p[x] ? x : p[x]=Find(p[x]);
}

void Merge(int u,int v){
    int x=Find(u);
    int y=Find(v);
    if(x!=y){
        p[x]=y;
    }
}

void work(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) p[i]=i;
    for(int i=1;i<=m;i++){
        int u,v; scanf("%d%d",&u,&v);
        Merge(u,v);
    }
    int q; scanf("%d",&q);
    for(int i=1;i<=q;i++){
        int s,t; scanf("%d%d",&s,&t);
        if(Find(s)==Find(t)) printf("yes\n");
        else printf("no\n");
    }
}

int main(){
    work();
    return 0;
}

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

using namespace std;
#define MAX_N (100010)
int n;
int m;
int q;
vector<int> G[MAX_N];
int id[MAX_N];
int cur_id;

void bfs( int cur ) {
    queue< int > Q;
    Q.push( cur );
    id[cur] = cur_id;
    while ( !Q.empty() ) {
	int v = Q.front(); Q.pop();
	for ( int i = 0; i < G[v].size(); i++ ) {
	    int next = G[v][i];
	    if ( id[next] == -1 ) {
		id[next] = cur_id;
		Q.push( next );
	    }
	}
    }

    return;
}

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

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

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

    for ( int i = 0; i < n; i++ ) {
	if ( id[i] == -1 ) { 
	    bfs( i );
	    cur_id++;
	}
    }

    scanf("%d", &q);


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

    return 0;
}

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

int n,m;
vector<vector<int>> G(100005,vector<int>(0));
vector<int> color(100005,0);

void dfs(int u,int c){
    if(color[u]>0) return;
    color[u]=-1;
    for(int i=0;i<G[u].size();i++){
        if(color[G[u][i]]==0){
            dfs(G[u][i],c);
        }
    }
    color[u]=c;
}

int main(){
    cin>>n>>m;
    for(int i=0;i<m;i++){
        int a,b;
        cin>>a>>b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    for(int i=0;i<n;i++){
        dfs(i,i+1);
    }

    int q;
    cin>>q;
    for(int i=0;i<q;i++){
        int a,b;
        cin>>a>>b;
        if(color[a]==color[b]) cout<<"yes";
        else cout<<"no";
        cout<<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(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);
  }

  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<bits/stdc++.h>
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);
      }
    }
  }
}
int main(){
  int s,t,m,p;
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>s>>t;
    G[s].push_back(t);
    G[t].push_back(s);
  }
  int id=1;
  for(int i=0;i<n;i++){
    color[i]=NIL;
  }
  for(int i=0;i<n;i++){
    if(color[i]==NIL){
      dfs(i,id++);
    }
  }
  cin>>p;
  for(int i=0;i<p;i++){
    cin>>s>>t;
    if(color[s]==color[t]){
      cout<<"yes"<<endl;
    }else{
      cout<<"no"<<endl;
    }
  }
  return(0);
}

C++: #include <stdio.h>
#include <stdint.h>
#include <iostream>
#include <fstream>
#include <cassert>
#include <climits>

#include <cstring>
#include <queue>
#include <map>
#include <utility>
using namespace std;

#define DEBUG (0)

#if DEBUG
#define DPRINTF(...) do { \
    printf("D(L%d) %s: ", __LINE__, __func__); \
    printf(__VA_ARGS__); \
  } while (false)
#else
#define DPRINTF(...)
#endif // DEBUG

/*
   Memory limitaion; 64 MiB
   # of users: 2 <= n <= 100 K
   # of relations: 0 <= m <= 100 K

   Data size:
    1) for relations (maybe hash table):
      100K * 4Byte * 2 (bi-direction) = 800K

    2) for visited flag:
      100K * 1Byte = 100K
*/

const int kMaxUsers = 100 * 1000;

bool g_isVisited[kMaxUsers]; // 100 KiB
vector<int> g_list[kMaxUsers];


void setRelations(int start, int end)
{
  g_list[start].push_back(end);
}


string isReachable(int num, int start, int end)
{

  // init
  memset(g_isVisited, 0, num);

  queue<int> que;
  que.push(start);

  while (!que.empty())
  {
    int id = que.front();

    que.pop();
    g_isVisited[id] = true;

    for (int j = 0; j < g_list[id].size(); j++)
    {
      const int &next = g_list[id][j];

      if (next == end)
        return string("yes");

      if (!g_isVisited[next])
        que.push(next);
    }
  }

  return string("no");
}


void printAllRelations()
{
#if DEBUG
  for (auto& m: g_relations)
    DPRINTF("%d %d\n", m.first, m.second);
#endif // DEBUG
}


int main()
{
  int n, m;
  cin >> n >> m; cin.ignore();

  DPRINTF("number of users %d\n", n);
  DPRINTF("number of relations %d\n", m);

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

    setRelations(s, t);
  }

  printAllRelations();

  int q;
  cin >> q; cin.ignore();

  for (int i = 0; i < q; i++)
  {
    int s, t;
    cin >> s >> t; cin.ignore();

    DPRINTF("src %d  dst %d\n", s, t);

    cout << isReachable(n, s, t) << endl;
  }

  return 0;

}