Problem:
Problem J: Meet on the Island
Problem
やまだくんは、友達と会うために旅行をすることにしました。
やまだくんの住む国は $N$ 個の島からなる島国であり、島と島の間には $M$ 個の海流が流れています。
$i$ 番目の海流が島 $A_i$ から島 $B_i$ に流れている時、またその時のみ、やまだくんは海流に乗ることで島 $A_i$ から島 $B_i$ に移動することができます。
やまだくんは、はじめ島 $1$ にいるものとします。以下の $Q$ 個のクエリを処理してください。
$i$ 番目のクエリでは、クエリの種類を表す $X_i$ と、島の番号を表す $Y_i$ が与えられます。
クエリ 1：
$X_i = 1$ のとき、やまだくんは今いる島から島 $Y_i$ へ $0$ 個以上の海流に乗って移動します。ここで、今いる島から $Y_i$ に $0$ 個以上の海流に乗って移動できることが保証されます。
クエリ 2：
$X_i = 2$ のとき、島 $Y_i$ に住んでいるやまだくんの友達が、 $0$ 個以上の海流に乗って移動することで、やまだくんのいる島にたどり着くことができるか判定してください。
Constraints
入力は以下の条件を満たします。
$1 \leq N \leq 200000$
$0 \leq M \leq \min(N(N-1),200000)$
$1 \leq A_i , B_i \leq N$
$i \neq j$ であって、 $A_i = A_j$ かつ $B_i = B_j$ であるような $( i , j )$ は存在しない。
$1 \leq Q \leq 100000$
$1 \leq X_i \leq 2$
$1 \leq Y_i \leq N$
$X_i = 1$ のとき、やまだくんが今いる島から、$0$ 個以上の海流を辿って $Y_i$ にたどり着けることが保証される。
$X_i = 2$ であるような$ i ( 1 \leq i \leq Q )$ が、少なくとも一つ存在することが保証される。
入力は全て整数である。
Input
入力は以下の形式で標準入力から与えられます。
$N$ $M$
$A_1$ $B_1$
$A_2$ $B_2$
$\vdots$
$A_M$ $B_M$
$Q$
$X_1$ $Y_1$
$X_2$ $Y_2$
$\vdots$
$X_Q$ $Y_Q$
Output
それぞれのクエリ２に対して順番に、移動可能な場合は"YES"、不可能な場合は"NO"を出力してください。
各クエリ2の答えは改行区切りで出力してください。
最後の出力の後に改行を出力するのを忘れないようにしてください。
Sample Input 1
4 4
1 2
2 3
3 4
4 2
3
2 4
1 2
2 4
Sample Output 1
NO
YES
島 $4$ から島 $1$ にはたどり着けないため、やまだくんが島 $1$ にいる間、島 $4$ にいる友達はやまだくんに会うことができません。
その後、やまだくんが島 $2$ に移動すると、島 $4$ にいる友達は島 $2$ に移動することで、やまだくんに会うことができるようになります。
Sample Input 2
2 0
2
2 1
2 2
Sample Output 2
YES
NO
Sample Input 3
7 9
1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
3 6
4
2 7
2 1
1 4
2 3
Sample Output 3
NO
YES
YES
他の海流を飛び越える海流や、海流が流れていない島が存在する場合があります。

Submitted solutions:

code_snippet: #include <bits/extc++.h>

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> AB(M);
  
  vector<vector<int>> nanachi(N), rev(N);
  
  for (int i = 0 ; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    AB[i] = {a, b};
    nanachi[a].push_back(b);
    rev[b].push_back(a);
  }
  
  vector<bool> ok(N);
  
  ok[0] = true;
  
  queue<int> que;
  
  que.push(0);
  
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    
    for (int w : rev[v]) {
      if (ok[w]) continue;
      ok[w] = true;
      que.push(w);
    }
  }
  
  int Q;
  cin >> Q;
  
  while (Q--) {
    int t, x;
    cin >> t >> x;
    x--;
    
    if (t == 1) {
      if (ok[x]) continue;
      
      ok[x] = true;
  
      queue<int> que;
      
      que.push(x);
      
      while (!que.empty()) {
        int v = que.front();
        que.pop();
        
        for (int w : rev[v]) {
          if (ok[w]) continue;
          ok[w] = true;
          que.push(w);
        }
      }
    } else {
      if (ok[x]) {
        cout << "YES\n";
      } else {
        cout << "NO\n";
      }
    }
  }
}
accuracy: 1
time_ms: 100
memory_kb: 23440
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (ll i = a; i < b; i++)
const int INF = (1 << 29);

int main(){
    int N, M;
    cin >> N >> M;
    vector<vector<int>> G(N);
    rep(i, 0, M){
        int a, b;
        cin >> a >> b;
        a--, b--;
        G[b].push_back(a);
    }
    int Q;
    cin >> Q;
    vector<int> seen(N);
    auto upd = [&](int a) -> void {
        if (seen[a]) return;
        vector<int> order = {a};
        seen[a] = 1;
        rep(rp, 0, order.size()){
            int b = order[rp];
            for (auto x : G[b]) if (seen[x] == 0){
                seen[x] = 1;
                order.push_back(x);
            }
        }
    };
    upd(0);
    while (Q--){
        int X, Y;
        cin >> X >> Y;
        Y--;
        if (X == 1) upd(Y);
        else cout << (seen[Y] ? "YES\n" : "NO\n");
    }
}
accuracy: 1
time_ms: 130
memory_kb: 13684
final_rank: 9

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

bool chmin(auto &a, auto b){ return a > b ? a = b, 1 : 0; }
bool chmax(auto &a, auto b){ return a < b ? a = b, 1 : 0; }

const ll linf = 1e18;

void solve(){
    int n, m; cin >> n >> m;
    vector<vector<int>> g(n);
    rep(i,0,m){
        int u, v; cin >> u >> v; u--, v--;
        g[v].emplace_back(u);
    }
    vector<bool> done(n,false);
    auto proc = [&](int start){
        if (done[start]) return ;
        queue<int> que;
        que.push(start);
        done[start] = true;
        while (!que.empty()){
            int v = que.front(); que.pop();
            for (int u : g[v]){
                if (done[u]) continue;
                que.push(u);
                done[u] = true;
            }
        }
    };
    proc(0);
    int q; cin >> q;
    while (q--){
        int t, v; cin >> t >> v; v--;
        if (t == 1){
            proc(v);
        }
        else {
            cout << (done[v] ? "YES" : "NO") << '\n';
        }
    }
}

int main(){
    solve();
}
accuracy: 1
time_ms: 90
memory_kb: 12704
final_rank: 6

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


void solve(){
    int n, m; cin >> n >> m;
    vector<vector<int>> g(n);
    rep(i,0,m){
        int u, v; cin >> u >> v; u--, v--;
        g[v].emplace_back(u);
    }
    vector<bool> done(n,false);
    auto vis = [&](int s){
        if (done[s]) return ;
        queue<int> que;
        que.push(s);
        done[s] = true;
        while (!que.empty()){
            int v = que.front(); que.pop();
            for (int u : g[v]){
                if (!done[u]){
                    done[u] = true;
                    que.push(u);
                }
            }
        }
    };
    int s = 0;
    vis(s);
    int q; cin >> q;
    while (q--){
        int t; cin >> t;
        if (t == 1){
            int v; cin >> v; v--;
            s = v;
            vis(s);
        }
        else if (t == 2){
            int v; cin >> v; v--;
            cout << (done[v] ? "YES" : "NO") << endl;
        }
    }
}

int main(){
    int t = 1; //cin >> t;
    while (t--){
        solve();
    }
}
accuracy: 1
time_ms: 90
memory_kb: 12704
final_rank: 6

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


void solve(){
    int n, m; cin >> n >> m;
    vector<vector<int>> g(n);
    rep(i,0,m){
        int u, v; cin >> u >> v; u--, v--;
        g[v].emplace_back(u);
    }
    vector<bool> done(n,false);
    auto vis = [&](int s){
        if (done[s]) return ;
        queue<int> que;
        que.push(s);
        done[s] = true;
        while (!que.empty()){
            int v = que.front(); que.pop();
            for (int u : g[v]){
                if (!done[u]){
                    done[u] = true;
                    que.push(u);
                }
            }
        }
    };
    int s = 0;
    done[s] = true;
    int q; cin >> q;
    while (q--){
        int t; cin >> t;
        if (t == 1){
            int v; cin >> v; v--;
            s = v;
            vis(s);
        }
        else if (t == 2){
            int v; cin >> v; v--;
            cout << (done[v] ? "YES" : "NO") << endl;
        }
    }
}

int main(){
    int t = 1; //cin >> t;
    while (t--){
        solve();
    }
}
accuracy: 0.14634146341463414
time_ms: 40
memory_kb: 9248
final_rank: 19

code_snippet: // AOJ #3160
// Meet on the Island 2025.2.6

