Problem:
Connected Components
Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network.
Input
In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$.
In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other).
In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character.
Output
For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise.
Constraints
$2 \leq n \leq 100,000$
$0 \leq m \leq 100,000$
$1 \leq q \leq 10,000$
Sample Input
10 9
0 1
0 2
3 4
5 7
5 6
6 7
6 8
7 8
8 9
3
0 1
5 9
1 3
Sample Output
yes
yes
no

Submitted solutions:

code_snippet: #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" << "\n";
        } else {
            cout << "no" << "\n";
        }
    }

    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 9124
final_rank: 8

code_snippet: #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 assingColor(){
    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);
    }

    assingColor();

    cin >> q;

    for(int i = 0; i < q; i++){
        cin >> s >> t;
        if(color[s] == color[t]){
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }
    
}
accuracy: 1
time_ms: 40
memory_kb: 9224
final_rank: 11

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

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

    vector<int> group(n, -1);
    int group_count = 0;
    bool finished = false;
    stack<int> a;
    while(!finished)
    {
        finished = true;
        for(int i = 0; i < n; i++)
        {
            if (group[i] == -1)
            {
                a.push(i);
                finished = false;
                break;
            }
        }

        while(a.size() != 0)
        {
            int v = a.top();
            a.pop();
            group[v] = group_count;
            for(int u : adj[v])
                if(group[u] == -1)
                    a.push(u);
        }

        group_count++;
    }
    
    int q;
    cin >> q;
    deque<bool> result(q, false);
    for(int query = 0; query < q; query++)
    {
        int s, t;
        cin >> s >> t;
        if (group[s] == group[t])
            result[query] = true;
    }

    for(bool r : result)
    {
        if(r)
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
}
accuracy: 1
time_ms: 530
memory_kb: 8724
final_rank: 20

code_snippet: #include <cstdio>
#include <vector>
#include <iterator>
#define NIL -1
#define max 100000
using namespace std;
int n;
vector<int> g[max];
int color[max];
int c;
void search(int a){
    color[a]=c;
    for(int i=0;i<(int)g[a].size();i++){
        if(color[g[a][i]]==NIL){
            search(g[a][i]);
        }
    }
    return;
}
int main(void){
    scanf("%d",&n);
    int m;
    for(int i=0;i<n;i++){
        color[i]=NIL;
    }
    c=0;
    scanf("%d",&m);
    for(int i=0;i<m;i++){
        int s,t;
        scanf("%d %d",&s,&t);
        g[s].push_back(t);
        g[t].push_back(s);
    }
    for(int i=0;i<n;i++){
        if(color[i]==NIL){
            search(i);
            c++;
        }
    }
    int q;
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        int d,f;
        scanf("%d %d",&d,&f);
        if(color[d]==color[f]){
            printf("yes\n");
        }else printf("no\n");
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 11868
final_rank: 15

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

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> g(n, vector<int>());
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<int> colors(n, 0);
    queue<int> que;
    int cnt = 0;
    for (int i = 0; i < n; i++) {
        if (colors[i] != 0) continue;
        cnt++;
        que.push(i);
        while (!que.empty()) {
            int cur = que.front();
            que.pop();
            colors[cur] = cnt;
            for (auto nex : g[cur]) {
                if (colors[nex] == 0) {
                    colors[nex] = cnt;
                    que.push(nex);
                }
            }
        }
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int s, t;
        cin >> s >> t;
        cout << (colors[s] == colors[t] ? "yes" : "no") << '\n';
    }

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 9204
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <stack>

int n;
const int MAXN = 100000;
std::vector<int> G[MAXN];
int color[MAXN];
const int NIL = -1;

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

void assign_color() {
    for (int i = 0; i < n; ++i) color[i] = NIL;

    int c = 1;
    for (int i = 0; i < n; ++i) {
        if (color[i] == NIL) {
            dfs(i, c++);
        }
    }
}

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

    assign_color();

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

    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 9172
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
struct unionfind{
    vector<int> par,size;
    unionfind(int n):par(n),size(n){
        for(int i=0;i<n;i++)par[i]=i;
    }
    int find(int x){
        if(par[x]==x)return x;
        return par[x]=find(par[x]);
    }
    bool unite(int x,int y){
        x=find(x);
        y=find(y);
        if(x==y)return false;
        if(size[x]<size[y])swap(x,y);
        par[y]=x;
        size[x]+=size[y];
        return true;
    }
    bool same(int a,int b){
        return find(a)==find(b);
    }
};
int main(){
    int n,m;
    cin >> n >> m;
    unionfind uf(n+1);
    for(int i=0;i<m;i++){
        int a,b;
        cin >> a >> b;
        uf.unite(a,b);
    }
    int q;
    cin >> q;
    for(int i=0;i<q;i++){
        int a,b;
        cin >> a >> b;
        if(uf.same(a,b))cout << "yes" << endl;
        else cout << "no" << endl;
    }
}
accuracy: 1
time_ms: 40
memory_kb: 3860
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (n); i++)


