Institute

시간 제한1초메모리 제한2048 MB

요약
패스가 필요한 간선과 필요 없는 간선이 섞인 방향 그래프에서, 정점 1에서 출발해 어떤 정점에 패스를 두고 그 정점으로 다시 돌아올 수 없게 되는지 판정한다.
난이도

보통10점 중 7점

유형
그래프, DFS
정답자
아직 제출이 없습니다

문제

Tikhon passed the entrance exams, and now studies at a research institute. However, he is afraid to walk around its campus. He is worried that he may forget his pass somewhere and permanently lose it, and then he will not be able to attend classes.

The campus of Tikhon's institute is a directed graph. Some of the edges of this graph can only be traversed with a pass.

To lose the pass permanently, Tikhon would need to start at his dormitory, which is located at the first vertex, then walk along zero or more edges of the campus, then leave his pass in some vertex, continue walking around the campus, and then not be able to return to the vertex where he has left the pass.

Tikhon would like to know if he is worried for no reason: please help him find out if it is possible to permanently lose the pass in the campus.

입력

The first line contains two integers nn and mm (1≤n,m≤3⋅1051 \le n, m \le 3 \cdot 10^5): the number of vertices and edges in the graph, respectively.

Each of the following mm lines contains three integers u_iu\_i, v_iv\_i, t_it\_i (1≤u_i,v_i≤n1 \le u\_i, v\_i \le n; 1≤t_i≤21 \le t\_i \le 2) describing the directed edges of the graph. Edge ii allows passage from u_iu\_i to v_iv\_i. If a pass is required to go through edge ii, then t_i=1t\_i = 1, otherwise t_i=2t\_i = 2.

The given graph may contain loops and multiple edges between the same vertices.

출력

Print a line with a single word (case-insensitive): "Yes" if Tikhon can permanently lose his pass, or "No" otherwise.

힌트

In the first example, Tikhon can first traverse the edge 1→21 \rightarrow 2 with a pass, then, leaving a pass at vertex 22, traverse the edge 2→32 \rightarrow 3. After that, he will not be able to return to vertex 22.

예제2

  1. 예제 1

    입력
    3 4
    1 2 1
    2 3 2
    3 2 1
    3 1 2
    
    예상 출력
    Yes
    
  2. 예제 2

    입력
    6 8
    1 2 1
    2 3 2
    3 2 2
    3 4 1
    4 1 2
    1 5 2
    5 4 2
    6 1 2
    
    예상 출력
    No