#include <bits/stdc++.h>
using namespace std;
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int N, M;
    cin >> N >> M;
    // 逆グラフを作成：原グラフで A -> B なら，逆グラフでは B -> A
    vector<vector<int>> rev(N+1);
    for(int i = 0; i < M; i++){
        int A, B;
        cin >> A >> B;
        rev[B].push_back(A);
    }
    
    int Q;
    cin >> Q;
    
    // やまだくんの現在位置．初期は1．
    int current = 1;
    // canMeet[x] = true なら，原グラフで x から現在の島にたどり着ける
    vector<bool> canMeet(N+1, false);
    
    // 初期状態：現在の島が1なので，逆グラフで1からたどれる頂点が集合 S(1)
    {
        queue<int> qu;
        qu.push(1);
        canMeet[1] = true;
        while(!qu.empty()){
            int cur = qu.front();
            qu.pop();
            for(auto nxt : rev[cur]){
                if(!canMeet[nxt]){
                    canMeet[nxt] = true;
                    qu.push(nxt);
                }
            }
        }
    }
    
    // 各クエリの処理
    for(int i = 0; i < Q; i++){
        int type, Y;
        cin >> type >> Y;
        if(type == 1){
            // クエリ1：移動
            current = Y;
            // モノトニシティの性質により，
            // S(Y) = { x: x -> Y (原グラフ) } は既に global に含まれている S(current) の部分集合に含まれていなければ，追加する
            if(!canMeet[Y]){
                queue<int> qu;
                qu.push(Y);
                canMeet[Y] = true;
                while(!qu.empty()){
                    int cur = qu.front();
                    qu.pop();
                    for(auto nxt : rev[cur]){
                        if(!canMeet[nxt]){
                            canMeet[nxt] = true;
                            qu.push(nxt);
                        }
                    }
                }
            }
        } else if(type == 2){
            cout << (canMeet[Y] ? "YES" : "NO") << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 12452
final_rank: 5

code_snippet: // competitive-verifier: PROBLEM
#include <iostream>
#include <vector>
/**
 * @brief 重み付きグラフ
 *
 * @tparam T 辺の重みの型
 */
template <class T>
struct Graph {
  private:
    struct _edge {
        constexpr _edge() : _from(), _to(), _weight() {}
        constexpr _edge(int from, int to, T weight) : _from(from), _to(to), _weight(weight) {}
        constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }
        constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }
        constexpr int from() const { return _from; }
        constexpr int to() const { return _to; }
        constexpr T weight() const { return _weight; }
      private:
        int _from, _to;
        T _weight;
    };
  public:
    using edge_type = typename Graph<T>::_edge;
    Graph() : _size(), edges() {}
    Graph(int v) : _size(v), edges(v) {}
    const auto &operator[](int i) const { return edges[i]; }
    auto &operator[](int i) { return edges[i]; }
    const auto begin() const { return edges.begin(); }
    auto begin() { return edges.begin(); }
    const auto end() const { return edges.end(); }
    auto end() { return edges.end(); }
    constexpr int size() const { return _size; }
    void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }
    void add_edge(int from, int to, T weight = T(1)) { edges[from].emplace_back(from, to, weight); }
    void add_edges(int from, int to, T weight = T(1)) {
        edges[from].emplace_back(from, to, weight);
        edges[to].emplace_back(to, from, weight);
    }
    void input_edge(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            T weight;
            std::cin >> from >> to >> weight;
            add_edge(from - base, to - base, weight);
        }
    }
    void input_edges(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            T weight;
            std::cin >> from >> to >> weight;
            add_edges(from - base, to - base, weight);
        }
    }
  private:
    int _size;
    std::vector<std::vector<edge_type>> edges;
};
template <>
struct Graph<void> {
  private:
    struct _edge {
        constexpr _edge() : _from(), _to() {}
        constexpr _edge(int from, int to) : _from(from), _to(to) {}
        constexpr int from() const { return _from; }
        constexpr int to() const { return _to; }
        constexpr int weight() const { return 1; }
        constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }
        constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }
      private:
        int _from, _to;
    };
  public:
    using edge_type = typename Graph<void>::_edge;
    Graph() : _size(), edges() {}
    Graph(int v) : _size(v), edges(v) {}
    const auto &operator[](int i) const { return edges[i]; }
    auto &operator[](int i) { return edges[i]; }
    const auto begin() const { return edges.begin(); }
    auto begin() { return edges.begin(); }
    const auto end() const { return edges.end(); }
    auto end() { return edges.end(); }
    constexpr int size() const { return _size; }
    void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }
    void add_edge(int from, int to) { edges[from].emplace_back(from, to); }
    void add_edges(int from, int to) {
        edges[from].emplace_back(from, to);
        edges[to].emplace_back(to, from);
    }
    void input_edge(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            std::cin >> from >> to;
            add_edge(from - base, to - base);
        }
    }
    void input_edges(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            std::cin >> from >> to;
            add_edges(from - base, to - base);
        }
    }
  private:
    int _size;
    std::vector<std::vector<edge_type>> edges;
};
#ifdef ATCODER
#pragma GCC target("sse4.2,avx512f,avx512dq,avx512ifma,avx512cd,avx512bw,avx512vl,bmi2")
#endif
#pragma GCC optimize("Ofast,fast-math,unroll-all-loops")
#include <bits/stdc++.h>
#ifndef ATCODER
#pragma GCC target("sse4.2,avx2,bmi2")
#endif
template <class T, class U>
constexpr bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
constexpr bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr double EPS = 1e-7;
constexpr double PI = 3.14159265358979323846;
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(20);
    }
    constexpr void operator()() const {}
} sonic;
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) os << (it == v.begin() ? "" : " ") << *it;
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
void Yes(bool is_correct = true) { std::cout << (is_correct ? "Yes\n" : "No\n"); }
void No(bool is_not_correct = true) { Yes(!is_not_correct); }
void YES(bool is_correct = true) { std::cout << (is_correct ? "YES\n" : "NO\n"); }
void NO(bool is_not_correct = true) { YES(!is_not_correct); }
void Takahashi(bool is_correct = true) { std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n'; }
void Aoki(bool is_not_correct = true) { Takahashi(!is_not_correct); }
int main(void) {
    int n, m;
    cin >> n >> m;
    Graph<void> g(n);
    rep (i, m) {
        int a, b;
        cin >> a >> b;
        g.add_edge(b - 1, a - 1);
    }
    vector<bool> visit(n);
    auto dfs = [&](auto self, int x) {
        if (visit[x])
            return;
        visit[x] = true;
        for (auto e : g[x]) self(self, e.to());
    };
    dfs(dfs, 0);
    int q;
    cin >> q;
    rep (i, q) {
        int x, y;
        cin >> x >> y;
        if (x == 1) {
            dfs(dfs, y - 1);
        } else {
            YES(visit[y - 1]);
        }
    }
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 16544
final_rank: 3

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 3160.cc: Problem J: Meet on the Island
 */

#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 200000;

/* typedef */

typedef queue<int> qi;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef stack<int> si;

/* global variables */

vi nbrs[MAX_N], gnbrs[MAX_N];
int gids[MAX_N];
bool used[MAX_N];

/* subroutines */

void scc_visit(const vi *nbrs, int v, vvi& scc,
	       si& S, vb &inS, vi& low, vi& num, int& time) {
  low[v] = num[v] = ++time;
  S.push(v);
  inS[v] = true;

  const vi& nbrv = nbrs[v];
  for (vi::const_iterator vit = nbrv.begin(); vit != nbrv.end(); vit++) {
    const int& w = *vit;
    if (num[w] == 0) {
      scc_visit(nbrs, w, scc, S, inS, low, num, time);
      low[v] = min(low[v], low[w]);
    }
    else if (inS[w])
      low[v] = min(low[v], num[w]);
  }

  if (low[v] == num[v]) {
    scc.push_back(vi());
    for (;;) {
      int w = S.top(); S.pop();
      inS[w] = false;
      scc.back().push_back(w);
      if (v == w) break;
    }
  }
}

void calc_scc(const int n, const vi *nbrs, vvi& scc) {
  vi num(n), low(n);
  si S;
  vb inS(n);
  int time = 0;

  for (int u = 0; u < n; u++)
    if (num[u] == 0)
      scc_visit(nbrs, u, scc, S, inS, low, num, time);
}

void traceback(int st) {
  if (used[st]) return;

  used[st] = true;
  qi q;
  q.push(st);

  while (! q.empty()) {
    int u = q.front(); q.pop();
    for (auto v: gnbrs[u])
      if (! used[v]) {
	used[v] = true;
	q.push(v);
      }
  }
}

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--, v--;
    nbrs[u].push_back(v);
  }

  vvi scc;
  calc_scc(n, nbrs, scc);

  int gn = scc.size();
  for (int i = 0; i < gn; i++)
    for (auto u: scc[i]) gids[u] = i;

  for (int u = 0; u < n; u++) {
    int gu = gids[u];
    for (auto v: nbrs[u]) {
      int gv = gids[v];
      if (gu != gv) gnbrs[gv].push_back(gu);
    }
  }

  int cu = gids[0];
  traceback(cu);

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

  while (qn--) {
    int x, y;
    scanf("%d%d", &x, &y), y--;

    if (x == 1) {
      cu = gids[y];
      traceback(cu);
      //for (int i = 0; i < gn; i++) printf("%d", used[i]); putchar('\n');
    }
    else {
      if (used[gids[y]]) puts("YES");
      else puts("NO");
    }
  }

  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 43112
final_rank: 13

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 3160.cc: Problem J: Meet on the Island
 */

#include<cstdio>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 200000;

/* typedef */

typedef queue<int> qi;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef stack<int> si;
typedef set<int> seti;

/* global variables */

vi nbrs[MAX_N], gnbrs[MAX_N];
int gids[MAX_N];
bool used[MAX_N];

/* subroutines */

void scc_visit(const vi *nbrs, int v, vvi& scc,
	       si& S, vb &inS, vi& low, vi& num, int& time) {
  low[v] = num[v] = ++time;
  S.push(v);
  inS[v] = true;

  const vi& nbrv = nbrs[v];
  for (vi::const_iterator vit = nbrv.begin(); vit != nbrv.end(); vit++) {
    const int& w = *vit;
    if (num[w] == 0) {
      scc_visit(nbrs, w, scc, S, inS, low, num, time);
      low[v] = min(low[v], low[w]);
    }
    else if (inS[w])
      low[v] = min(low[v], num[w]);
  }

  if (low[v] == num[v]) {
    scc.push_back(vi());
    for (;;) {
      int w = S.top(); S.pop();
      inS[w] = false;
      scc.back().push_back(w);
      if (v == w) break;
    }
  }
}

void calc_scc(const int n, const vi *nbrs, vvi& scc) {
  vi num(n), low(n);
  si S;
  vb inS(n);
  int time = 0;

  for (int u = 0; u < n; u++)
    if (num[u] == 0)
      scc_visit(nbrs, u, scc, S, inS, low, num, time);
}

void traceback(int st, seti &ts) {
  if (used[st]) return;

  used[st] = true, ts.insert(st);
  qi q;
  q.push(st);

  while (! q.empty()) {
    int u = q.front(); q.pop();
    for (auto v: nbrs[u])
      if (! used[v]) {
	used[v] = true, ts.insert(v);
	q.push(v);
      }
  }
}

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--, v--;
    nbrs[u].push_back(v);
  }

  vvi scc;
  calc_scc(n, nbrs, scc);

  int gn = scc.size();
  for (int i = 0; i < gn; i++)
    for (auto u: scc[i]) gids[u] = i;

  for (int u = 0; u < n; u++) {
    int gu = gids[u];
    for (auto v: nbrs[u]) {
      int gv = gids[v];
      if (gu != gv) gnbrs[gv].push_back(gu);
    }
  }

  seti ts;
  int cu = gids[0];
  traceback(cu, ts);

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

  while (qn--) {
    int x, y;
    scanf("%d%d", &x, &y), y--;

    if (x == 1) {
      cu = gids[y];
      traceback(cu, ts);
    }
    else {
      if (ts.find(gids[y]) != ts.end()) puts("YES");
      else puts("NO");
    }
  }

  return 0;
}
accuracy: 0.17073170731707318
time_ms: 40
memory_kb: 31616
final_rank: 16

