Peyman has promised Ali that he will help him in teaching Algorithm in the summer…
- User
Peyman has promised Ali that he will help him in teaching Algorithm in the summer course. The problem is that he lives in Mashhad and does not like to be away from his home for a long time. For this reason, Ali asked him only two topics n Teach the subject of the algorithm lesson.
Algorithm lesson n It has n topics, each of which may be a prerequisite for a number of other topics. The order of teaching topics in the summer course is not clear, but we know that the prerequisite relationships will be respected in this order. (If a subject is a prerequisite for another subject, it will definitely be taught earlier) We also know that the teaching of a subject takes exactly one day and according to the schedule, it is supposed to be during n n days, all the subjects of the algorithm lesson should be taught.
Peyman plans to fulfill his promise during a two-day trip to Tehran, teach two topics in the algorithm course, and return home. For this reason, he must choose two topics in such a way that in any order of the topics that are chosen for teaching the algorithm, those two topics will be in the program on two consecutive days. Also, Peyman does not like the topics he teaches to be unrelated and wants to choose those two topics in such a way that one of them is a prerequisite for the other. In what ways can he choose two topics with his desired conditions?
input In the first line of the input two natural n n and m m indicates the number of topics in the algorithm course and the number of prerequisite relationships between the topics. in m m next input line, two natural numbers in each line v i v i and u i u i It has been stated that it indicates that the topic v i v i Subject prerequisites u i u i is It is guaranteed that each prerequisite relationship between two topics appears at most once in the input. It is guaranteed that there is an order for teaching topics in which all prerequisite relationships are met. 1 ≤ n , m ≤ 500
000 1 ≤ n, m ≤ 500 000 1 ≤ m ≤ ( n 2 ) 1≤m≤( 2 n ) 1 ≤ v i , u i ≤ n , v i ≠ u i 1≤v i ,u i ≤n,v i
=u i
output In the first line of the output k Print the k number of ways the contract can choose two topics with the given conditions. in k k next line output in each line two numbers a i a i and b i b i Print so that the topic a i a i Prerequisite of the subject b i b i and these two topics are an arbitrary method of agreement. The output order should be such that for each i i and J j that i < J i<j, a i < a J a i <a J or if a i
a J a i =a J , b i < b J b i b J .
Sub-issues Constraint score subproblem 1 9 n ≤ 9 n≤9 2 21 n , m ≤ 1
000 n, m ≤ 1000 3 17 n ≤ 1
000 n≤1 000 4 53 without additional restrictions Example Sample entry 1 4 4 1 2 1 3 2 4 3 4 Plain text Sample output 1 0 Plain text Sample input 2 6 5 1 2 2 3 3 4 3 5 5 6 Plain text Sample output 2 2 1 2 2 3 Plain text Sample entry 3 4 6 4 3 4 2 4 1 3 2 3 1 2 1 Plain text Sample output 3 3 2 1 3 2 4 3 c++
- Assistant