A directed multigraph alternates send and receive rounds from an initial 1-bit packet; count the starting switches whose bit makes some buffer grow without bound.
Medium7GraphDFSSimulationImplementationNo attempts yetTime limit2sMemory limit512 MBYou are an engineer at Cafebazaar and your work is to analyze networks and how they behave. Your colleagues designed a network of n switches with m directed links that connect pairs of these switches. Each switch has one buffer where it stores data, and it has two modes, sending mode and receiving mode. A switch in sending mode sends the data stored in its buffer to every one of its outgoing links at the same time, and clears its buffer at the end. A switch in receiving mode concatenates the data from all of its incoming links and stores the result in its buffer, so the length of the data in its buffer then equals the sum of the lengths of the data on the incoming links.
At time t=0 every switch is in sending mode with an empty buffer, except switch i, whose buffer holds a package of 1 bit. Every switch changes its mode after each second, so at time t=1 all switches turn to receiving mode, at time t=2 they turn to sending mode, and so on. Switch i is called explosive if the maximum length of the data stored in the buffers of the switches is not bounded as t goes to infinity.
Compute the number of explosive switches in the network.
The input holds several test cases. The first line of each test case has two space separated integers n and m, where n is the number of switches and m is the number of directed links (1≤n,m≤50000). Each of the next m lines has two space separated integers u and v, meaning a directed link from switch u to switch v (1≤u,v≤n, u=v). The same pair (u,v) may appear several times, and then it counts as that many separate links.
The input ends with a line containing 0 0, which you must not process.
For each test case, print one line with the number of explosive switches.