Baby game of lala
Time Limit:1000MS Memory Limit:65536K
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. Could 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 A). 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 6 1 2 2 3 1 3 3 4 3 5 4 5 0 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
/* ------------------------------------------------------------------------------------- 爆内存了,明天再想想其它解决方法 ------------------------------------------------------------------------------------- */ #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%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%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 ; }