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 <bits/stdc++.h>
using namespace std;
const int N = 1000000;
const int CRL = -1;
int v[N]={0};
void ketugou(int k,int l,int n){
	int x,mn;
	x = max(k,l);
	mn = min(k,l);
	for(int i=0;i<n;i++){
		if(v[i]==x)v[i]=mn;
	}
}

int main(){
	int n,m,q;
	memset(v, CRL, sizeof(v));
	cin >> n >> m;
	for(int i=0;i<m;i++){
		int k,l;
		cin >> k >> l;
		if(v[k]==CRL && v[l]==CRL){
			v[k] = v[l] = min(k,l);
		}else if(v[k]!=CRL && v[l]!=CRL){
			ketugou(k,l,n);
		}else if(v[k]==CRL){
			v[k]=v[l];
		}else{
			v[l]=v[k];
		}
	}
	cin >> q;
	for(int i=0;i<q;i++){
		int k,l;
		cin >> k >> l;
		if(v[k]==CRL || v[l]==CRL) cout << "no" << endl;
		else if(v[k]==v[l])cout << "yes" << endl;
		else cout << "no" << endl;
	}
    return 0;
}

C++: /**
 */

/*
 * test case 1
 2 1 1 2 1 1 2  
 test case 2
 4 4 1 2 1 3 2 4 3 4 1 1 2   
 */


#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector< vector<int> > rel;
vector<bool> visited;
int n, m, q;
int dest = 0;


bool fre1(int j) 
{
	for (int i=0;i<rel[j].size();i++)
	{
		int kk = rel[j][i];
		if (kk==dest) return true;
		if (visited[kk])continue;
		visited[kk]=true;
		if(fre1(kk))return true;
	}
	return false;
}

/**
 * preserve a priority que of nodes with the most connections. 
 * if there is a node with only 2 connections go look for the other one.
 */

priority_queue< pair<int, int> > pq;


bool fre3(int start) 
{
	for (int i=0;i<rel[start].size();i++)
	{
		int cand = rel[start][i];
		if (cand==dest) return true;
		if (visited[cand])continue;
		visited[cand]=true;
		int point = rel[cand].size();
		if (point == 1) continue;
		if (point == 2) {
			if (fre3(cand)) return true;
		} else {
			pq.push(pair<int, int>(point, cand));
		}
	}
	return false;
}


bool fre2(int start) 
{
	pq.push(pair<int,int>(0, start));
	while (pq.size())
	{
		pair<int, int> p = pq.top();
		pq.pop();
		start = p.second;
		if (visited[start])continue;
		if (fre3(start)) return true;
	}
	return false;
}


int main()
{
	cin >> n;
	rel.resize(n);
	cin >> m;
	for (int i=1;i<=m;i++){
		int j, k;
		cin >> j >> k;
		rel[j].push_back(k);
		rel[k].push_back(j);
	}

	cin >> q;
	for (int i=1;i<=q;i++){
		int start;
		cin >> start >> dest;
		visited.clear();
		visited.resize(n, false);
		cout << (fre2(start) ? "yes" : "no") << endl;
		while (pq.size()) pq.pop(); // forgot this.
	}

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

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++: //ALDS1_11D
#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++: // ALDS1_11_D.cpp
// Graph I - Connected Components

#include <iostream>
#include <vector>
using namespace std;

void dfs(vector<vector<int>> &sns, vector<int> &arrived, int x, int y)
{
    if (arrived[y] != 0 && arrived[x] == 2) return;
    arrived[x]++;
    for (int i = 0; i < sns[x].size(); i++) {
        int next = sns[x][i];
        if (arrived[next] == 0) {
            dfs(sns, arrived, next, y);
        }
    }
    arrived[x]++;
}

int main()
{
//    freopen("ALDS1_11_D_in22.txt", "r", stdin);
//    freopen("out22.txt", "w", stdout);
    int m, n, q;
    cin >> n >> m;
    vector<vector<int> > sns(n, vector<int>());
    int x, y;
    for (int i = 0; i < m; i++) {
        cin >> x >> y;
        sns[x].push_back(y);
        sns[y].push_back(x);
    }

    cin >> q;
    while (q--) {
        vector<int> arrived(n);
        cin >> x >> y;
        dfs(sns, arrived, x, y);
        if (arrived[y]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;

void dfs(const vvi &to, vi &id, int u, int val) {
    id[u] = val;
    for (int v: to[u]) {
        if (id[v] < 0) dfs(to, id, v, val);
    }
};

int main() {
    int n, m, s, t, q, u, v;
    cin >> n >> m;
    vvi to(n);
    for (int i=0; i<m; ++i) {
        cin >> s >> t;
        to[s].push_back(t);
        to[t].push_back(s);
    }
    vi id(n, -1);
    for (int i=0; i<n; ++i) {
        if (id[i] < 0) dfs(to, id, i, i);
    }
    cin >> q;
    for (int i=0; i<q; ++i) {
        cin >> u >> v;
        cout << (id[u] == id[v] ? "yes" : "no") << endl;
    }
    return 0;
}