code_snippet: #include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <vector>
#include <numeric>
#include <algorithm>
#include <queue>
#include <string>
#include <random>
#include <array>
#include <climits>
#include <map>
#include <cassert>
#include <stack>
#include <iomanip>
#include <cfloat>
#include <fstream>
std::vector<int> calc_dag(const std::vector<std::vector<int>>& nodes) {
	const int n = nodes.size();
	std::vector<int> out_time(n, -1), indices; indices.reserve(n);
	std::stack<int> stack;
	for (auto root = 0; root < n; ++root) {
		if (out_time[root] != -1) continue;
		stack.push(root);
		while (!stack.empty()) {
			const auto top = stack.top(); stack.pop();
			if (top >= 0) {
				if (out_time[top] != -1) continue;
				out_time[top] = 0;
				stack.push(-1 - top);
				for (const auto next : nodes[top]) {
					if (out_time[next] != -1) continue;
					stack.push(next);
				}
			}
			else {
				indices.push_back(-1 - top);
			}
		}
	}
	std::reverse(indices.begin(), indices.end());
	std::vector<std::vector<int>> reverse(nodes.size());
	for (auto from = 0; from < nodes.size(); ++from) {
		for (const auto to : nodes[from]) {
			reverse[to].push_back(from);
		}
	}
	std::fill(out_time.begin(), out_time.end(), -1);
	int time = 0;
	for (const auto root : indices) {
		if (out_time[root] != -1) continue;
		out_time[root] = time;
		stack.push(root);
		while (!stack.empty()) {
			const auto top = stack.top(); stack.pop();
			for (const auto next : reverse[top]) {
				if (out_time[next] != -1) continue;
				out_time[next] = time;
				stack.push(next);
			}
		}
		++time;
	}
	return out_time;
}
int main() {
	int n, m; std::cin >> n >> m;
	std::vector<std::pair<int, int>> edges(m);
	for (auto& [s, t] : edges) {
		std::cin >> s >> t; --s; --t;
	}
	int q; std::cin >> q;
	std::vector<std::pair<int, int>> queries(q);
	for (auto& [x, y] : queries) {
		std::cin >> x >> y; --y;
	}
	std::vector<std::vector<int>> graph(n);
	for (const auto [s, t] : edges) {
		graph[s].push_back(t);
	}
	const auto dag = calc_dag(graph);
	std::vector<std::vector<int>> dag_rev_graph(n);
	for (const auto [s, t] : edges) {
		dag_rev_graph[dag[t]].push_back(dag[s]);
	}
	std::vector<bool> can_reach(n, false);
	std::queue<int> queue;
	can_reach[dag[0]] = true;
	queue.push(dag[0]);
	while (!queue.empty()) {
		const auto top = queue.front(); queue.pop();
		for (const auto prev : dag_rev_graph[top]) {
			if (can_reach[prev]) continue;
			can_reach[prev] = true;
			queue.push(prev);
		}
	}
	for (const auto [x, y] : queries) {
		if (x == 1) {
			if (can_reach[dag[y]]) continue;
			can_reach[dag[y]] = true;
			queue.push(dag[y]);
			while (!queue.empty()) {
				const auto top = queue.front(); queue.pop();
				for (const auto prev : dag_rev_graph[top]) {
					if (can_reach[prev]) continue;
					can_reach[prev] = true;
					queue.push(prev);
				}
			}
		}
		else {
			if (can_reach[dag[y]]) {
				std::cout << "YES\n";
			}
			else {
				std::cout << "NO\n";
			}
		}
	}
}
accuracy: 1
time_ms: 100
memory_kb: 25520
final_rank: 11

code_snippet: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main(){
    int N, M;
    scanf("%d %d", &N, &M);
    vector<vector<int>> G(N);
    vector<vector<int>> to(N), from(N);
    for(int i = 0; i < M; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        a--;
        b--;
        G[b].push_back(a);
    }
    int Q;
    cin >> Q;
    queue<int> q;
    q.push(0);
    vector<int> can(N, 0);
    can[0] = 1;
    while(!q.empty()){
        int t = q.front();
        q.pop();
        for(int s: G[t]){
            if(can[s] == 0){
                can[s] = 1;
                q.push(s);
            }
        }
    }
    for(int i = 0; i < Q; i++){
        int X, Y;
        scanf("%d %d", &X, &Y);
        Y--;
        if(X == 1){
            if(can[Y] == 0){
                q.push(Y);
                can[Y] = 1;
                while(!q.empty()){
                    int t = q.front();
                    q.pop();
                    for(int s : G[t]){
                        if(can[s] == 0){
                            can[s] = 1;
                            q.push(s);
                        }
                    }
                }
            }
        }
        else{
            if(can[Y] == 1) printf("YES\n");
            else printf("NO\n");
        }
    }
}
accuracy: 1
time_ms: 30
memory_kb: 22604
final_rank: 4

code_snippet: #line 2 "/home/yuruhiya/programming/library/template/template.cpp"
#include <bits/stdc++.h>
#line 4 "/home/yuruhiya/programming/library/template/constants.cpp"
#include <string_view>
#line 7 "/home/yuruhiya/programming/library/template/constants.cpp"

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rfor(i, m, n) for (int i = (m); i >= (n); --i)
#define unless(c) if (!(c))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define range_it(a, l, r) (a).begin() + (l), (a).begin() + (r)

using namespace std;
using ll = long long;
using LD = long double;
using VB = vector<bool>;
using VVB = vector<VB>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VS = vector<string>;
using VD = vector<LD>;
using PII = pair<int, int>;
using VP = vector<PII>;
using PLL = pair<ll, ll>;
using VPL = vector<PLL>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQS = priority_queue<T, vector<T>, greater<T>>;
constexpr int inf = 1000000000;
constexpr long long inf_ll = 1000000000000000000ll, MOD = 1000000007;
constexpr long double PI = 3.14159265358979323846, EPS = 1e-12;
namespace CharacterClass {
	constexpr string_view
	    digit = "0123456789",
	    xdigit = "0123456789ABCDEFabcdef", lower = "abcdefghijklmnopqrstuvwxyz",
	    upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
	    alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
	    alnum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
	    word = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz",
	    punct = R"(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)",
	    graph =
	        R"(!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~)",
	    print =
	        R"( !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~)",
	    blank = " \t", space = " \t\n\r\f\v";
}  // namespace CharacterClass
#line 7 "/home/yuruhiya/programming/library/template/Input.cpp"
using namespace std;

#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#define fwrite_unlocked fwrite
#define fflush_unlocked fflush
#endif
class Scanner {
	static int gc() {
		return getchar_unlocked();
	}
	static char next_char() {
		char c;
		read(c);
		return c;
	}
	template <class T> static void read(T& v) {
		cin >> v;
	}
	static void read(char& v) {
		while (isspace(v = gc()))
			;
	}
	static void read(bool& v) {
		v = next_char() != '0';
	}
	static void read(string& v) {
		v.clear();
		for (char c = next_char(); !isspace(c); c = gc()) v += c;
	}
	static void read(int& v) {
		v = 0;
		bool neg = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c); c = gc()) v = v * 10 + (c - '0');
		if (neg) v = -v;
	}
	static void read(long long& v) {
		v = 0;
		bool neg = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c); c = gc()) v = v * 10 + (c - '0');
		if (neg) v = -v;
	}
	static void read(double& v) {
		v = 0;
		double dp = 1;
		bool neg = false, after_dp = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c) || c == '.'; c = gc()) {
			if (c == '.') {
				after_dp = true;
			} else if (after_dp) {
				v += (c - '0') * (dp *= 0.1);
			} else {
				v = v * 10 + (c - '0');
			}
		}
		if (neg) v = -v;
	}
	static void read(long double& v) {
		v = 0;
		long double dp = 1;
		bool neg = false, after_dp = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c) || c == '.'; c = gc()) {
			if (c == '.') {
				after_dp = true;
			} else if (after_dp) {
				v += (c - '0') * (dp *= 0.1);
			} else {
				v = v * 10 + (c - '0');
			}
		}
		if (neg) v = -v;
	}
	template <class T, class U> static void read(pair<T, U>& v) {
		read(v.first);
		read(v.second);
	}
	template <class T> static void read(vector<T>& v) {
		for (auto& e : v) read(e);
	}
	template <size_t N = 0, class T> static void read_tuple_impl(T& v) {
		if constexpr (N < tuple_size_v<T>) {
			read(get<N>(v));
			read_tuple_impl<N + 1>(v);
		}
	}
	template <class... T> static void read(tuple<T...>& v) {
		read_tuple_impl(v);
	}
	struct ReadVectorHelper {
		size_t n;
		ReadVectorHelper(size_t _n) : n(_n) {}
		template <class T> operator vector<T>() {
			vector<T> v(n);
			read(v);
			return v;
		}
	};
	struct Read2DVectorHelper {
		size_t n, m;
		Read2DVectorHelper(const pair<size_t, size_t>& nm) : n(nm.first), m(nm.second) {}
		template <class T> operator vector<vector<T>>() {
			vector<vector<T>> v(n, vector<T>(m));
			read(v);
			return v;
		}
	};

