#P10572. [JRKSJ R8] +1-1
[JRKSJ R8] +1-1
Problem Description
You are given an undirected graph with vertices and edges. Each vertex has a character ( or ) on it.
There are queries. Each query gives , and you need to determine whether there exists a path from to (it does not have to be a simple path) such that, if you write down the characters on the vertices along the path in order, the resulting string is a valid bracket sequence.
Input Format
The first line contains three integers .
The second line contains a string of length consisting only of ( and ), representing the characters on vertices .
The next lines each contain two integers , denoting an edge in the graph.
The next lines each contain two integers .
Output Format
Output a 01 string of length . The -th character represents the answer to the -th query: output 1 if such a path exists, otherwise output 0.
5 6 5
((())
1 2
1 3
2 3
3 4
4 5
2 4
1 2
3 4
1 4
1 5
2 5
01111
Hint
Definition of a valid bracket sequence:
- The empty string is a valid bracket sequence.
- If are valid bracket sequences, then is a valid bracket sequence.
- If is a valid bracket sequence, then is a valid bracket sequence.
- Any other string is not a valid bracket sequence.
For example, () and (()()) are valid bracket sequences, while (() and ())( are not.
Sample Explanation
To make it easier to read, there is a blank line between the edges and the queries in the sample input. However, this blank line does not exist in the actual testdata.

Vertices have character (, and vertices have character ).
: Obviously, a valid bracket sequence cannot end with (.
: The string represented by the path is ().
: The string represented by the path is ((())).
: The string represented by the path is (()).
: The string represented by the path is (()).
Constraints
This problem uses bundled testdata.
- Subtask 1 (20 pts): , .
- Subtask 2 (30 pts): The graph is a forest.
- Subtask 3 (20 pts): .
- Subtask 4 (30 pts): No special restrictions.
For all testdata, , $0 \le m \le \min(\frac{n\times(n-1)}{2}, 5\times 10^5)$, . The given graph is guaranteed to have no multiple edges and no self-loops.
Translated by ChatGPT 5