Friend chains

**Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)**

Total submission (s): 15 accepted submission (s): 10

Problem descriptionfor a group of people, there is an idea that everyone is equals to or less than 6 steps away from any other person in the group, by way of introduction. so that a chain of "a friend of a friend" can be made to connect any 2 persons and it contains no more than 7 persons.

For example, if XXX is YYY's friend and YYY is ZZZ's friend, But xxx is not ZZZ's friend, then there is a friend chain of length 2 between XXX and zzz. the length of a friend chain is one less than the number of persons in the chain.

Note that if XXX is YYY's friend, then YYY is XXX's friend. give the group of people and the friend relationship between them. you want to know the minimum value K, which for any two persons in the group, there is a friend chain connecting them and the chain's length is no more than K.

Inputthere are multiple cases.

For each case, there is an integer N (2 <=n <= 1000) which represents the number of people in the group.

Each of the next n lines contains a string which represents the name of one people. The string consists of alphabet letters and the length of it is no more than 10.

Then there is a number M (0 <= m <= 10000) which represents the number of friend relationships in the group.

Each of the next M lines contains two names which are separated by a space, and they are friends.

Input ends with n = 0.

Outputfor each case, print the minimum value K in one line.

If the value of K is infinite, then print-1 instead.

Sample input3 xxx yyy ZZZ 2 xxx yyy yyy zzz 0

Sample output2

Source2012 Asia Hangzhou regional contest is the shortest path between two pairs. Floyed, O (N ^ 3) obviously times out. Perform n BFS requests to find the shortest path. O (N ^ 2). The write speed is not fast and it is too risky ~~~~

// ========================================================== ========================================== // Name: hdu4460.cpp // Author: // Version: // Copyright: Your copyright notice // Description: Hello world in C ++, ANSI-style // ========================================================== ========================================== # Include <Iostream> # Include <Stdio. h> # Include < String . H> # Include <Algorithm> # Include <Map> # Include <Math. h> # Include <Queue> # Include <Vector> Using Namespace STD; Const Int Maxn = 1010 ; Const Int INF = 0x3f3f3f ; Int Dis [maxn] [maxn]; Bool Used [maxn]; vector < Int > VEC [maxn]; queue < Int > Que; Void DFS ( Int I) {memset (used, False , Sizeof (Used); DIS [I] [I] = 0 ; Used [I] = True ; Que. Push (I ); While (! Que. Empty ()){ Int T = Que. Front (); Que. Pop (); Int M = VEC [T]. Size (); For ( Int J = 0 ; J <m; j ++ ){ Int V = VEC [T] [J]; If (Used [v]) Continue ; DIS [I] [v] = Dis [I] [T] + 1 ; Que. Push (V); used [v] = True ; }}} Map < String , Int > MP; Int Main (){ String STR; String Str2; Int N, m; While (Scanf ( " % D " , & N) = 1 && N) {MP. Clear (); For (Int I = 0 ; I <n; I ++ ) {CIN > STR; MP [STR] = I ;} For ( Int I = 0 ; I <n; I ++ ) {Dis [I] [I] = 0 ; For ( Int J = I +1 ; J <n; j ++ ) Dis [I] [J] = Dis [J] [I] = INF;} scanf ( " % D " ,& M ); For ( Int I = 0 ; I <n; I ++ ) VEC [I]. Clear (); While (M -- ) {CIN >>Str >>>Str2; Int T1 = MP [STR]; Int T2 = MP [str2]; VEC [T1]. push_back (T2); VEC [T2]. push_back (T1 );} For ( Int I = 0 ; I <n; I ++ ) DFS (I ); Int Ans = 0 ; For (Int I = 0 ; I <n; I ++ ) For ( Int J = I + 1 ; J <n; j ++ ) Ans = Max (ANS, DIS [I] [J]); If (ANS = inf) ans =- 1 ; Printf ( " % D \ n " , ANS );} Return 0 ;}