#arc173d. D - Bracket Walk

D - Bracket Walk

Score: 700700 points

问题陈述

给定一个有向图 GG,包含 NN 个顶点和 MM 条边。顶点编号从 11NN,每条边都带有 () 的标签。第 ii 条边从顶点 uiu_i 指向顶点 viv_i,并带有标签 cic_i。该图不包含多边或自环。

在这个图中,对于任意两个顶点 sstt,都存在一条从 sstt 的路径。

确定是否存在一个图 GG 上的 遍历,满足以下所有条件:

  • 遍历的起始和结束顶点是相同的。
  • 对于 i=1,2,,Mi=1,2,\dots,M,第 ii 条边在遍历中至少使用一次。
  • 通过按使用顺序排列遍历中使用的边的标签得到的字符串是一个合法的括号序列。

什么是遍历?在图 GG 上的遍历是一个序列 (v1,e1,v2,,vk1,ek1,vk)(v_1,e_1,v_2,\dots,v_{k-1},e_{k-1},v_k),包含 kk 个顶点(kk 是一个正整数)和 k1k-1 条边,其中边 eie_i 从顶点 viv_i 指向顶点 vi+1v_{i+1}。顶点 v1v_1vkv_k 分别称为遍历的起始和结束顶点。什么是合法的括号序列?

合法的括号序列是一个字符串,满足以下任一条件:

  • 它是一个空字符串。
  • 它是一个通过按顺序连接 (、一个合法的括号序列 AA) 得到的字符串。
  • 它是一个通过按顺序连接两个非空的合法括号序列 AABB 得到的字符串。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given a directed graph GG with NN vertices and MM edges. The vertices are numbered from 11 to NN, and each edge is labeled with ( or ). The ii-th edge is directed from vertex uiu_i to vertex viv_i with a label cic_i. The graph does not contain multi-edges or self-loops.

In this graph, for any two vertices ss and tt, there is a path from ss to tt.

Determine if there is a walk on the graph GG that satisfies all of the following conditions:

  • The start and end vertices of the walk are the same.
  • For i=1,2,,Mi=1,2,\dots,M, the ii-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 GG is a sequence (v1,e1,v2,,vk1,ek1,vk)(v_1,e_1,v_2,\dots,v_{k-1},e_{k-1},v_k) of kk vertices (kk is a positive integer) and k1k-1 edges, where the edge eie_i is directed from vertex viv_i to vertex vi+1v_{i+1}. The vertices v1v_1 and vkv_k 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 AA, and ) in this order.
  • It is a string obtained by concatenating two non-empty regular bracket sequences AA and BB in this order.

Constraints

  • 2N40002 \leq N \leq 4000
  • NM8000N \leq M \leq 8000
  • 1ui,viN1 \leq u_i,v_i \leq N
  • cic_i is ( or ).
  • uiviu_i \neq v_i
  • If iji \neq j, then (ui,vi)(uj,vj)(u_i,v_i) \neq (u_j,v_j).
  • All input values are integers.
  • In the input graph, for any two vertices ss and tt, there is a path from ss to tt.

Input

The input is given from Standard Input in the following format:

NN MM

u1u_1 v1v_1 c1c_1

u2u_2 v2v_2 c2c_2

\vdots

uMu_M vMv_M cMc_M

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 1,2,3,4,1,5,6,71,2,3,4,1,5,6,7 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