#P5292. [HNOI2019] 校园旅行

[HNOI2019] 校园旅行

Description

Each building in a school has a unique ID. One day, you feel bored and decide to wander around the campus.

You have stayed on campus for a while and are very familiar with the IDs of all buildings, so you cannot help writing down the IDs of every nearby building. However, you do not really write down the IDs; instead, you take each building’s ID modulo 22, getting 00 or 11 as the label of that building. Concatenating the labels of multiple buildings forms a 01 string.

You are very interested in this string, especially when it is a palindrome, so you decide to study this problem.

The school can be considered as a graph: buildings are vertices, and there are undirected edges between some pairs of vertices. Each vertex has a label (00 or 11). Each time, you choose two vertices in the graph, and you want to know whether there exists a path between these two vertices such that the labels of the vertices visited along the path form a palindrome string.

A palindrome string is a string that is the same after being reversed. For example, “010” and “1001” are palindromes, while “01” and “110” are not. Note that a string of length 11 is always a palindrome, so if the two queried vertices are the same, such a path always exists. Also note that the path does not have to be a simple path, meaning each edge and each vertex may be visited any number of times.

Input Format

The input file name is tour.in.

The first line contains three integers n,m,qn, m, q, representing the number of vertices, the number of edges, and the number of queries.

The second line is a 01 string of length nn, where the ii-th character represents the label of the ii-th vertex (i.e., vertex ii). Vertices are numbered starting from 11.

The next mm lines each contain two integers ui,viu_i, v_i, indicating that there is an undirected edge between vertex uiu_i and vertex viv_i. There are no self-loops or multiple edges.

The next qq lines each contain two integers xi,yix_i, y_i, asking whether there exists a valid path between vertex xix_i and vertex yiy_i.

Output Format

The output file name is tour.out.

Output qq lines. Each line contains the string “YES” or “NO” (without quotes). Output “YES” if such a path exists, and “NO” otherwise.

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

NO
YES
10 11 10
0011011111
4 6
10 6
5 9
4 7
10 7
5 8
1 9
5 7
1 10
5 1
5 6
10 3
7 4
8 10
9 4
8 9
6 6
2 2
9 9
10 9
3 4
NO
YES
YES
NO
YES
YES
YES
YES
YES
NO

Hint

[Sample Explanation 1]

For the first query, vertex 33 and vertex 22 are not connected, so the answer is “NO”.

For the second query, one valid path is 131\to 3, and the string formed by the labels on the path is “00”. Note that the valid path is not unique.

[Constraints]

For 30%30\% of the testdata, 1m1041 \leq m \leq 10 ^ 4.

For 70%70\% of the testdata, 1n30001 \leq n \leq 3000, 1m5×1041 \leq m \leq 5\times 10 ^ 4.

For 100%100\% of the testdata, 1n50001 \leq n \leq 5000, 1m5×1051 \leq m \leq 5\times 10 ^ 5, 1q1051 \leq q \leq 10 ^ 5.

[Compile Commands]

For C++: g++ -o tour tour.cpp –lm -O2

For C: gcc -o tour tour.c –lm -O2

For Pascal: fpc tour.pas -O2

Translated by ChatGPT 5