Reachability Query 2

AtCoder
IDabc435_d
Time3000ms
Memory256MB
Difficulty
You are given a directed graph with $N$ vertices and $M$ edges. The vertices are numbered from $1$ to $N$, and the $i$\-th edge is a directed edge from vertex $X_i$ to vertex $Y_i$. Initially, all vertices are white. Process $Q$ queries in order. Each query is of one of the following two types: * `1 v`: Color vertex $v$ black. * `2 v`: Determine whether it is possible to reach a black vertex by following edges from vertex $v$. ## Constraints * $1\leq N \leq 3\times 10^5$ * $0\leq M \leq 3\times 10^5$ * $1\leq Q \leq 3\times 10^5$ * $1\leq X_i,Y_i \leq N$ * There are no self-loops, that is, $X_i \neq Y_i$. * There are no multiple edges, that is, $(X_i,Y_i)$ are distinct. * In queries, $1 \leq v \leq N$. * All input values are integers. ## Input The input is given from Standard Input in the following format: $N$ $M$ $X_1$ $Y_1$ $\vdots$ $X_M$ $Y_M$ $Q$ $\mathrm{query}_1$ $\vdots$ $\mathrm{query}_Q$ $\mathrm{query}_i$ represents the $i$\-th query and is given in one of the following formats: $1$ $v$ $2$ $v$ [samples]
Samples
Input #1
5 6
1 2
2 3
3 1
4 5
1 4
2 5
5
1 3
2 1
2 4
1 5
2 4
Output #1
Yes
No
Yes

*   Initially, the given graph is as shown in the leftmost figure below.
*   By the first query, vertex $3$ becomes black, as shown in the center figure.
*   In the second query, it is possible to reach black vertex $3$ from vertex $1$.
*   In the third query, it is not possible to reach a black vertex from vertex $4$.
*   By the fourth query, vertex $5$ becomes black, as shown in the rightmost figure.
*   In the fifth query, it is possible to reach black vertex $5$ from vertex $4$.

![image](https://img.atcoder.jp/abc435/82e480fb4420fdfd7a0f13168774c1fb.png)
API Response (JSON)
{
  "problem": {
    "name": "Reachability Query 2",
    "description": {
      "content": "You are given a directed graph with $N$ vertices and $M$ edges.   The vertices are numbered from $1$ to $N$, and the $i$\\-th edge is a directed edge from vertex $X_i$ to vertex $Y_i$.   Initially, all",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 3000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc435_d"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a directed graph with $N$ vertices and $M$ edges.  \nThe vertices are numbered from $1$ to $N$, and the $i$\\-th edge is a directed edge from vertex $X_i$ to vertex $Y_i$.  \nInitially, all...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments