#abc266f. F - Well-defined Path Queries on a Namori

F - Well-defined Path Queries on a Namori

Score : 500500 points

问题描述

给定一个连通的无向简单图 GG,包含 NN 个顶点,编号为 11NN,以及 NN 条边。第 ii 条边在顶点 uiu_i 和顶点 viv_i 之间双向连接。

接下来回答 QQ 个查询:

  • 确定是否存在一条从顶点 xix_i 到顶点 yiy_i 的唯一简单路径(简单路径是指不重复经过顶点的路径)。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a connected simple undirected graph GG with NN vertices numbered 11 to NN and NN edges. The ii-th edge connects Vertex uiu_i and Vertex viv_i bidirectionally.

Answer the following QQ queries.

  • Determine whether there is a unique simple path from Vertex xix_i to Vertex yiy_i (a simple path is a path without repetition of vertices).

Constraints

  • 3N2×1053 \leq N \leq 2 \times 10^5
  • 1ui<viN1 \leq u_i < v_i\leq N
  • (ui,vi)(uj,vj)(u_i,v_i) \neq (u_j,v_j) if iji \neq j.
  • GG is a connected simple undirected graph with NN vertices and NN edges.
  • 1Q2×1051 \leq Q \leq 2 \times 10^5
  • 1xi<yiN1 \leq x_i < y_i\leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

u1u_1 v1v_1

u2u_2 v2v_2

\vdots

uNu_N vNv_N

QQ

x1x_1 y1y_1

x2x_2 y2y_2

\vdots

xQx_Q yQy_Q

Output

Print QQ lines.

The ii-th line (1iQ)(1 \leq i \leq Q) should contain Yes if there is a unique simple path from Vertex xix_i to Vertex yiy_i, and No otherwise.

Sample Input 1

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

Sample Output 1

No
Yes
No

The simple paths from Vertex 11 to 22 are (1,2)(1,2) and (1,3,2)(1,3,2), which are not unique, so the answer to the first query is No.

The simple path from Vertex 11 to 44 is (1,4)(1,4), which is unique, so the answer to the second query is Yes.

The simple paths from Vertex 11 to 55 are (1,2,5)(1,2,5) and (1,3,2,5)(1,3,2,5), which are not unique, so the answer to the third query is No.

Sample Input 2

10
3 5
5 7
4 8
2 9
1 2
7 9
1 6
4 10
2 5
2 10
10
1 8
6 9
8 10
6 8
3 10
3 9
1 10
5 8
1 10
7 8

Sample Output 2

Yes
No
Yes
Yes
No
No
Yes
No
Yes
No

update @ 2024/3/10 11:15:16