problems / 285

Unbreakable Routes (medium version)

medium graphs bridges dfs 1000 ms · 256 MB

Unbreakable Routes (medium version)

A network has n routers, numbered from 1 to n, and m cables. Each cable joins two different routers and carries traffic in both directions. Two routers may be joined by several cables, and the network does not have to be connected.

Two routers u and v are unbreakable if traffic can get from u to v now, and it still can after any single cable is cut, no matter which cable that is. A router is always unbreakable with itself.

Answer q queries: for each pair u, v, decide whether they are unbreakable.

Input

The first line contains two integers n and m (1 ≤ n ≤ 2·10^5, 0 ≤ m ≤ 2·10^5), the number of routers and cables.

Each of the next m lines contains two integers a_i and b_i (1 ≤ a_i, b_i ≤ n, a_i ≠ b_i): cable i joins routers a_i and b_i. The same pair of routers may appear several times.

The next line contains one integer q (1 ≤ q ≤ 2·10^5), the number of queries.

Each of the next q lines contains two integers u and v (1 ≤ u, v ≤ n). u and v may be equal.

Output

For each query, print YES on its own line if u and v are unbreakable, and NO otherwise.

Notes

In the first example, routers 1, 2 and 3 form a ring, so any two of them stay connected when one cable is cut. Cable 3–4 is the only way to reach router 4, so cutting it separates 4 from the rest. Routers 4 and 5 are joined by two cables, so cutting one leaves the other. Router 6 has no cables at all and only reaches itself.

In the second example, the routers form the line 1–2–3–4. Cutting cable 1–2 or cable 3–4 breaks the routes that use it, but routers 2 and 3 are joined by two parallel cables, so no single cut separates them.

Example 1

Input

6 6
1 2
2 3
3 1
3 4
4 5
5 4
6
1 3
2 4
4 5
3 5
6 6
1 6

Output

YES
NO
YES
NO
YES
NO

Example 2

Input

4 4
1 2
2 3
3 2
3 4
4
1 2
2 3
1 4
4 4

Output

NO
YES
NO
YES

Hints · reveal one at a time if you're stuck

Hint 1

Most cables don't matter: cutting a cable that lies on a cycle leaves every pair of routers as connected as before. Which cables can actually separate two routers?

Hint 2

Remove all cables whose cut disconnects their own two ends. Routers are unbreakable exactly when they are still in the same piece afterwards, so label the pieces once and answer every query by comparing labels.

Hint 3

Those cables are the bridges, found in one DFS with Tarjan's low-link values. Skip only the cable you arrived by (by its index), since a parallel cable is a real detour, and use an iterative DFS: the graph can be a path 2·10^5 routers long.

report an issue Prefer your own editor? sooks prep 285 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.