public:
	string read_line() const {
		string v;
		for (char c = gc(); c != '\n' && c != '\0'; c = gc()) v += c;
		return v;
	}
	template <class T> T read() const {
		T v;
		read(v);
		return v;
	}
	template <class T> vector<T> read_vector(size_t n) const {
		vector<T> a(n);
		read(a);
		return a;
	}
	template <class T> operator T() const {
		return read<T>();
	}
	int operator--(int) const {
		return read<int>() - 1;
	}
	ReadVectorHelper operator[](size_t n) const {
		return ReadVectorHelper(n);
	}
	Read2DVectorHelper operator[](const pair<size_t, size_t>& nm) const {
		return Read2DVectorHelper(nm);
	}
	void operator()() const {}
	template <class H, class... T> void operator()(H&& h, T&&... t) const {
		read(h);
		operator()(forward<T>(t)...);
	}

private:
	template <template <class...> class, class...> struct Multiple;
	template <template <class...> class V, class Head, class... Tail>
	struct Multiple<V, Head, Tail...> {
		template <class... Args> using vec = V<vector<Head>, Args...>;
		using type = typename Multiple<vec, Tail...>::type;
	};
	template <template <class...> class V> struct Multiple<V> { using type = V<>; };
	template <class... T> using multiple_t = typename Multiple<tuple, T...>::type;
	template <size_t N = 0, class T> void multiple_impl(T& t) const {
		if constexpr (N < tuple_size_v<T>) {
			auto& vec = get<N>(t);
			using V = typename remove_reference_t<decltype(vec)>::value_type;
			vec.push_back(read<V>());
			multiple_impl<N + 1>(t);
		}
	}

public:
	template <class... T> auto multiple(size_t h) const {
		multiple_t<T...> result;
		while (h--) multiple_impl(result);
		return result;
	}
} in;
#define inputs(T, ...) \
	T __VA_ARGS__;     \
	in(__VA_ARGS__)
#define ini(...) inputs(int, __VA_ARGS__)
#define inl(...) inputs(long long, __VA_ARGS__)
#define ins(...) inputs(string, __VA_ARGS__)
#line 7 "/home/yuruhiya/programming/library/template/Output.cpp"
#include <charconv>
#line 10 "/home/yuruhiya/programming/library/template/Output.cpp"
using namespace std;

struct BoolStr {
	const char *t, *f;
	BoolStr(const char* _t, const char* _f) : t(_t), f(_f) {}
} Yes("Yes", "No"), yes("yes", "no"), YES("YES", "NO"), Int("1", "0");
struct DivStr {
	const char *d, *l;
	DivStr(const char* _d, const char* _l) : d(_d), l(_l) {}
} spc(" ", "\n"), no_spc("", "\n"), end_line("\n", "\n"), comma(",", "\n"),
    no_endl(" ", "");