// vectorを出力する
void vecprint(vector<ll> vec) {
    rep(i, vec.size()) {
        cout << vec[i] << endl;
    }
}
void newvecprint(vector<ll> vec) {
    rep(i, vec.size()) {
        cout << vec[i] << " ";
    }
    cout << endl;
}

// 最大か最小を求める
int minval(vector<ll> vec) {
    return *min_element(begin(vec), end(vec));
}
int maxval(vector<ll> vec) {
    return *max_element(begin(vec), end(vec));
}

ll n;
vector<ll> G[100000];
ll color[1000000];

void dfs(ll r, ll c) {
    stack<ll> s;
    s.push(r);
    color[r] = c;
    while (!s.empty()) {
        ll u = s.top();
        s.pop();
        rep(i,G[u].size()) {
            ll v = G[u][i];
            if (color[v] == -1) {
                color[v] = c;
                s.push(v);
            }
        }
    }
}

void solve() {
    ll m, s, t, q;
    cin >> n >> m;
    rep(i,m) {
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }
    ll id = 1;
    rep(i,n) {
        color[i] = -1;
    }
    rep(u, n) {
        if (color[u] == -1) {
            dfs(u, id++);
        }
    }
    cin >> q;
    rep(i,q) {
        cin >> s >> t;
        if (color[s] == color[t]) {
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }

    return;
}

int main() {
    solve();
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 11848
final_rank: 16

code_snippet: #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;

const int MAX = 100010;
int v, e;
vector<int> G[MAX];
int q;
int color[MAX];
int c;

void dfs(int p, int co) {
    if (color[p] != -1) return ;
    color[p] = co;
    for (int i = 0; i < G[p].size(); i++) {
        int node = G[p][i];
        dfs(node, co);
    }
}

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    scanf("%d %d", &v, &e);
    for (int i = 0; i < e; i++) {
        int a, b;
        scanf("%d %d", &a, &b);
        G[a].push_back(b);
        G[b].push_back(a);
    }
    memset(color, -1, sizeof(color));
    c = 0;
    for (int i = 0; i < v; i++) {
        if (color[i] == -1) {
            dfs(i, c++);
        }
    }
    scanf("%d", &q);
    while (q--) {
        int a, b;
        scanf("%d %d", &a, &b);
        if (color[a] == color[b]) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 12468
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<n; i++)

int main()
{
    int n,m;
    cin >> n >> m;
    vector<vector<int>> to(n);
    rep(i,m) {
        int s, t;
        cin >> s >> t;
        to[s].push_back(t);
        to[t].push_back(s);
    }
    vector<int> color(n, -1);
    vector<bool> visited(n);
    auto dfs = [&] (auto f, int v, int c) -> void {
        if (visited[v]) return;
        visited[v] = true;
        color[v] = c;
        for (int x : to[v]) {
            f(f, x, c);
        }
    };

    int g = 0;
    rep(i,n) {
        if (!visited[i]) {
            dfs(dfs,i,g);
            g++;
        }
    }

    int q;
    cin >> q;
    rep(qi,q) {
        int s,t;
        cin >> s >> t;
        if (color[s] == -1 || color[t] == -1 || color[s] != color[t]) cout << "no\n";
        else cout << "yes\n";
    }

    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 15256
final_rank: 18

code_snippet: #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 i=0; i < n; i++){
        if(color[i] == NIL){
            dfs(i, 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;
}
accuracy: 1
time_ms: 40
memory_kb: 9232
final_rank: 12

code_snippet: #include<iostream>
#include<vector>
#include<stack>
#include<set>

using namespace std;
static const int NIL = -1;
static const int N_MAX = 100000;

vector<int> friends[N_MAX];
int colors[N_MAX];

void dfs(int a, int color) {
    // aから友達関係を辿りbに辿り着けるかどうか
    stack<int> visit;
    set<int> visited;

    visit.push(a);
    // cout << visit.empty() << endl;
    while(!visit.empty()) {
        int c = visit.top();
        visit.pop();
        // cにまず色をつける
        colors[c] = color;
        // cと直接繋がっているユーザをpushしていく
        for(vector<int>::iterator it=friends[c].begin(); it != friends[c].end(); it++) {
            
            if(colors[(*it)] == NIL)
                visit.push((*it));            
        }
    }
}


void assignColor(int n) {
    // すべてのユーザーを、繋がってる順に色分け
    for(int i=0; i<n; i++ ) {
        colors[i] = NIL;
    }
    int color = 0;
    for(int i=0; i<n; i++){
        // すでにいずれかのグループに属している場合、スキップ
        if(colors[i] != NIL) continue;
        dfs(i, color++);
    }
}


int main() {
    int n, m;

    cin >> n >> m;

    int from, to;
    for(int i=0; i<m; i++) {
        scanf("%d %d", &from, &to);
        friends[from].push_back(to);
        friends[to].push_back(from);
    }

    assignColor(n);
    // for(int i=0; i<n; i++){
    //     cout << i << ": " << colors[i] << endl;
    // }
    int q;
    cin >> q;
    for(int i=0; i<q; i++){
        scanf("%d %d", &from, &to);
        if(colors[from] == colors[to]) {
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }



    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 9220
final_rank: 6

code_snippet: #include <stdio.h>

#define MAX_N 100000

int parent[MAX_N];

// Union-Find木を初期化
void init(int n) {
    for (int i = 0; i < n; i++) {
        parent[i] = i;
    }
}

// 木の根を求める (経路圧縮あり)
int find(int x) {
    if (parent[x] == x) {
        return x;
    } else {
        // 親を根に直接つなぎ替える(経路圧縮)
        return parent[x] = find(parent[x]);
    }
}

// xとyの属する集合を併合
void unite(int x, int y) {
    int rootX = find(x);
    int rootY = find(y);
    if (rootX != rootY) {
        parent[rootX] = rootY;
    }
}

// xとyが同じ集合に属するか判定
int same(int x, int y) {
    return find(x) == find(y);
}

int main() {
    int n, m, s, t, q;

    scanf("%d %d", &n, &m);
    init(n);

    for (int i = 0; i < m; i++) {
        scanf("%d %d", &s, &t);
        unite(s, t);
    }

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

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3736
final_rank: 1

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

// Union-Find用の構造体
struct UnionFind {
    vector<int> parent, rank;

    UnionFind(int n) {
        parent.resize(n);
        rank.resize(n, 0);
        // 最初は各ノードが自分自身を親として持つ
        for (int i = 0; i < n; ++i) {
            parent[i] = i;
        }
    }

    // find関数：経路圧縮を使って親を探す
    int find(int x) {
        if (parent[x] != x) {
            parent[x] = find(parent[x]);  // 経路圧縮
        }
        return parent[x];
    }

    // union関数：2つの集合を統合する（ランクによる）
    void unionSets(int x, int y) {
        int rootX = find(x);
        int rootY = find(y);

        if (rootX != rootY) {
            // ランクが低い方を高い方にくっつける
            if (rank[rootX] > rank[rootY]) {
                parent[rootY] = rootX;
            } else if (rank[rootX] < rank[rootY]) {
                parent[rootX] = rootY;
            } else {
                parent[rootY] = rootX;
                rank[rootX]++;
            }
        }
    }
};

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

    // Union-Findの初期化
    UnionFind uf(n);

    // 友達関係の入力
    for (int i = 0; i < m; ++i) {
        int s, t;
        cin >> s >> t;
        uf.unionSets(s, t);
    }

    int q;
    cin >> q;

    // 質問の処理
    for (int i = 0; i < q; ++i) {
        int s, t;
        cin >> s >> t;
        if (uf.find(s) == uf.find(t)) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }

    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3836
final_rank: 4

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

struct UnionFind {
    vector<int> parent, rank;
    UnionFind(int n) {
        parent.resize(n);
        rank.assign(n, 0);
        for (int i = 0; i < n; i++) parent[i] = i;
    }
    int find(int x) {
        if (parent[x] == x) return x;
        return parent[x] = find(parent[x]); 
    }
    void unite(int x, int y) {
        int rx = find(x), ry = find(y);
        if (rx == ry) return;
        if (rank[rx] < rank[ry]) parent[rx] = ry;
        else {
            parent[ry] = rx;
            if (rank[rx] == rank[ry]) rank[rx]++;
        }
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
};

int main() {
    int n, m;
    cin >> n >> m;
    UnionFind uf(n);
    for (int i = 0; i < m; i++) {
        int s, t;
        cin >> s >> t;
        uf.unite(s, t);
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int s, t;
        cin >> s >> t;
        if (uf.same(s, t)) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3876
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int,int> PII;
const int N=1e5+5;
vector<int>E[N];
int n,type[N],idx=0,m;
void bfs(int u){
    int t=idx++;
    queue<int>q;
    q.push(u);
    type[u]=t;
    while(!q.empty()){
        int u=q.front();q.pop();
        for(auto v:E[u]){
            if(type[v]!=-1) continue;
            type[v]=t;
            q.push(v);
        }
    }
}
void solve(){
    cin>>n>>m;
    for(int i=0;i<m;i++){
        int u,v;cin>>u>>v;
        E[u].push_back(v);
        E[v].push_back(u);
    }
    for(int i=0;i<n;i++) type[i]=-1;
    for(int i=0;i<n;i++){
        if(type[i]!=-1) continue;
        bfs(i);
    }
    int q;cin>>q;
    while(q--){
        int u,v;cin>>u>>v;
        if(type[u]==type[v]) cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int T=1;
    // cin>>T;
    for(int t=1;t<=T;t++){
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 10040
final_rank: 13

code_snippet: #include <iostream>
#include <climits>
#include <vector>
#include <stack>
#include <algorithm>
#include <memory>
#include <string>
#include <cmath>
#include <iomanip>
#include <array>
using namespace std;

#define MAX (100001)


int getNext(vector<vector<int> > &graph, vector<int> &g_ifsearch, vector<int> &g_next, int n, int id)
{
    int size = graph[id].size();
    for (int i = g_next[id]; i < size; ++i)
    {
        int sid =  graph[id][i];
        if (g_ifsearch[sid] == 0)
        {
            ++g_next[id];
            return graph[id][i];
        } 
    }
    return -1;
}

int depthSearch(vector<vector<int> > &graph, vector<int> &g_ifsearch, vector<int> &g_next, int n, int id, int color)
{
    stack<int> s;
    int next = 0;
    int top = 0;
    s.push(id);
    while (!s.empty())
    {
        top = s.top();
        g_ifsearch[top] = color;
        next = getNext(graph, g_ifsearch, g_next, n, top);
        if (next == -1)
        {
            s.pop();
        }
        else
        {
            s.push(next);
        }
    }
    return 0;
}


int main()
{
    int n = 0, m = 0, p = 0;
    int id = 0;
    int result = 0;
    int color = 1;
    cin >> n;
    vector<vector<int> > graph(n);
    vector<int> g_ifsearch(n);
    vector<int> g_next(n);
    cin >> m;
    for (int i = 0; i < m; ++i)
    {
        int r = 0, c = 0;
        cin >> r;
        cin >> c;
        graph[r].push_back(c);
        graph[c].push_back(r);
    }
    for (int i = 0; i < n; ++i)
    {
        if (g_ifsearch[i] == 0)
        {
            depthSearch(graph, g_ifsearch, g_next, n, i, ++color);
        }
    }

    cin >> p;
    for (int i = 0; i < p; ++i)
    {
        int c1 = 0, c2 = 0;
        cin >> c1;
        cin >> c2;
        cout << ((g_ifsearch[c1] == g_ifsearch[c2]) ? "yes" : "no") << endl;
    }
    // for (auto i : results)
    // {
    //     cout << ((i == 1) ? "yes" : "no") << endl;
    // }
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 9572
final_rank: 14

code_snippet: #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 assignColors()
{
    int id = 0;
    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);
    }

    assignColors();

    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;
 }
accuracy: 1
time_ms: 40
memory_kb: 9220
final_rank: 10

code_snippet: #include <iostream>
#include <vector>

using namespace std;

class UnionFind {
public:
    vector<int> parent, rank;

    UnionFind(int n) {
        parent.resize(n);
        rank.resize(n, 0);
        for (int i = 0; i < n; i++) {
            parent[i] = i;
        }
    }

    int find(int x) {
        if (parent[x] != x) {
            parent[x] = find(parent[x]); // 経路圧縮
        }
        return parent[x];
    }

    void unite(int x, int y) {
        int rootX = find(x);
        int rootY = find(y);
        if (rootX != rootY) {
            if (rank[rootX] < rank[rootY]) {
                parent[rootX] = rootY;
            } else if (rank[rootX] > rank[rootY]) {
                parent[rootY] = rootX;
            } else {
                parent[rootY] = rootX;
                rank[rootX]++;
            }
        }
    }

    bool isConnected(int x, int y) {
        return find(x) == find(y);
    }
};

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

    UnionFind uf(n);

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        uf.unite(a, b);
    }

    int queryCount;
    cin >> queryCount;

    for (int i = 0; i < queryCount; i++) {
        int s, t;
        cin >> s >> t;

        if (uf.isConnected(s, t)) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3732
final_rank: 2

code_snippet: #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 << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 18564
final_rank: 19