A Noisy Class

View as PDF

Submit solution

Points: 7
Time limit: 0.5s
Memory limit: 128M

Problem type

One day Mr. Sidhu's class is excessively loud. Unfortunately, giving them an activity about tiles didn't really work.

He notices that when he tells a certain student to stop talking, they will immediately resume since another noisy student will distract them, but if he tells the noisy student to stop talking, there will be silence at last.

After many hours of careful observations, Mr. Sidhu has given you a list containing the connections between students and asks you if it is even possible for the class to be completely silent.

Given the size of his class (as seen in New Students), he can only tell students to stop talking individually. Note that connections are in one direction - the noisy students talk to the distracted students, but not the other way around.

Input Specification

The first line consists of N (2 \le N \le 10\,000), the number of students; each student is assigned a number from 1 to N.

The next line contains M (1 \le M \le 10\,000), the number of connections.

The next M lines contain two space-separated integers, representing the noisy student and the distracted student respectively. Given the sheer size, it is not guaranteed that the connections are distinct.

Output Specification

You are to output Y if it is possible and N if it is not.

Sample Input 1

1 2
2 3
2 4
4 3

Sample Output 1


Explanation for Sample Output 1

Mr. Sidhu can tell the students to stop talking in the following order: 1, 2, 4, 3

Sample Input 2

1 2
2 1

Sample Output 2


Explanation for Sample Output 2

After telling student 1 to quiet down student 2 will immediately distract him, and vice-versa. Since there is a cycle the class will never be quiet!


  • -3
    OneYearOld  commented on May 16, 2020, 9:50 p.m.

  • 9
    quantum  commented on April 29, 2015, 3:23 a.m.

    The test data contains at least one case where the same edge is listed twice.

    • -5
      Suraj_IOI  commented on May 18, 2019, 6:36 p.m. edit 2

      This comment is hidden due to too much negative feedback. Show it anyway.

    • 7
      Sentient  commented on April 29, 2015, 10:10 p.m.

      It was initially not stated whether the edges are all distinct; the problem statement has been updated to clarify that this is not guaranteed.