Baby game of lala
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 200 Accepted: 61
Description
Do you know teletubbies? They are four cute babies, who names: Dipsy, Po, Lala, and Tinky Winky. one day they are playing a baby game. they draw N points on the ground, and write some lines connect them. the points are number from 1 to N, they start at point
1, and make a step by turn. in each step, they randomly choose a line and walk alone it to another point. once a baby walk into the point N, he wins. lala always goes first. she wants to win the game as soon as possible, because she has to go home for supper.
Cocould you help her to find out the minimal possible steps for her to win?
Input
Input will first give two numbers: N and M, means the babies have drawn N points and M lines. followed by M lines, which consist 2 numbers A and B, means A is connected with B (and of course B is connected
With ). 0 <N <10000, 0 <M <100000. a, B <N. there will be several cases. while N = 0 and M = 0 will terminate the input, and you don't need to process this case.
Output
For each case, output the minimal possible steps for Lala to win.
Sample Input
5 61 22 31 33 43 54 50 0
Sample Output
2
Hint
Note that the crazy babies have drawn up to 10000 points-_-, what an awful job .... So if you don't have an efficient algorithm, don't waste your time on this problem.
Source
GDUT Programming Contest 2007 by ziliang
/* The memory is exploding. Think about other solutions tomorrow. */# include <stdio. h> # include <memory. h> int queue [10004]; int nSign [10004]; int nLen = 0; int a [10004] [10004]; int main (void) {long I = 0; long j = 0; long n = 0; long m = 0; long row = 0; long cow = 0; long nTotal = 0; int fFlag = 0; while (scanf ("% ld", & n, & m), n! = 0 & m! = 0) {memset (a, 0, sizeof (a); memset (queue, 0, sizeof (queue); memset (nSign, 0, sizeof (nSign )); nLen = 0; nTotal = 0; fFlag = 0; for (I = 0; I <m; ++ I) {scanf ("% d", & row, & cow); a [row] [cow] = 1 ;}for (I = 1; I <= n; ++ I) {if (a [1] [I]! = 0 & nSign [I] = 0) {queue [nLen ++] = I; nSign [I] = 1 ;}} while (nLen! = 0) {for (I = 0; I <nLen; ++ I) {if (queue [I] = n) {fFlag = 1; break ;}} if (1 = fFlag) {break;} else {row = queue [0]; nLen --; nTotal ++ ;}for (j = 0; j <nLen; ++ j) {queue [j] = queue [j + 1];} nLen = j; for (I = 1; I <= n; ++ I) {if (a [row] [I]! = 0 & nSign [I] = 0) {queue [nLen ++] = I; nSign [I] = 1 ;}}} printf ("% ld \ n", nTotal);} return 0 ;}