class Printer {
	BoolStr B{Yes};
	DivStr D{spc};

public:
	void print(int v) const {
		char buf[12]{};
		if (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {
			fwrite(buf, sizeof(char), ptr - buf, stdout);
		} else {
			assert(false);
		}
	}
	void print(long long v) const {
		char buf[21]{};
		if (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {
			fwrite(buf, sizeof(char), ptr - buf, stdout);
		} else {
			assert(false);
		}
	}
	void print(bool v) const {
		print(v ? B.t : B.f);
	}
	void print(vector<bool>::reference v) const {
		print(v ? B.t : B.f);
	}
	void print(char v) const {
		putchar_unlocked(v);
	}
	void print(const char* v) const {
		fwrite_unlocked(v, 1, strlen(v), stdout);
	}
	void print(double v) const {
		printf("%.20f", v);
	}
	void print(long double v) const {
		printf("%.20Lf", v);
	}
	template <class T> void print(const T& v) const {
		cout << v;
	}
	template <class T, class U> void print(const pair<T, U>& v) const {
		print(v.first);
		print(D.d);
		print(v.second);
	}
	template <class InputIterater>
	void print_range(const InputIterater& begin, const InputIterater& end) const {
		for (InputIterater i = begin; i != end; ++i) {
			if (i != begin) print(D.d);
			print(*i);
		}
	}
	template <class T> void print(const vector<T>& v) const {
		print_range(v.begin(), v.end());
	}
	template <class T, size_t N> void print(const array<T, N>& v) const {
		print_range(v.begin(), v.end());
	}
	template <class T> void print(const vector<vector<T>>& v) const {
		for (size_t i = 0; i < v.size(); ++i) {
			if (i) print(D.l);
			print(v[i]);
		}
	}

	Printer() = default;
	Printer(const BoolStr& _boolstr, const DivStr& _divstr) : B(_boolstr), D(_divstr) {}
	Printer& operator()() {
		print(D.l);
		return *this;
	}
	template <class H> Printer& operator()(H&& h) {
		print(h);
		print(D.l);
		return *this;
	}
	template <class H, class... T> Printer& operator()(H&& h, T&&... t) {
		print(h);
		print(D.d);
		return operator()(forward<T>(t)...);
	}
	template <class InputIterator>
	Printer& range(const InputIterator& begin, const InputIterator& end) {
		print_range(begin, end);
		print(D.l);
		return *this;
	}
	template <class T> Printer& range(const T& a) {
		range(a.begin(), a.end());
		return *this;
	}
	template <class... T> void exit(T&&... t) {
		operator()(forward<T>(t)...);
		std::exit(EXIT_SUCCESS);
	}
	Printer& flush() {
		fflush_unlocked(stdout);
		return *this;
	}
	Printer& set(const BoolStr& b) {
		B = b;
		return *this;
	}
	Printer& set(const DivStr& d) {
		D = d;
		return *this;
	}
	Printer& set(const char* t, const char* f) {
		B = BoolStr(t, f);
		return *this;
	}
} out;
#line 3 "/home/yuruhiya/programming/library/template/Step.cpp"
using namespace std;

template <class T> struct Step {
	using value_type = T;

	class iterator {
		value_type a, b, c;

	public:
		constexpr iterator() : a(value_type()), b(value_type()), c(value_type()) {}
		constexpr iterator(value_type _b, value_type _c, value_type _s)
		    : a(_b), b(_c), c(_s) {}
		constexpr iterator& operator++() {
			--b;
			a += c;
			return *this;
		}
		constexpr iterator operator++(int) {
			iterator tmp = *this;
			--b;
			a += c;
			return tmp;
		}
		constexpr const value_type& operator*() const {
			return a;
		}
		constexpr const value_type* operator->() const {
			return &a;
		}
		constexpr bool operator==(const iterator& i) const {
			return b == i.b;
		}
		constexpr bool operator!=(const iterator& i) const {
			return !(b == i.b);
		}
		constexpr value_type start() const {
			return a;
		}
		constexpr value_type size() const {
			return b;
		}
		constexpr value_type step() const {
			return c;
		}
	};
	constexpr Step(value_type b, value_type c, value_type s) : be(b, c, s) {}
	constexpr iterator begin() const {
		return be;
	}
	constexpr iterator end() const {
		return en;
	}
	constexpr value_type start() const {
		return be.start();
	}
	constexpr value_type size() const {
		return be.size();
	}
	constexpr value_type step() const {
		return be.step();
	}
	constexpr value_type sum() const {
		return start() * size() + step() * (size() * (size() - 1) / 2);
	}
	operator vector<value_type>() const {
		return to_a();
	}
	auto to_a() const {
		vector<value_type> result;
		result.reserve(size());
		for (auto i : *this) {
			result.push_back(i);
		}
		return result;
	}

private:
	iterator be, en;
};
template <class T> constexpr auto step(T a) {
	return Step<T>(0, a, 1);
}
template <class T> constexpr auto step(T a, T b) {
	return Step<T>(a, b - a, 1);
}
template <class T> constexpr auto step(T a, T b, T c) {
	return Step<T>(a, a < b ? (b - a - 1) / c + 1 : 0, c);
}
#line 8 "/home/yuruhiya/programming/library/template/Ruby.cpp"
using namespace std;

template <class F> struct Callable {
	F func;
	Callable(const F& f) : func(f) {}
};
template <class T, class F> auto operator|(const T& v, const Callable<F>& c) {
	return c.func(v);
}

struct Sort_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(begin(v), end(v), f);
			return v;
		});
	}
	template <class T> friend auto operator|(T v, [[maybe_unused]] const Sort_impl& c) {
		sort(begin(v), end(v));
		return v;
	}
} Sort;
struct SortBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(begin(v), end(v),
			     [&](const auto& i, const auto& j) { return f(i) < f(j); });
			return v;
		});
	}
} SortBy;
struct RSort_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(rbegin(v), rend(v), f);
			return v;
		});
	}
	template <class T> friend auto operator|(T v, [[maybe_unused]] const RSort_impl& c) {
		sort(rbegin(v), rend(v));
		return v;
	}
} RSort;
struct RSortBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(begin(v), end(v),
			     [&](const auto& i, const auto& j) { return f(i) > f(j); });
			return v;
		});
	}
} RSortBy;
struct Reverse_impl {
	template <class T> friend auto operator|(T v, const Reverse_impl& c) {
		reverse(begin(v), end(v));
		return v;
	}
} Reverse;
struct Unique_impl {
	template <class T> friend auto operator|(T v, const Unique_impl& c) {
		v.erase(unique(begin(v), end(v), end(v)));
		return v;
	}
} Unique;
struct Uniq_impl {
	template <class T> friend auto operator|(T v, const Uniq_impl& c) {
		sort(begin(v), end(v));
		v.erase(unique(begin(v), end(v)), end(v));
		return v;
	}
} Uniq;
struct Rotate_impl {
	auto operator()(int&& left) {
		return Callable([&](auto v) {
			int s = static_cast<int>(size(v));
			assert(-s <= left && left <= s);
			if (0 <= left) {
				rotate(begin(v), begin(v) + left, end(v));
			} else {
				rotate(begin(v), end(v) + left, end(v));
			}
			return v;
		});
	}
} Rotate;
struct Max_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) { return *max_element(begin(v), end(v), f); });
	}
	template <class T> friend auto operator|(T v, const Max_impl& c) {
		return *max_element(begin(v), end(v));
	}
} Max;
struct Min_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) { return *min_element(begin(v), end(v), f); });
	}
	template <class T> friend auto operator|(T v, const Min_impl& c) {
		return *min_element(begin(v), end(v));
	}
} Min;
struct MaxPos_impl {
	template <class T> friend auto operator|(T v, const MaxPos_impl& c) {
		return max_element(begin(v), end(v)) - begin(v);
	}
} MaxPos;
struct MinPos_impl {
	template <class T> friend auto operator|(T v, const MinPos_impl& c) {
		return min_element(begin(v), end(v)) - begin(v);
	}
} MinPos;
struct MaxBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto max_it = begin(v);
			auto max_val = f(*max_it);
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); max_val < val) {
					max_it = it;
					max_val = val;
				}
			}
			return *max_it;
		});
	}
} MaxBy;
struct MinBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto min_it = begin(v);
			auto min_val = f(*min_it);
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); min_val > val) {
					min_it = it;
					min_val = val;
				}
			}
			return *min_it;
		});
	}
} MinBy;
struct MaxOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto max_val = f(*begin(v));
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); max_val < val) {
					max_val = val;
				}
			}
			return max_val;
		});
	}
} MaxOf;
struct MinOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto min_val = f(*begin(v));
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); min_val > val) {
					min_val = val;
				}
			}
			return min_val;
		});
	}
} MinOf;
struct Count_impl {
	template <class V> auto operator()(const V& val) {
		return Callable([&](auto v) { return count(begin(v), end(v), val); });
	}
} Count;
struct CountIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) { return count_if(begin(v), end(v), f); });
	}
} CountIf;
struct Index_impl {
	template <class V> auto operator()(const V& val) {
		return Callable([&](auto v) -> optional<int> {
			auto result = find(begin(v), end(v), val);
			return result != end(v) ? optional(result - begin(v)) : nullopt;
		});
	}
	template <class V> auto operator()(const V& val, size_t i) {
		return Callable([&](auto v) -> optional<int> {
			auto result = find(next(begin(v), i), end(v), val);
			return result != end(v) ? optional(result - begin(v)) : nullopt;
		});
	}
} Index;
struct IndexIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) -> optional<int> {
			auto result = find_if(begin(v), end(v), f);
			return result != end(v) ? optional(result - begin(v)) : nullopt;
		});
	}
} IndexIf;
struct FindIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) -> optional<typename decltype(v)::value_type> {
			auto result = find_if(begin(v), end(v), f);
			return result != end(v) ? optional(*result) : nullopt;
		});
	}
} FindIf;
struct Sum_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			return accumulate(next(begin(v)), end(v), f(*begin(v)),
			                  [&](const auto& a, const auto& b) { return a + f(b); });
		});
	}
	template <class T> friend auto operator|(T v, const Sum_impl& c) {
		return accumulate(begin(v), end(v), typename T::value_type{});
	}
} Sum;
struct Includes {
	template <class V> auto operator()(const V& val) {
		return Callable([&](auto v) { return find(begin(v), end(v), val) != end(v); });
	}
} Includes;
struct IncludesIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) { return find_if(begin(v), end(v), f) != end(v); });
	}
} IncludesIf;
struct RemoveIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) {
			v.erase(remove_if(begin(v), end(v), f), end(v));
			return v;
		});
	}
} RemoveIf;
struct Each_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				f(i);
			}
		});
	}
} Each;
struct Select_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			using value_type = typename decltype(v)::value_type;
			vector<value_type> result;
			for (const auto& i : v) {
				if (f(i)) result.push_back(i);
			}
			return result;
		});
	}
} Select;
struct Map_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			using result_type = invoke_result_t<F, typename decltype(v)::value_type>;
			vector<result_type> result;
			result.reserve(size(v));
			for (const auto& i : v) {
				result.push_back(f(i));
			}
			return result;
		});
	}
} Map;
struct Indexed_impl {
	template <class T> friend auto operator|(const T& v, Indexed_impl& c) {
		using value_type = typename T::value_type;
		vector<pair<value_type, int>> result;
		result.reserve(size(v));
		int index = 0;
		for (const auto& i : v) {
			result.emplace_back(i, index++);
		}
		return result;
	}
} Indexed;
struct AllOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				if (!f(i)) return false;
			}
			return true;
		});
	}
} AllOf;
struct AnyOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				if (f(i)) return true;
			}
			return false;
		});
	}
} AnyOf;
struct NoneOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				if (f(i)) return false;
			}
			return true;
		});
	}
} NoneOf;

struct Tally_impl {
	template <class F> auto operator()(size_t max_val) {
		return Callable([&](auto v) {
			vector<size_t> result(max_val);
			for (const auto& i : v) {
				result[static_cast<size_t>(i)]++;
			}
			return result;
		});
	}
	template <class T, class value_type = typename T::value_type>
	friend auto operator|(const T& v, Tally_impl& c) {
		map<value_type, size_t> result;
		for (const auto& i : v) {
			result[i]++;
		}
		return result;
	}
} Tally;

template <class T> auto operator*(const vector<T>& a, size_t n) {
	T result;
	for (size_t i = 0; i < n; ++i) {
		result.insert(result.end(), a.begin(), a.end());
	}
	return result;
}
auto operator*(string a, size_t n) {
	string result;
	for (size_t i = 0; i < n; ++i) {
		result += a;
	}
	return result;
}
template <class T, class U> auto& operator<<(vector<T>& a, const U& b) {
	a.insert(a.end(), all(b));
	return a;
}
template <class T> auto& operator<<(string& a, const T& b) {
	a.insert(a.end(), all(b));
	return a;
}
template <class T, class U> auto operator+(vector<T> a, const U& b) {
	a << b;
	return a;
}
template <class T> auto operator+(string a, const T& b) {
	a << b;
	return a;
}
#line 7 "/home/yuruhiya/programming/library/template/functions.cpp"
using namespace std;

template <class T = long long> constexpr T TEN(size_t n) {
	T result = 1;
	for (size_t i = 0; i < n; ++i) result *= 10;
	return result;
}
template <class T, class U,
          enable_if_t<is_integral_v<T> && is_integral_v<U>, nullptr_t> = nullptr>
