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>
#include<limits.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)

#define MAX 100000

vector<vector<int>> adj_list(MAX);
vector<int> visited(MAX, -1);
vector<int> init_visited(MAX, -1);

int group = 0;

void dfs(int node){
    // cout << "init node " << node << endl;
    visited.at(node) = group;
    
    for(int i = 0; ; i++){
        int next_node = adj_list.at(node).at(i);
        // cout << next_node << endl;

        if(next_node != -1 && visited.at(next_node) == -1){
            // cout << "called dfs with " << next_node << endl;
            dfs(next_node);
        }
        if(next_node == -1){
            break;
        }
    }
    // cout << "returned from " << node << endl;
}

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


    //　隣接リストの作成
    rep(i, m){
        int from, to;
        cin >> from >> to;
        adj_list.at(from).push_back(to);
        adj_list.at(to).push_back(from);
    }
    // 番兵の挿入
    rep(i, n){
        adj_list.at(i).push_back(-1);
    }

    for(int i = 0; i < n; i++){
        if(visited.at(i) == -1){
            dfs(i);
            group++;
        }
    }

    int q; cin >> q;
    rep(i, q){
        int from, to;
        cin >> from >> to;
        if(visited.at(from) == visited.at(to)){
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }

    // cout << "=================" << endl;

    // for(int i = 0; i < m; i++){
    //     cout << "node " << i << " ";
    //     for(int j = 0; j < adj_list.at(i).size(); j++){
    //         cout << adj_list.at(i).at(j) << " ";
    //     }
    //     cout << endl;
    // }

    // rep(i, n){
    //     cout << "node " << i << " " << visited.at(i) << endl;
    // }
}

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

const long MAX = 100000;
const long NIL = -1;

long n;
vector<long> G[MAX];
long color[MAX];

void dfs(long r, long c){
	stack<long> S;
	S.push(r);
	color[r] = c;
	while (!S.empty()){
		long u = S.top();
		S.pop();
		for (long i = 0; i < G[u].size(); i++){
			long v = G[u][i];
			if (color[v] == NIL){
				color[v] = c;
				S.push(v);
			}
		}
	}
}

void assignColor(){
	long id = 1;
	for (long i = 0; i < n; i++) color[i] = NIL;
	for (long u = 0; u < n; u++){
		if (color[u] == NIL) dfs(u, id++);
	}
}

int main(){
	long s, t, m, q;
	cin >> n >> m;
	for (long i = 0; i < m; i++){
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}

	assignColor();

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

	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int maxx=100100;
vector<int> A[maxx];
int color[maxx];
int w,c=1;
stack<int> S;

void dfs(int u){
	color[u]=c;
	for(int i=0;i<A[u].size();i++){
		if(color[i]==-1){
			S.push(i);
			dfs(i);
		}
	}
	int x=S.top();
	S.pop();
	return ;
}

void dye(){
	S.push(0);
	color[0]=c;
L1:	while(S.empty()==0){
		int x=S.top();
		dfs(x);
	}
	for(int i=0;i<w;i++){
		if(color[i]==-1){
			S.push(i);color[i]=c++;goto L1;
		}
	}
	return ;
}

int main(){
	int q,a,b;
	cin>>q>>w;
	for(int i=0;i<=maxx;i++) color[i]=-1;
	while(--q){
		cin>>a>>b;
		A[a].push_back(b);
		A[b].push_back(a);
	}
	dye();
	cin>>q;
	while(q--){
		cin>>a>>b;
		if(color[a]==color[b]) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}

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

struct Edge
{
  int from;
  int to;

  friend bool operator==(const Edge &l, const Edge &r)
  {
    return l.from == r.from && l.to == r.to;
  }
};

int main()
{
  int n, m, v1, v2;
  scanf("%d%d", &n, &m);
  vector<Edge> v;

  for (int i = 0; i < m; i++)
  {
    scanf("%d%d", &v1, &v2);
    v.push_back({v1, v2});
  }

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

  int id = 0;
  while (!v.empty())
  {
    queue<Edge> q;
    q.push(v.front());
    while (!q.empty())
    {
      Edge edge = q.front();
      q.pop();
      group[edge.from] = id;
      group[edge.to] = id;
      v.erase(remove(v.begin(), v.end(), edge), v.end());
      // auto itr = find(v.begin(), v.end(), edge);
      // if (itr != v.end())
      //   v.erase(itr);
      for (auto const &e : v)
        if (e.from == edge.from || e.to == edge.from || e.from == edge.to || e.to == edge.to)
          q.push(e);
    }
    id++;
  }

  int q;
  scanf("%d", &q);

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

  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <stack>
using namespace std;
#define maxn 10005
#define NTL -1
int color[maxn];
vector<int> g[maxn];
stack<int> s;
int n,m,q;
void tarjan(int r,int c)
{
    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]==NTL)
            {
                color[v]=c;
                s.push(v);
            }
        }
    }
}
void assigncolor()
{
    int id=0;
    for(int i=0;i<n;i++)
        color[i]=NTL;
    for(int u=0;u<n;u++)
    {
        if(color[u]==NTL)
            tarjan(u,id++);
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    assigncolor();
    scanf("%d",&q);
    for(int i=0;i<q;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        if(color[u]==color[v])
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}