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<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#define Lens 100005

using namespace std;
int n, m, q;
vector<int> A[Lens];
queue<int>	Q;
char c[Lens],d[Lens];

int Find(int s, int t)
{
	Q.push(s);
	while (!Q.empty())
	{
		s = Q.front();
		Q.pop();
		if (c[s] == 'w')
		{
			c[s] = 'g';
			for (int j = 0; j < A[s].size(); j++)
			{
				Q.push(A[s][j]);
				if (A[s][j] == t)
				{
					return 1;
				}
			}
		}
	}
	return 0;
}

int main()
{
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int s, t;
		cin >> s >> t;
		d[s] = d[t] = 'w';
		A[s].push_back(t);
		A[t].push_back(s);
	}
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		for (int j = 0; j < m; j++)
			c[j] = d[j];
		int s, t;
		cin >> s >> t;
		if (Find(s, t))
			cout << "yes" << endl;

		else
			cout << "no" << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <cstdio>
#include <vector>
using namespace std;

int n;
void make_group(vector<int> &group, vector< vector<int> > &g) {
  bool visited[n];
  for(int i=0;i<n;i++) visited[i] = false;
  queue<int> que;
  int count = 1;
  for(int s=0;s<n;s++) {
    if(!visited[s]) {
      visited[s] = true;
      que.push(s);
      while(!que.empty()) {
        int v = que.front(); que.pop();
        group[v] = count;
        for(int i=0;i<g[v].size();i++) {
          if(!visited[g[v][i]]) {
            visited[g[v][i]] = true;
            que.push(g[v][i]);
          }
        }
      }
    }
    count++;
  }
}

int main() {
  int m,s,t,q;
  scanf("%d %d\n",&n,&m);
  vector< vector<int> > g(n);
  vector<int> group(n,0);
  for(int i=0;i<m;i++) {
    scanf("%d %d",&s,&t);
    g[s].push_back(t);
    g[t].push_back(s);
  }

  make_group(group,g);
  // cout << group << endl;
  /*
  for(int i=0;i<n;i++) {
    printf("%d:%d\n",i,group[i]);
  }
  */

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

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