constexpr auto div_ceil(T n, U m) {
	return (n + m - 1) / m;
}
template <class T, class U> constexpr auto div_ceil2(T n, U m) {
	return div_ceil(n, m) * m;
}
template <class T> constexpr T triangle(T n) {
	return (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1);
}
template <class T> constexpr T nC2(T n) {
	return (n & 1) ? (n - 1) / 2 * n : n / 2 * (n - 1);
}
template <class T, class U> constexpr auto middle(const T& l, const U& r) {
	return l + (r - l) / 2;
}
template <class T, class U, class V>
constexpr bool in_range(const T& v, const U& lower, const V& upper) {
	return lower <= v && v < upper;
}
template <class T, enable_if_t<is_integral_v<T>, nullptr_t> = nullptr>
constexpr bool is_square(T n) {
	T s = sqrt(n);
	return s * s == n || (s + 1) * (s + 1) == n;
}
template <class T = long long> constexpr T BIT(int b) {
	return T(1) << b;
}
template <class T> constexpr int BIT(T x, int i) {
	return (x & (1 << i)) ? 1 : 0;
}
template <class T, class U, enable_if_t<is_integral_v<U>, nullptr_t> = nullptr>
constexpr T Pow(T a, U n) {
	assert(n >= 0);
	T result = 1;
	while (n > 0) {
		if (n & 1) {
			result *= a;
			n--;
		} else {
			a *= a;
			n >>= 1;
		}
	}
	return result;
}
template <class T, class U, enable_if_t<is_integral_v<U>, nullptr_t> = nullptr>
constexpr T Powmod(T a, U n, T mod) {
	assert(n >= 0);
	if (a > mod) a %= mod;
	T result = 1;
	while (n > 0) {
		if (n & 1) {
			result = result * a % mod;
			n--;
		} else {
			a = a * a % mod;
			n >>= 1;
		}
	}
	return result;
}
template <class T> bool chmax(T& a, const T& b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}
template <class T> bool chmin(T& a, const T& b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T> int sz(const T& v) {
	return v.size();
}
template <class T, class U> int lower_index(const T& a, const U& v) {
	return lower_bound(all(a), v) - a.begin();
}
template <class T, class U> int upper_index(const T& a, const U& v) {
	return upper_bound(all(a), v) - a.begin();
}
template <class T> auto Slice(const T& v, size_t i, size_t len) {
	return i < v.size() ? T(v.begin() + i, v.begin() + min(i + len, v.size())) : T();
}
template <class T, class U = typename T::value_type> U Gcdv(const T& v) {
	return accumulate(next(v.begin()), v.end(), U(*v.begin()), gcd<U, U>);
}
template <class T, class U = typename T::value_type> U Lcmv(const T& v) {
	return accumulate(next(v.begin()), v.end(), U(*v.begin()), lcm<U, U>);
}
namespace internal {
	template <class T, size_t N> auto make_vector(vector<int>& sizes, const T& init) {
		if constexpr (N == 1) {
			return vector(sizes[0], init);
		} else {
			int size = sizes[N - 1];
			sizes.pop_back();
			return vector(size, make_vector<T, N - 1>(sizes, init));
		}
	}
}  // namespace internal
template <class T, size_t N>
auto make_vector(const int (&sizes)[N], const T& init = T()) {
	vector s(rbegin(sizes), rend(sizes));
	return internal::make_vector<T, N>(s, init);
}
#line 9 "/home/yuruhiya/programming/library/template/template.cpp"
#if __has_include(<library/dump.hpp>)
#include <library/dump.hpp>
#define LOCAL
#else
#define dump(...) ((void)0)
#endif

template <class T> constexpr T oj_local(const T& oj, const T& local) {
#ifndef LOCAL
	return oj;
#else
	return local;
#endif
}
#line 5 "/home/yuruhiya/programming/library/Graph/StronglyConnectedComponents.cpp"
using namespace std;

class StronglyConnectedComponents {
	int n;
	vector<vector<int>> graph, rgraph;
	vector<bool> used;
	vector<int> cmp, vs;
	int k;
	bool builded = false;
	void dfs(int v) {
		used[v] = true;
		for (auto e : graph[v]) {
			if (!used[e]) dfs(e);
		}
		vs.push_back(v);
	}
	void rdfs(int v, int k) {
		used[v] = true;
		cmp[v] = k;
		for (auto e : rgraph[v]) {
			if (!used[e]) rdfs(e, k);
		}
	}

public:
	StronglyConnectedComponents(int _n) : n(_n), graph(n), rgraph(n) {}
	StronglyConnectedComponents(const vector<vector<int>>& _graph)
	    : n(_graph.size()), graph(_graph), rgraph(n) {
		for (int v = 0; v < n; ++v) {
			for (int u : graph[v]) {
				rgraph[u].push_back(v);
			}
		}
	}
	void add_edge(int s, int t) {
		builded = false;
		graph[s].push_back(t);
		rgraph[t].push_back(s);
	}
	int build() {
		vs.clear();
		used.assign(n, false);
		cmp.assign(n, 0);
		for (int i = 0; i < n; ++i) {
			if (!used[i]) dfs(i);
		}
		k = 0;
		fill(used.begin(), used.end(), false);
		for (int i = vs.size() - 1; i >= 0; --i) {
			if (!used[vs[i]]) rdfs(vs[i], k++);
		}
		builded = true;
		return k;
	}
	int operator[](int i) const {
		assert(builded);
		return cmp[i];
	}
	const vector<int>& get_cmp() const {
		assert(builded);
		return cmp;
	}
	const vector<vector<int>>& get_graph() const {
		assert(builded);
		return graph;
	}
	int count_strongly_components() const {
		assert(builded);
		return k;
	}
	vector<vector<int>> groups() const {
		assert(builded);
		vector<vector<int>> result(k);
		for (int i = 0; i < n; ++i) {
			result[cmp[i]].push_back(i);
		}
		return result;
	}
	vector<vector<int>> make_DAG() const {
		assert(builded);
		vector<vector<int>> result(k);
		for (int i = 0; i < n; ++i) {
			for (auto e : graph[i]) {
				if (cmp[i] != cmp[e]) {
					result[cmp[i]].push_back(cmp[e]);
				}
			}
		}
		for (auto& v : result) {
			sort(v.begin(), v.end());
			v.erase(unique(v.begin(), v.end()), v.end());
		}
		return result;
	}
};
#line 3 "a.cpp"

VVI reverse_edge(const VVI& g) {
	int n = g.size();
	VVI rg(n);
	rep(v, n) {
		for (int u : g[v]) {
			rg[u].push_back(v);
		}
	}
	return rg;
}

int main() {
	ini(n, m);
	StronglyConnectedComponents scc(n);
	rep(i, m) {
		int a = in--, b = in--;
		scc.add_edge(a, b);
	}

	int k = scc.build();
	auto g = scc.make_DAG();
	auto rg = reverse_edge(g);

	VB flag(k);

	auto update_flag = [&](auto&& f, int v) -> void {
		if (flag[v]) return;
		flag[v] = true;
		for (int u : rg[v]) {
			if (!flag[u]) f(f, u);
		}
	};

	update_flag(update_flag, scc[0]);

	for (int q = in; q--;) {
		int x = in, y = in--;
		if (x == 1) {
			update_flag(update_flag, scc[y]);
		} else {
			out.set(YES)(flag[scc[y]]);
		}
	}
}
accuracy: 1
time_ms: 50
memory_kb: 45244
final_rank: 14

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 9223372036854775807
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 200005

int N,E;
bool check[SIZE];
vector<int> rev_G[SIZE];

void dfs(int node_id,int pre){

	check[node_id] = true;
	for(int i = 0; i < rev_G[node_id].size(); i++){
		int next = rev_G[node_id][i];
		if(next == pre||check[next])continue;

		dfs(next,node_id);
	}
}

int main(){

	scanf("%d %d",&N,&E);

	int from,to;
	for(int loop = 0; loop < E; loop++){

		scanf("%d %d",&from,&to);
		from--;
		to--;
		rev_G[to].push_back(from);
	}

	for(int i = 0; i < N; i++){

		check[i] = false;
	}

	dfs(0,-1);

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

	int command,loc;
	int current = 0;

	for(int loop = 0; loop < num_query; loop++){

		scanf("%d %d",&command,&loc);
		loc--;
		if(command == 1){

			current = loc;
			if(!check[current]){
				dfs(current,-1);
			}
		}else{

			if(check[loc]){

				printf("YES\n");
			}else{

				printf("NO\n");
			}
		}
	}

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 15720
final_rank: 2

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 9223372036854775807
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 200005


struct GROUP{
	vector<int> nodes;
};

int N,E;
GROUP group[SIZE]; //強連結成分のグループ
vector<int> scc_G[SIZE];
vector<int> reverse_scc_G[SIZE];
stack<int> S;

bool check[SIZE];
int table[SIZE],in_num[SIZE];
int boss[SIZE],height[SIZE];
int group_index,global_index;


vector<int> G[SIZE];

bool visited[SIZE];
int LEFT[SIZE],RIGHT[SIZE];


void scc_dfs(int node_id){
	check[node_id] = true;

	for(int i = 0; i < scc_G[node_id].size(); i++){
		if(!check[scc_G[node_id][i]])scc_dfs(scc_G[node_id][i]);
	}
	S.push(node_id);
}

void reverse_scc_dfs(int node_id){
	check[node_id] = true;

	group[group_index].nodes.push_back(node_id); //group[group_index]にノードを突っ込む
	table[node_id] = group_index;

	for(int i = 0; i < reverse_scc_G[node_id].size(); i++){
		if(!check[reverse_scc_G[node_id][i]])reverse_scc_dfs(reverse_scc_G[node_id][i]);
	}
}

int get_boss(int id){
	if(boss[id] == id)return id;
	else{
		return boss[id] = get_boss(boss[id]);
	}
}

int isSame(int x,int y){
	return get_boss(x) == get_boss(y);
}

void unite(int x,int y){
	int boss_x = get_boss(x);
	int boss_y = get_boss(y);

	if(boss_x == boss_y)return;

	if(height[boss_x] > height[boss_y]){

		boss[boss_y] = boss_x;

	}else if(height[boss_x] < height[boss_y]){

		boss[boss_x] = boss_y;

	}else{ //height[boss_x] == height[boss_y]

		boss[boss_y] = boss_x;
		height[x]++;
	}
}

void euler_tour(int node_id,int pre){
	LEFT[node_id] = global_index++;
	visited[node_id] = true;

	for(int i = 0; i < G[node_id].size(); i++){

		int next = G[node_id][i];
		if(next == pre)continue;

		euler_tour(next,node_id);
	}
	RIGHT[node_id] = global_index++;
}

int main(){

	scanf("%d %d",&N,&E);

	for(int i = 0; i < N; i++){

		boss[i] = i;
		height[i] = 0;
	}

	int from,to;

	for(int i = 0; i < E; i++){
		scanf("%d %d",&from,&to);
		from--;
		to--;

		scc_G[from].push_back(to);
		reverse_scc_G[to].push_back(from);
	}

	for(int i = 0; i < N; i++)check[i] = false;

	//まずは帰りがけ順を計算
	for(int i = 0; i < N;i++){
		if(!check[i])scc_dfs(i);
	}

	for(int i = 0; i < N;i++)check[i] = false;
	for(int i = 0; i < SIZE; i++)group[i].nodes.clear();

	group_index = -1;
	//ノードを、帰りがけ順の逆順に、各強連結成分(グループ)に分解
	while(!S.empty()){
		if(!check[S.top()]){
			group_index++;

			reverse_scc_dfs(S.top());
		}
		S.pop();
	}


	for(int i = 0; i <= group_index; i++){
		for(int k = 0; k < group[i].nodes.size(); k++){
			for(int p = 0; p < scc_G[group[i].nodes[k]].size(); p++){
				int next_group = table[scc_G[group[i].nodes[k]][p]];
				if(next_group != i){
					G[i].push_back(next_group);
					unite(i,next_group);
				}
			}
		}
	}

	for(int i = 0; i <= group_index; i++){
		if(G[i].size() == 0)continue;

		sort(G[i].begin(),G[i].end());
		G[i].erase(unique(G[i].begin(),G[i].end()),G[i].end());
	}

	for(int i = 0; i <= group_index; i++){

		visited[i] = false;
	}
	for(int i = 0; i <= group_index; i++){
		if(!visited[i]){
			global_index = 0;
			euler_tour(i,-1);
		}
	}

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

	int current = 0;
	int command,loc;

	for(int loop = 0; loop < num_query; loop++){

		scanf("%d %d",&command,&loc);
		loc--;

		if(command == 1){

			current = loc;

		}else{

			int from_group = table[loc];
			int to_group = table[current];

			if(!isSame(from_group,to_group)){

				printf("NO\n");

			}else{

				if(from_group == to_group){

					printf("YES\n");
				}else{

					if(LEFT[from_group] < LEFT[to_group] && RIGHT[from_group] > RIGHT[to_group]){

						printf("YES\n");
					}else{

						printf("NO\n");
					}
				}
			}
		}
	}

	return 0;
}
accuracy: 0.17073170731707318
time_ms: 60
memory_kb: 45564
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
using LL = long long int;
#define incII(i, l, r) for(LL i = (l)    ; i <= (r); i++)
#define incIX(i, l, r) for(LL i = (l)    ; i <  (r); i++)
#define incXI(i, l, r) for(LL i = (l) + 1; i <= (r); i++)
#define incXX(i, l, r) for(LL i = (l) + 1; i <  (r); i++)
#define decII(i, l, r) for(LL i = (r)    ; i >= (l); i--)
#define decIX(i, l, r) for(LL i = (r) - 1; i >= (l); i--)
#define decXI(i, l, r) for(LL i = (r)    ; i >  (l); i--)
#define decXX(i, l, r) for(LL i = (r) - 1; i >  (l); i--)
#define inc(i, n)  incIX(i, 0, n)
#define dec(i, n)  decIX(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec1(i, n) decII(i, 1, n)
auto inII = [](auto x, auto l, auto r) { return (l <= x && x <= r); };
auto inIX = [](auto x, auto l, auto r) { return (l <= x && x <  r); };
auto inXI = [](auto x, auto l, auto r) { return (l <  x && x <= r); };
auto inXX = [](auto x, auto l, auto r) { return (l <  x && x <  r); };
auto setmin   = [](auto & a, auto b) { return (b <  a ? a = b, true : false); };
auto setmax   = [](auto & a, auto b) { return (b >  a ? a = b, true : false); };
auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); };
auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); };
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define FI first
#define SE second
#define FR front()
#define BA back()
#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define RV(c) reverse(ALL(c))
#define SC static_cast
#define SI(c) SC<int>(c.size())
#define SL(c) SC<LL >(c.size())
#define RF(e, c) for(auto & e: c)
#define SF(c, ...) for(auto & [__VA_ARGS__]: c)
#define until(e) while(! (e))
#define if_not(e) if(! (e))
#define ef else if
#define UR assert(false)
auto * IS = & cin;
auto * OS = & cout;
array<string, 3> SEQ = { "", " ", "" };
// input
template<typename T> T in() { T a; (* IS) >> a; return a; }
// input: tuple
template<int I, typename U> void tin_(istream & is, U & t) {
	if constexpr(I < tuple_size<U>::value) { is >> get<I>(t); tin_<I + 1>(is, t); }
}
template<typename ... T> istream & operator>>(istream & is, tuple<T ...> & t) { tin_<0>(is, t); return is; }
template<typename ... T> auto tin() { return in<tuple<T ...>>(); }
// input: array
template<typename T, size_t N> istream & operator>>(istream & is, array<T, N> & a) { RF(e, a) { is >> e; } return is; }
template<typename T, size_t N> auto ain() { return in<array<T, N>>(); }
// input: multi-dimensional vector
template<typename T> T vin() { T v; (* IS) >> v; return v; }
template<typename T, typename N, typename ... M> auto vin(N n, M ... m) {
	vector<decltype(vin<T, M ...>(m ...))> v(n); inc(i, n) { v[i] = vin<T, M ...>(m ...); } return v;
}
// input: multi-column (tuple<vector>)
template<typename U, int I> void colin_([[maybe_unused]] U & t) { }
template<typename U, int I, typename A, typename ... B> void colin_(U & t) {
	get<I>(t).PB(in<A>()); colin_<U, I + 1, B ...>(t);
}
template<typename ... T> auto colin(int n) {
	tuple<vector<T> ...> t; inc(i, n) { colin_<tuple<vector<T> ...>, 0, T ...>(t); } return t;
}
// output
void out_([[maybe_unused]] string s) { }
template<typename A> void out_([[maybe_unused]] string s, A && a) { (* OS) << a; }
template<typename A, typename ... B> void out_(string s, A && a, B && ... b) { (* OS) << a << s; out_(s, b ...); }
auto outF = [](auto x, auto y, auto z, auto ... a) { (* OS) << x; out_(y, a ...); (* OS) << z << flush; };
auto out  = [](auto ... a) { outF("", " " , "\n", a ...); };
auto outS = [](auto ... a) { outF("", " " , " " , a ...); };
auto outL = [](auto ... a) { outF("", "\n", "\n", a ...); };
auto outN = [](auto ... a) { outF("", ""  , ""  , a ...); };
// output: multi-dimensional vector
template<typename T> ostream & operator<<(ostream & os, vector<T> const & v) {
	os << SEQ[0]; inc(i, SI(v)) { os << (i == 0 ? "" : SEQ[1]) << v[i]; } return (os << SEQ[2]);
}
template<typename T> void vout_(T && v) { (* OS) << v; }
template<typename T, typename A, typename ... B> void vout_(T && v, A a, B ... b) {
	inc(i, SI(v)) { (* OS) << (i == 0 ? "" : a); vout_(v[i], b ...); }
}
template<typename T, typename A, typename ... B> void vout (T && v, A a, B ... b) { vout_(v, a, b ...); (* OS) << a << flush; }
template<typename T, typename A, typename ... B> void voutN(T && v, A a, B ... b) { vout_(v, a, b ...); (* OS)      << flush; }

