Ultraviolet A Problem Solution: 10044-Erdos number

Source: Internet
Author: User

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:

  1. /*************************************** **********************************
  2. * Copyright (c) 2008 by liukaipeng *
  3. * Liukaipeng at gmail dot com *
  4. **************************************** *********************************/
  5. /* @ Judge_id 00000 10044 C ++ "Erdos number "*/
  6. # Include <iostream>
  7. # Include <fstream>
  8. # Include <string>
  9. # Include <vector>
  10. # Include <map>
  11. # Include <queue>
  12. # Include <limits>
  13. Using namespace STD;
  14. Void get_authors (vector <string> const & papers,
  15. Vector <vector <size_t> & authors,
  16. Map <string, size_t> & namelist)
  17. {
  18. For (INT I = 0; I <papers. Size (); ++ I ){
  19. String const & P = papers [I];
  20. For (int fb, Fe = 0, LB, Le = 0; P [Fe]! = ':' & P [le]! = ':';){
  21. Fb = P. find_first_not_of (",", le );
  22. Fe = P. find_first_of (",:", FB );
  23. Lb = P. find_first_not_of (",", Fe );
  24. Le = P. find_first_of (",:", LB );
  25. If (Fb = string: NPOs | Fe = string: NPOs |
  26. Lb = string: NPOs | Le = string: NPOs ){
  27. Break;
  28. }
  29. String name = P. substr (FB, Fe-FB) + "," + P. substr (LB, le-lb );
  30. Map <string, size_t>: iterator it = namelist. Find (name );
  31. If (IT = namelist. End ()){
  32. It = namelist. insert (it, make_pair (name, namelist. Size ()));
  33. }
  34. Authors [I]. push_back (IT-> second );
  35. }
  36. }
  37. }
  38. Void get_coauthors (vector <size_t> const & authors,
  39. Vector <vector <size_t> & coauthors)
  40. {
  41. For (INT I = 0; I <authors. Size (); ++ I ){
  42. Vector <size_t> const & A = authors [I];
  43. If (A. Empty ()){
  44. Continue;
  45. }
  46. For (INT a1 = 0; A1 <A. Size ()-1; ++ A1 ){
  47. For (INT a2 = A1 + 1; a2 <A. Size (); ++ A2 ){
  48. Coauthors [A [a1]. push_back (A [a2]);
  49. Coauthors [A [a2]. push_back (A [a1]);
  50. }
  51. }
  52. }
  53. }
  54. Void get_erdos_numbers (vector <size_t> const & coauthors,
  55. Vector <int> & erdos_numbers, size_t S)
  56. {
  57. Vector <bool> visited (coauthors. Size ());
  58. Queue <pair <size_t, int> q;
  59. Size_t v = s;
  60. Int D = 0;
  61. Q. Push (make_pair (v, d ));
  62. Visited [v] = true;
  63. While (! Q. Empty ()){
  64. S = Q. Front (). First;
  65. D = Q. Front (). Second;
  66. Q. Pop ();
  67. Erdos_numbers [s] = D;
  68. For (INT I = 0; I <coauthors [s]. Size (); ++ I ){
  69. V = coauthors [s] [I];
  70. If (! Visited [v]) {
  71. Q. Push (make_pair (v, D + 1 ));
  72. Visited [v] = true;
  73. }
  74. }
  75. }
  76. }
  77. Void put_erdos_numbers (vector <string> const & names,
  78. Map <string, size_t> const & namelist,
  79. Vector <int> const & erdos_numbers)
  80. {
  81. For (INT I = 0; I <names. Size (); ++ I ){
  82. If (! Names [I]. Empty ()){
  83. Map <string, size_t>: const_iterator it = namelist. Find (Names [I]);
  84. If (it! = Namelist. End () & erdos_numbers [it-> second]! =-1 ){
  85. Cout <Names [I] <"" <erdos_numbers [it-> second] <"/N ";
  86. } Else {
  87. Cout <Names [I] <"infinity/N ";
  88. }
  89. } Else {
  90. Cout <"/N ";
  91. }
  92. }
  93. }
  94. Int main (INT argc, char * argv [])
  95. {
  96. # Ifndef online_judge
  97. String name = argv [0];
  98. Filebuf infb, outfb;
  99. Cin. rdbuf (infb. Open (name + ". In"). c_str (), ios_base: In ));
  100. Cout. rdbuf (outfb. Open (name + ". Out"). c_str (), ios_base: Out ));
  101. # Endif
  102. Int scenarios;
  103. Cin> scenarios;
  104. For (int s = 1; S <= scenarios; ++ s ){
  105. Int P, N;
  106. Cin> P> N;
  107. Cin. Ignore (numeric_limits <streamsize>: max (), '/N ');
  108. Vector <string> papers (P );
  109. For (INT I = 0; I <p; ++ I ){
  110. Getline (CIN, papers [I]);
  111. }
  112. Map <string, size_t> namelist;/* map author name to Author ID */
  113. Vector <vector <size_t> authors (P );
  114. Get_authors (papers, authors, namelist );
  115. Vector <vector <size_t> coauthors (namelist. Size ());
  116. Get_coauthors (authors, coauthors );
  117. Vector <int> erdos_numbers (namelist. Size (),-1 );
  118. Map <string, size_t>: const_iterator it = namelist. Find ("Erdos, P .");
  119. Get_erdos_numbers (coauthors, erdos_numbers, IT-> second );
  120. Vector <string> names (N );
  121. For (INT I = 0; I <n; ++ I ){
  122. Getline (CIN, Names [I]);
  123. }
  124. Cout <"scenario" <S <"/N ";
  125. Put_erdos_numbers (names, namelist, erdos_numbers );
  126. }
  127. Return 0;
  128. }

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.