G: DAG Trio / DAG Trio
This problem is the same setting as D: DAG Trio (Easy) except for the constraints.
Prologue
My little brother has been muttering "dag torio" recently.
Worried, I looked it up and found that in his class, $k$-DAG (a directed graph in which removing $k$ edges makes the number of connected components $k$, and all of them are DAG) is popular, and 3-DAG is called DAG trio.
To be respected by my brother, I decided to make a program that determines whether a given graph is DAG trio or not.
Problem Statement
Given a directed graph with $N$ vertices and $M$ edges.
Each vertex is numbered from $1$ to $N$.
Each directed edge is numbered from $1$ to $M$.
Directed edge $i$ goes from vertex $a_i$ to vertex $b_i$.
The graph is connected and simple (there is a path between any two vertices when ignoring the direction of edges, and there are no self-loops or multiple edges).
If the given graph is DAG trio, output "YES", otherwise output "NO".
Input
$N \ M$
$a_1 \ b_1$
$a_2 \ b_2$
$\vdots$
$a_M \ b_M$
Constraints
$3 \le N \le 500$
$\max(3, N−1) \le M \le 30000$
$1 \le a_i, b_i \le N$
The graph is connected when ignoring the direction of edges.
$a_i \neq b_i$ for all $i$.
$\{a_i, b_i\} \neq \{a_j, b_j\}$ for all $i \neq j$.
Output
Output "YES" or "NO" in a single line.
Sample
Sample Input 1
3 3
1 2
2 3
3 1
Sample Output 1
YES
Sample Input 2
6 7
1 2
2 3
4 3
4 5
5 6
6 4
3 6
Sample Output 2
YES
Sample Input 3
7 10
4 2
4 7
4 6
2 7
2 5
2 1
5 6
1 3
6 3
4 3
Sample Output 3
NO
Sample Input 4
4 4
1 2
3 2
4 3
2 4
Sample Output 4
YES
Sample Input 5
8 9
5 1
3 8
1 2
4 8
4 7
7 5
6 5
3 2
4 2
Sample Output 5
YES
