TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description

John and Mary attend a single party. There are n, where 2 ≤ n ≤ 1000, people in the party. John would like to tell a joke to Mary. However, due to some unexpected reason, he cannot tell his joke to Mary by himself. Therefore, he needs somebodies’ help to forward the joke. In this party, Participant A can forward a heard message to Participant B if A is B’s friend and elder than B. In particular, we use a forwarding-pair “A B” to mean that A is B’s friend and elder than B. Given the forwarding-pairs among all the participants, your goal is to compute the number of different ways that John can forward his joke to Mary (note: two ways are different if for the participants involved in both ways, there is at least one participant who is involved in only one way; moreover, k(≥ 2) ways are said to be different if any two ways from the k ways are different). In addition, if no such a way exists, the answer is 0.

For convenience, we use nature numbers 1, 2, . . . , n to present n peoples, in which 1 represents John and n represents Mary. In addition, an input pair i j is a forwarding-pair. Given the number n and a set of forwarding-pairs, your task is to write a computer program to compute the desired value.

Input Format

The input consists of a number of test cases. The first line contains a positive integer, n and k, where 2 ≤ n ≤ 1000 is the number of peoples in the party and k is the number of forwarding-pairs. The next 0 ≤ k ≤ 100000 lines contain k pairs in which each line is represented by two numbers separated by a single space. Finally, a 0 at the (k + 2)th line indicates the end of the first test case.

The next test case starts after the previous ending symbol 0. A -1 indicates the end of the whole inputs.

Output Format

The output contains one line for each test case. Each line contains an integer, which is the number of different ways that John can forward his joke to Mary.

Sample Input 1

7 7 
1 2 
2 3 
3 4 
4 5 
5 7 
3 6 
6 4 
0 
2 1 
1 2 
-1 

Sample Output 1

2
0

Hints

Problem Source

Migrated from old NTUJ.

NCPC2007

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 5000 65536 200