ACM-Baby game of lala (MLE, graph, breadth first search)

Source: Internet
Author: User

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 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.