#arc173d. D - Bracket Walk
D - Bracket Walk
Score: points
问题陈述
给定一个有向图 ,包含 个顶点和 条边。顶点编号从 到 ,每条边都带有 (
或 )
的标签。第 条边从顶点 指向顶点 ,并带有标签 。该图不包含多边或自环。
在这个图中,对于任意两个顶点 和 ,都存在一条从 到 的路径。
确定是否存在一个图 上的 遍历,满足以下所有条件:
- 遍历的起始和结束顶点是相同的。
- 对于 ,第 条边在遍历中至少使用一次。
- 通过按使用顺序排列遍历中使用的边的标签得到的字符串是一个合法的括号序列。
什么是遍历?在图 上的遍历是一个序列 ,包含 个顶点( 是一个正整数)和 条边,其中边 从顶点 指向顶点 。顶点 和 分别称为遍历的起始和结束顶点。什么是合法的括号序列?
合法的括号序列是一个字符串,满足以下任一条件:
- 它是一个空字符串。
- 它是一个通过按顺序连接
(
、一个合法的括号序列 和)
得到的字符串。 - 它是一个通过按顺序连接两个非空的合法括号序列 和 得到的字符串。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
You are given a directed graph with vertices and edges. The vertices are numbered from to , and each edge is labeled with (
or )
. The -th edge is directed from vertex to vertex with a label . The graph does not contain multi-edges or self-loops.
In this graph, for any two vertices and , there is a path from to .
Determine if there is a walk on the graph that satisfies all of the following conditions:
- The start and end vertices of the walk are the same.
- For , the -th edge is used at least once in the walk.
- The string obtained by arranging the labels of the edges used in the walk in the order of their usage is a regular bracket sequence.
What is a walk? A walk on a graph is a sequence of vertices ( is a positive integer) and edges, where the edge is directed from vertex to vertex . The vertices and are called the start and end vertices of the walk, respectively. What is a regular bracket sequence?
A regular bracket sequence is a string that satisfies one of the following conditions:
- It is an empty string.
- It is a string obtained by concatenating
(
, a regular bracket sequence , and)
in this order. - It is a string obtained by concatenating two non-empty regular bracket sequences and in this order.
Constraints
- is
(
or)
. - If , then .
- All input values are integers.
- In the input graph, for any two vertices and , there is a path from to .
Input
The input is given from Standard Input in the following format:
Output
If there is a walk satisfying the conditions, print Yes
; otherwise, print No
.
Sample Input 1
5 7
1 2 (
2 3 )
3 4 (
4 1 )
2 4 )
4 5 (
5 1 )
Sample Output 1
Yes
The walk that uses edges in this order uses all the edges at least once, and the string ()()()()
obtained by arranging the labels of the edges in the order of their usage is a regular bracket sequence, so all conditions are satisfied.
The walk may use the same edge multiple times or visit the same vertex multiple times.
Sample Input 2
2 2
1 2 )
2 1 )
Sample Output 2
No
Sample Input 3
10 20
4 5 (
5 6 (
6 7 )
2 5 )
5 8 (
6 3 )
8 5 )
1 2 (
9 10 (
4 7 (
3 4 )
8 9 (
2 1 )
1 4 )
2 3 )
3 2 (
7 8 (
7 4 )
10 9 )
9 8 )
Sample Output 3
Yes