Under construction

Still being built. Things may change

Restaurant Tour

Time limit3sMemory limit1024 MB

Summary
Given a directed graph of N restaurants and M one-way roads, decide whether one walk can cover all restaurants, all roads, or both.
Level

Medium6 of 10

Topics
Graph, Topological sort, DFS
Solved
No attempts yet

Problem

Eunsu is going to travel to a city to tour its restaurants. The city has NN restaurants in total. The city has MM one-way roads that let you move from one restaurant to another, and each road has a souvenir shop halfway along it. A road may start and end at the same restaurant, and several roads may connect the same pair of restaurants. Eunsu starts at any restaurant, moves along roads from restaurant to restaurant, and ends at any restaurant. The start and end do not need to be the same. The city is far from Eunsu's village, so Eunsu wants to visit as many restaurants and shops as possible on one trip. If a trip visits every restaurant, or every souvenir shop, Eunsu gets a discount on the next trip. Eunsu wants to know whether such a trip is possible. Your task is to plan the trip.

Input

The first line contains NN and MM, separated by a space (1≤N,M≤500 0001 \leq N, M \leq 500\,000). Each of the next MM lines contains two integers uu and vv, separated by a space, which means there is a one-way road from restaurant uu to restaurant vv (1≤u,v≤N1 \leq u, v \leq N).

Output

Print three lines. The first line prints 1 if Eunsu can plan a trip that visits all NN restaurants, and 0 otherwise. The second line prints 1 if Eunsu can plan a trip that visits all MM souvenir shops, and 0 otherwise. The third line prints 1 if Eunsu can plan a trip that visits all NN restaurants and all MM souvenir shops, and 0 otherwise.

Examples2

  1. Example 1

    Input
    4 6
    1 2
    1 4
    2 3
    2 4
    3 1
    3 4
    
    Expected output
    1
    0
    0
    
  2. Example 2

    Input
    3 2
    1 3
    2 1
    
    Expected output
    1
    1
    1