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<queue>
#include<vector>
using namespace std;
#define MAX_N 500000
vector<int>X[MAX_N]; queue<int>Q;
int COLOR[MAX_N], N, M, Q1, A, B;
int main() {
	cin >> N >> M;
	for (int i = 0; i < M; i++) { cin >> A >> B; X[A].push_back(B); X[B].push_back(A); }
	int cnt = 0;
	for (int i = 0; i < N; i++) {
		if (COLOR[i] == 0) {
			Q.push(i); cnt++; COLOR[i] = cnt;
			while (!Q.empty()) {
				int a = Q.front(); Q.pop();
				for (int j = 0; j < X[a].size(); j++) {
					int to = X[a][j];
					if (COLOR[to] == 0) {
						COLOR[to] = cnt;
						Q.push(to);
					}
				}
			}
		}
	}
	cin >> Q1;
	for (int i = 0; i < Q1; i++) {
		cin >> A >> B;
		if (COLOR[A] == COLOR[B]) {
			cout << "1" << endl;
		}
		else {
			cout << "0" << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>

using namespace std;

class Disjoint {
public:
    Disjoint(int n) {
        for (int i = 0; i < n; i++) {
            id.push_back(i);
            size.push_back(1);
        }
    }

    bool isConnected(int s, int t) {
        return find(s) == find(t);
    }

    int find(int vertex) {
        while (vertex != id[vertex]) {
            id[vertex] = id[id[vertex]];
            vertex = id[vertex];
        }
        return vertex;
    }

    void connect(int s, int t) {
        int sRoot = find(s);
        int tRoot = find(t);

        if (sRoot == tRoot)
            return;

        if (size[sRoot] < size[tRoot]) {
            size[tRoot] += size[sRoot];
            id[sRoot] = id[tRoot];
        }
        else {
            size[sRoot] += size[tRoot];
            id[tRoot] = id[sRoot];
        }
    }

private:
    vector<int> id;
    vector<int> size;
};

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

    Disjoint disjoint = Disjoint(n);
    int s, t;
    for (int i = 0; i < m; i++) {
        cin >> s >> t;
        disjoint.connect(s, t);
    }

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

    return 0;
}

C++: #include<iostream>
#include<stack>
#include<vector>
#include<string.h>
#define MAX 100005
#define FOR(i,a,b) for(int i=a;i<b;i++)
using namespace std;
int N,M;
int Time=0;
vector<int> V[MAX];
int sup[MAX];
void DFS(int i,int u)
{
	stack<int> Q;
	Q.push(i);
	sup[i]=u;
	while(!Q.empty())
	{
		int k=Q.top();
		Q.pop();
		FOR(j,0,V[k].size())
			if(!sup[V[k][j]])
			{
				sup[V[k][j]]=u;
				Q.push(V[k][j]);
			}
	}
}
void solve()
{
	FOR(i,0,N)
		if(!sup[i])
			DFS(i,++Time);
}
int main()
{
	memset(sup,0,sizeof(sup));
	int s,t,q;
	scanf("%d %d",&N,&M);
	FOR(i,0,M)
	{
		scanf("%d %d",&s,&t);
		V[s].push_back(t);
		V[t].push_back(s);
	}
	solve();
	
	scanf("%d",&q);
	while(q--)
	{
		scanf("%d %d",&s,&t);
		if(sup[s]==sup[t]) printf("yes\n");
		else printf("no");
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define pb push_back
using namespace std;
int nam[100001];
bool vis[100001];
vector<int> a[100001];
queue<int> q;
bool ok=false;
int n,u,k,x,m,qq,s,e,t=1;
void dfs(int k){
	 if (k==e)
	    ok=true;
	 if (ok) return; 
     vis[k]=true;
	 for (int i=0; i<a[k].size(); i++){
	     if (!vis[a[k][i]] && !ok){
	     	dfs(a[k][i]);
		 }
     }
}
int main(){
	memset(a,0,sizeof(a));
	memset(vis,false,sizeof(vis));
	memset(nam,0,sizeof(nam));
	cin>>m>>n;
	for (int i=0; i<n; i++){
		cin>>u>>k;
		a[u].pb(k);
		a[k].pb(u);
	}
	cin>>qq;
	while (qq--){
	       ok=false; 
	       while (!q.empty()) q.pop();
		   cin>>s>>e;
		   if (!vis[s]){
		      q.push(s); 
		      while (!q.empty()){
		   	        x=q.front(); vis[x]=true; q.pop(); 
		   	        nam[x]=t;
				    for (int i=0; i<a[x].size(); i++){
	                    if (!vis[a[x][i]])
	     	               q.push(a[x][i]);
                    }
		      }
		      t++;
		  }
		  if (nam[s]==nam[e])
		      cout<<"yes"<<endl;
		   else 
		      cout<<"no"<<endl;	
	} 
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
#include<set>
using namespace std;

bool DFS(const vector<set<int> >& G, int a, int b) {
    if (G[a].size() == 0) return false;

    stack<pair<int, set<int>::iterator> > S;
    vector<bool> is_visited(G.size(), false);

    set<int>::iterator idx = G[a].begin();
    do {
        if (idx == G[a].end()) {
            a   = S.top().first;
            idx = S.top().second;
            S.pop();
            idx++;
            continue;
        }
        if (*idx == b) return true;
        if (!is_visited[*idx]) {
            is_visited[*idx] = true;
            S.push(make_pair(a, idx));
            a   = *idx;
            idx = G[a].begin();
            continue;
        }
        idx++;
    } while (!S.empty());
    return false;
}


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

    vector<set<int> > G(n);
// bool D[100000][100000];

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

// for (int i = 0; i < n; i++) {
//     cout << i << " : ";
//     for (set<int>::iterator it = G[i].begin(); it != G[i].end(); it++){
//         cout << *it << " ";
//     }
//     cout << endl;
// }



    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        if (DFS(G, a, b)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}