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;

#define ll long long
typedef complex<double> P;

int main() {
  int n, m; cin >> n >> m;
  vector<int> adj[n];

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

  int sd[n] = {1};
  queue<int> que;
  que.push(0);
  int c = 1;

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

    for (int i = 0; i < adj[v].size(); i++) {
      if(!sd[adj[v][i]]) {
        que.push(adj[v][i]);
        sd[adj[v][i]] = c;
      }
    }

    if(que.empty()) {
      for (int i = 0; i < n; i++) {
        if(!sd[i]) {
          sd[i] = ++c;
          que.push(i);
          break;
        }
      }
    }

  }

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

C++: #include <iostream>
#include <vector>
using namespace std;
vector<int> G[100];
vector<int> A;
int isVisited[100];

void setupVisited(int n)
{
    for (int i = 0; i < n; i++) {
	isVisited[i] = 0;
    }
}
bool enableConnection(int l, int o)
{
    for (int i = 0; i < G[l].size(); i++) {
	if (G[l][i] == o) {
	    return true;
	}
	if (isVisited[l] == 1) {
	    return false;
	}
	isVisited[l] = 1;
	if (enableConnection(G[l][i], o) || enableConnection(o, G[l][i])) {
	    return true;
	}
    }
    return false;
}

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

    //then
    int b = 0;
    cin >> b;
    for (int i = 0; i < b; i++) {
	int l;
	int o;
	cin >> l;
	cin >> o;
	setupVisited(c);
	if (enableConnection(l, o) || enableConnection(o, l)) {
	    cout << "yes" << endl;
	} else {
	    cout << "no" << endl;
	}
    }
    return 0;
}

C++: #include <stdio.h>
#define rep(i, n) for(int i = 0; i < n; ++i)
 
int main(void) {
  int n, m;
  scanf("%d%d", &n, &m);
  int uf[n];
  rep(i, n) uf[i] = i;
  rep(i, m) {
    int s, t, nows, nowt;
    scanf("%d%d", &s, &t);
    nows = s, nowt = t;
    while(uf[nows] != nows) {
      nows = uf[nows];
    }
    while(uf[nowt] != nowt) {
      nowt = uf[nowt];
    }
    uf[nowt] = nows;
  }
  rep(i, n) {
    int nows = i;
    while(uf[nows] != nows) {
      nows = uf[nows];
    }
    uf[i] = nows;
  }
  /*rep(i, n) printf("%d ", uf[i]);
  printf("\n");*/
  int q;
  scanf("%d", &q);
  rep(i, q) {
    int s, t;
    scanf("%d%d", &s, &t);
    if(uf[s] != uf[t]) printf("no\n");
    else printf("yes\n");
  }
  return 0;
}

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

void dfs(int root, int node, vector<vector<int> >& adjlist, vector<int>& used){

    for(int i = 0; i < adjlist[node].size(); i++){
        if(used[adjlist[node][i]] == -1){
            used[adjlist[node][i]] = root;
            dfs(root, adjlist[node][i], adjlist, used);
        }
    }
}

int main(){

    int n, m; cin >> n >> m;

    vector<vector<int> > adjlist(n);               

    //入力
    for(int i = 0; i < m; i++){
        int a, b; cin >> a >> b;                   // a ←→ b に辺がある
        adjlist[a].push_back(b);
        adjlist[b].push_back(a);
        
    }

    vector<int> used(n, -1);

    for(int i = 0; i < n; i++){
        if(used[i] == -1){
            used[i] = i;
            dfs(i, i, adjlist, used);
        }
    }

    /*for(int i = 0; i < n; i++){
        cout << i << " " << used[i] << endl;
    }*/

    //質問
    int q; cin >> q;
    for(int i = 0; i < q; i++){
        int a, b; cin >> a >> b;
        
        
        if(used[a] == used[b]) cout << "yes" << endl;
        else cout << "no" << endl;
    }

    return 0;
}