// ---- ----

int main() {
	auto [n, m] = ain<int, 2>();
	vector<vector<int>> g(n);
	inc(i, m) {
		auto [a, b] = ain<int, 2>();
		a--; b--;
		g[b].PB(a);
	}
	
	vector<bool> r(n, false);
	function<void(int)> dfs = [&](int v) {
		if(r[v]) { return; }
		r[v] = true;
		RF(e, g[v]) { dfs(e); }
	};
	dfs(0);
	
	vector<string> ans;
	auto Q = in<int>();
	inc(q, Q) {
		auto [x, y] = ain<int, 2>();
		y--;
		if(x == 1) {
			dfs(y);
		} else {
			ans.PB(r[y] ? "YES" : "NO");
		}
	}
	vout(ans, "\n");
}
accuracy: 1
time_ms: 90
memory_kb: 19012
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using LL = long long int;
#define incII(i, l, r) for(LL i = (l)    ; i <= (r); i++)
#define incIX(i, l, r) for(LL i = (l)    ; i <  (r); i++)
#define incXI(i, l, r) for(LL i = (l) + 1; i <= (r); i++)
#define incXX(i, l, r) for(LL i = (l) + 1; i <  (r); i++)
#define decII(i, l, r) for(LL i = (r)    ; i >= (l); i--)
#define decIX(i, l, r) for(LL i = (r) - 1; i >= (l); i--)
#define decXI(i, l, r) for(LL i = (r)    ; i >  (l); i--)
#define decXX(i, l, r) for(LL i = (r) - 1; i >  (l); i--)
#define inc(i, n)  incIX(i, 0, n)
#define dec(i, n)  decIX(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec1(i, n) decII(i, 1, n)
auto inII = [](auto x, auto l, auto r) { return (l <= x && x <= r); };
auto inIX = [](auto x, auto l, auto r) { return (l <= x && x <  r); };
auto inXI = [](auto x, auto l, auto r) { return (l <  x && x <= r); };
auto inXX = [](auto x, auto l, auto r) { return (l <  x && x <  r); };
auto setmin   = [](auto & a, auto b) { return (b <  a ? a = b, true : false); };
auto setmax   = [](auto & a, auto b) { return (b >  a ? a = b, true : false); };
auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); };
auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); };
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define FI first
#define SE second
#define FR front()
#define BA back()
#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define RV(c) reverse(ALL(c))
#define SC static_cast
#define SI(c) SC<int>(c.size())
#define SL(c) SC<LL >(c.size())
#define RF(e, c) for(auto & e: c)
#define SF(c, ...) for(auto & [__VA_ARGS__]: c)
#define until(e) while(! (e))
#define if_not(e) if(! (e))
#define ef else if
#define UR assert(false)
auto * IS = & cin;
auto * OS = & cout;
array<string, 3> SEQ = { "", " ", "" };
// input
template<typename T> T in() { T a; (* IS) >> a; return a; }
// input: tuple
template<int I, typename U> void tin_(istream & is, U & t) {
	if constexpr(I < tuple_size<U>::value) { is >> get<I>(t); tin_<I + 1>(is, t); }
}
template<typename ... T> istream & operator>>(istream & is, tuple<T ...> & t) { tin_<0>(is, t); return is; }
template<typename ... T> auto tin() { return in<tuple<T ...>>(); }
// input: array
template<typename T, size_t N> istream & operator>>(istream & is, array<T, N> & a) { RF(e, a) { is >> e; } return is; }
template<typename T, size_t N> auto ain() { return in<array<T, N>>(); }
// input: multi-dimensional vector
template<typename T> T vin() { T v; (* IS) >> v; return v; }
template<typename T, typename N, typename ... M> auto vin(N n, M ... m) {
	vector<decltype(vin<T, M ...>(m ...))> v(n); inc(i, n) { v[i] = vin<T, M ...>(m ...); } return v;
}
// input: multi-column (tuple<vector>)
template<typename U, int I> void colin_([[maybe_unused]] U & t) { }
template<typename U, int I, typename A, typename ... B> void colin_(U & t) {
	get<I>(t).PB(in<A>()); colin_<U, I + 1, B ...>(t);
}
template<typename ... T> auto colin(int n) {
	tuple<vector<T> ...> t; inc(i, n) { colin_<tuple<vector<T> ...>, 0, T ...>(t); } return t;
}
// output
void out_([[maybe_unused]] string s) { }
template<typename A> void out_([[maybe_unused]] string s, A && a) { (* OS) << a; }
template<typename A, typename ... B> void out_(string s, A && a, B && ... b) { (* OS) << a << s; out_(s, b ...); }
auto outF = [](auto x, auto y, auto z, auto ... a) { (* OS) << x; out_(y, a ...); (* OS) << z << flush; };
auto out  = [](auto ... a) { outF("", " " , "\n", a ...); };
auto outS = [](auto ... a) { outF("", " " , " " , a ...); };
auto outL = [](auto ... a) { outF("", "\n", "\n", a ...); };
auto outN = [](auto ... a) { outF("", ""  , ""  , a ...); };
// output: multi-dimensional vector
template<typename T> ostream & operator<<(ostream & os, vector<T> const & v) {
	os << SEQ[0]; inc(i, SI(v)) { os << (i == 0 ? "" : SEQ[1]) << v[i]; } return (os << SEQ[2]);
}
template<typename T> void vout_(T && v) { (* OS) << v; }
template<typename T, typename A, typename ... B> void vout_(T && v, A a, B ... b) {
	inc(i, SI(v)) { (* OS) << (i == 0 ? "" : a); vout_(v[i], b ...); }
}
template<typename T, typename A, typename ... B> void vout (T && v, A a, B ... b) { vout_(v, a, b ...); (* OS) << a << flush; }
template<typename T, typename A, typename ... B> void voutN(T && v, A a, B ... b) { vout_(v, a, b ...); (* OS)      << flush; }

