The unclear specifications on the input format and size lead to the undeserving
Difficulties to solve this problem. after Numerous frustrating res and was, I was forced to switch to C ++. i'm still not sure about the judge's data format, but I believe its size is smaller 5000.
The follow code uses a simple BFS on the coauthor graph. the parsing of paper database is relatively simple but tricky. it cocould be surely much simpler if the judge's data format strictly conforming to the specification.
Code:
- /*************************************** **********************************
- * Copyright (c) 2008 by liukaipeng *
- * Liukaipeng at gmail dot com *
- **************************************** *********************************/
- /* @ Judge_id 00000 10044 C ++ "Erdos number "*/
- # Include <iostream>
- # Include <fstream>
- # Include <string>
- # Include <vector>
- # Include <map>
- # Include <queue>
- # Include <limits>
- Using namespace STD;
- Void get_authors (vector <string> const & papers,
- Vector <vector <size_t> & authors,
- Map <string, size_t> & namelist)
- {
- For (INT I = 0; I <papers. Size (); ++ I ){
- String const & P = papers [I];
- For (int fb, Fe = 0, LB, Le = 0; P [Fe]! = ':' & P [le]! = ':';){
- Fb = P. find_first_not_of (",", le );
- Fe = P. find_first_of (",:", FB );
- Lb = P. find_first_not_of (",", Fe );
- Le = P. find_first_of (",:", LB );
- If (Fb = string: NPOs | Fe = string: NPOs |
- Lb = string: NPOs | Le = string: NPOs ){
- Break;
- }
- String name = P. substr (FB, Fe-FB) + "," + P. substr (LB, le-lb );
- Map <string, size_t>: iterator it = namelist. Find (name );
- If (IT = namelist. End ()){
- It = namelist. insert (it, make_pair (name, namelist. Size ()));
- }
- Authors [I]. push_back (IT-> second );
- }
- }
- }
- Void get_coauthors (vector <size_t> const & authors,
- Vector <vector <size_t> & coauthors)
- {
- For (INT I = 0; I <authors. Size (); ++ I ){
- Vector <size_t> const & A = authors [I];
- If (A. Empty ()){
- Continue;
- }
- For (INT a1 = 0; A1 <A. Size ()-1; ++ A1 ){
- For (INT a2 = A1 + 1; a2 <A. Size (); ++ A2 ){
- Coauthors [A [a1]. push_back (A [a2]);
- Coauthors [A [a2]. push_back (A [a1]);
- }
- }
- }
- }
- Void get_erdos_numbers (vector <size_t> const & coauthors,
- Vector <int> & erdos_numbers, size_t S)
- {
- Vector <bool> visited (coauthors. Size ());
- Queue <pair <size_t, int> q;
- Size_t v = s;
- Int D = 0;
- Q. Push (make_pair (v, d ));
- Visited [v] = true;
- While (! Q. Empty ()){
- S = Q. Front (). First;
- D = Q. Front (). Second;
- Q. Pop ();
- Erdos_numbers [s] = D;
- For (INT I = 0; I <coauthors [s]. Size (); ++ I ){
- V = coauthors [s] [I];
- If (! Visited [v]) {
- Q. Push (make_pair (v, D + 1 ));
- Visited [v] = true;
- }
- }
- }
- }
- Void put_erdos_numbers (vector <string> const & names,
- Map <string, size_t> const & namelist,
- Vector <int> const & erdos_numbers)
- {
- For (INT I = 0; I <names. Size (); ++ I ){
- If (! Names [I]. Empty ()){
- Map <string, size_t>: const_iterator it = namelist. Find (Names [I]);
- If (it! = Namelist. End () & erdos_numbers [it-> second]! =-1 ){
- Cout <Names [I] <"" <erdos_numbers [it-> second] <"/N ";
- } Else {
- Cout <Names [I] <"infinity/N ";
- }
- } Else {
- Cout <"/N ";
- }
- }
- }
- Int main (INT argc, char * argv [])
- {
- # Ifndef online_judge
- String name = argv [0];
- Filebuf infb, outfb;
- Cin. rdbuf (infb. Open (name + ". In"). c_str (), ios_base: In ));
- Cout. rdbuf (outfb. Open (name + ". Out"). c_str (), ios_base: Out ));
- # Endif
- Int scenarios;
- Cin> scenarios;
- For (int s = 1; S <= scenarios; ++ s ){
- Int P, N;
- Cin> P> N;
- Cin. Ignore (numeric_limits <streamsize>: max (), '/N ');
- Vector <string> papers (P );
- For (INT I = 0; I <p; ++ I ){
- Getline (CIN, papers [I]);
- }
- Map <string, size_t> namelist;/* map author name to Author ID */
- Vector <vector <size_t> authors (P );
- Get_authors (papers, authors, namelist );
- Vector <vector <size_t> coauthors (namelist. Size ());
- Get_coauthors (authors, coauthors );
- Vector <int> erdos_numbers (namelist. Size (),-1 );
- Map <string, size_t>: const_iterator it = namelist. Find ("Erdos, P .");
- Get_erdos_numbers (coauthors, erdos_numbers, IT-> second );
- Vector <string> names (N );
- For (INT I = 0; I <n; ++ I ){
- Getline (CIN, Names [I]);
- }
- Cout <"scenario" <S <"/N ";
- Put_erdos_numbers (names, namelist, erdos_numbers );
- }
- Return 0;
- }