// ---- ----

int main() {
	auto [n, m] = ain<int, 2>();
	vector<vector<int>> g(n);
	inc(i, m) {
		auto [a, b] = ain<int, 2>();
		a--; b--;
		g[b].PB(a);
	}
	
	vector<bool> r(n, false);
	r[0] = true;
	function<void(int)> dfs = [&](int v) {
		if(r[v]) { return; }
		r[v] = true;
		RF(e, g[v]) { dfs(e); }
	};
	
	vector<string> ans;
	auto Q = in<int>();
	inc(q, Q) {
		auto [x, y] = ain<int, 2>();
		y--;
		if(x == 1) {
			dfs(y);
		} else {
			ans.PB(r[y] ? "YES" : "NO");
		}
	}
	vout(ans, "\n");
}
accuracy: 0.14634146341463414
time_ms: 60
memory_kb: 8528
final_rank: 20

code_snippet: // clang-format off
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
// clang-format on

struct SCC {
    int V;
    vector<vector<int>> G, rG;
    vector<int> vs;   // 帰りがけ順の並び
    vector<int> cmp;  //属する強連結成分トポロジカル順序
    vector<bool> used;

    SCC() {}
    SCC(int n) {
        V = n;
        G = vector<vector<int>>(n);
        rG = vector<vector<int>>(n);
    }

    void add_edge(int from, int to) {
        G[from].push_back(to);
        rG[to].push_back(from);
    }

    void dfs(int v) {
        used[v] = true;
        rep(i, G[v].size()) if (!used[G[v][i]]) dfs(G[v][i]);
        vs.push_back(v);
    }

    void rdfs(int v, int k) {
        used[v] = true;
        cmp[v] = k;
        rep(i, rG[v].size()) if (!used[rG[v][i]]) rdfs(rG[v][i], k);
    }

    int scc() {
        used = vector<bool>(V, false);
        vs.clear();
        rep(i, V) if (!used[i]) dfs(i);

        used = vector<bool>(V, false);
        cmp = vector<int>(V);
        int num_scc = 0;
        for (int i = vs.size() - 1; i >= 0; --i)
            if (!used[vs[i]]) rdfs(vs[i], num_scc++);
        return num_scc;
    }
};

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

    SCC s(n);

    vector<int> a(m), b(m);
    rep(i, m) {
        cin >> a[i] >> b[i];
        --a[i];
        --b[i];
        s.add_edge(a[i], b[i]);
    }

    const int V = s.scc();
    vector<vector<int>> rG(V);
    rep(i, m) {
        int u = s.cmp[a[i]], v = s.cmp[b[i]];
        if (u != v) rG[v].pb(u);
    }

    vector<bool> vis(V);
    auto MOVE = [&](int x) {
        if (vis[x]) return;
        vis[x] = true;
        queue<int> que({x});
        while (!que.empty()) {
            int v = que.front();
            que.pop();
            for (int e : rG[v])
                if (!vis[e]) {
                    vis[e] = true;
                    que.push(e);
                }
        }
    };

    MOVE(s.cmp[0]);

    int q;
    cin >> q;
    rep(qi, q) {
        int x, y;
        cin >> x >> y;
        --y;
        if (x == 1)
            MOVE(s.cmp[y]);
        else
            cout << (vis[s.cmp[y]] ? "YES" : "NO") << "\n";
    }
    return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 34960
final_rank: 15

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

int n, m, q;
vector<vector<int>> g, rg;
vector<bool> reachable;

void update(int x);

int main() {
  cin >> n >> m;
  g.resize(n);
  rg.resize(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    g[--a].push_back(--b);
    rg[b].push_back(a);
  }
  reachable.assign(n, 0);
  update(0);
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    if (x)
      cout << (reachable[y] ? "YES" : "NO") << endl;
    else
      update(y);
  }
  return 0;
}

void update(int x) {
  queue<int> qu;
  qu.push(x);
  if (reachable[x]) return;
  reachable[x] = 1;
  while (qu.size()) {
    int now = qu.front();
    qu.pop();
    reachable[now] = 1;
    for (auto to : rg[now])
      if (!reachable[to]) {
        reachable[to] = 1;
        qu.push(to);
      }
  }
}
accuracy: 1
time_ms: 140
memory_kb: 21480
final_rank: 12

code_snippet: //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;

//----------------------- Print Function ----------------------//

inline void print() {
    cout << '\n';
}
template <typename First, typename... Rest>
void print(const First &first, const Rest &... rest) {
    cout << first << ' ';
    print(rest...);
}

template <typename T>
void print(const T &a) {
    for (auto e : a) cout << e << ' ';
    cout << '\n';
}

//------------------------- Libraries -------------------------//

//--------------------------- Solve ---------------------------//

using Graph = vector<vector<int> >;

int N, M;
vector<bool> reach;
Graph g;

void dfs(int v) {
    reach[v] = true;
    for (int e : g[v]) {
        if (!reach[e]) dfs(e);
    }
}

void solve() {
    cin >> N >> M;
    g.resize(N);
    reach.resize(N);
    for (int i = 0; i < M; i++) {
        int a, b; cin >> a >> b;
        a--; b--;
        g[b].push_back(a);
    }

    reach[0] = true;
    dfs(0);

    int Q; cin >> Q;
    while (Q--) {
        int x, y; cin >> x >> y;
        y--;
        if (x == 1) {
            if (!reach[y]) dfs(y);
        }
        else if (x == 2) {
            if (reach[y]) cout << "YES" << '\n';
            else cout << "NO" << '\n';
        }
    }
}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 12296
final_rank: 1

code_snippet: //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;

//----------------------- Print Function ----------------------//

inline void print() {
    cout << '\n';
}
template <typename First, typename... Rest>
void print(const First &first, const Rest &... rest) {
    cout << first << ' ';
    print(rest...);
}

template <typename T>
void print(const T &a) {
    for (auto e : a) cout << e << ' ';
    cout << '\n';
}

//------------------------- Libraries -------------------------//

//--------------------------- Solve ---------------------------//

using Graph = vector<vector<int> >;

int N, M;
vector<bool> reach;
Graph g;

void dfs(int v) {
    if (reach[v]) return;
    reach[v] = true;
    for (int e : g[v]) {
        if (!reach[e]) dfs(e);
    }
}

void solve() {
    cin >> N >> M;
    g.resize(N);
    reach.resize(N);
    for (int i = 0; i < M; i++) {
        int a, b; cin >> a >> b;
        a--; b--;
        g[b].push_back(a);
    }

    reach[0] = true;
    dfs(0);
    
    int Q; cin >> Q;
    while (Q--) {
        int x, y; cin >> x >> y;
        y--;
        if (x == 1) {
            dfs(y);
        }
        else if (x == 2) {
            if (reach[y]) cout << "YES" << '\n';
            else cout << "NO" << '\n';
        }
    }
}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 0.14634146341463414
time_ms: 20
memory_kb: 8696
final_rank: 18