10044-Erdos number Time limit exceeded

Source: Internet
Author: User

The following is my code, but it is time limit exceeded after passing the ultraviolet A test. I don't know why, and I have tested several pieces of data that are correct.


[Cpp]
# Include <iostream>
# Include <string>
# Include <vector>
# Include <map>
# Include <stdio. h>
# Include <algorithm>
 
Using namespace std;
 
String Erdos = "Erdos, P .";
String Scenario = "Scenario ";
String Infinity = "infinity ";
Map <string, int> Scientists;
 
Void loopFind (map <int, vector <string> map_name, vector <string> parters, vector <int> lines, int value ){
If (parters. size () <= 0) return;
 
For (int I = 0; I <parters. size (); ++ I ){
Vector <string> parter1;
Vector <int> lines1;
String tmp = parters. at (I );
For (map <int, vector <string >:: iterator it = map_name.begin (); it! = Map_name.end (); ++ it ){
Vector <string> tmpNames = it-> second;
Vector <int>: iterator tmpIndex = find (lines. begin (), lines. end (), it-> first );
Vector <string >:: iterator it1 = find (tmpNames. begin (), tmpNames. end (), tmp );
If (tmpIndex! = Lines. end () break;
Else if (it1! = TmpNames. end ()){
For (int j = 0; j <tmpNames. size (); ++ j ){
String tmp1 = tmpNames. at (j );
If (tmp1! = Tmp ){
Scientists [tmp1] = value;
Parter1.push _ back (tmp1 );
}
}
Lines1.push _ back (it-> first );
}
}
LoopFind (map_name, parter1, lines1, value + 1 );
}
}
 
Void findScientists (map <int, vector <string> map_name ){
Scientists [Erdos] = 0;
Vector <string> parters;
Vector <int> lines;

For (map <int, vector <string >:: iterator it = map_name.begin (); it! = Map_name.end (); ++ it ){
Vector <string> names = it-> second;
Vector <string >:: iterator tmp = find (names. begin (), names. end (), Erdos );
If (tmp! = Names. end ()){
For (int I = 0; I <names. size (); ++ I ){
String tmp1 = names. at (I );
If (tmp1! = Erdos ){
Scientists [tmp1] = 1;
Parters. push_back (tmp1 );
}
}
Lines. push_back (it-> first );
}
}
LoopFind (map_name, parters, lines, 2 );
}
 
Void getErdosNumber (vector <string> paper, vector <string> names ){
Int index = 1;
Map <int, vector <string> map_name;

For (vector <string >:: iterator it = paper. begin (); it! = Paper. end (); ++ it ){
String tmpString = * it;
Vector <string> tmpSci;
Int begin = 0;
Int end = 0;
If (tmpString. length () <1) continue;
While (end <tmpString. length ()){
If (tmpString [end] = '.' & tmpString [end + 1] = ',' & tmpString [end + 2] = ''){
String tmpName = tmpString. substr (begin, end-begin + 1 );
TmpSci. push_back (tmpName );
Begin = end + 3;
End = begin;
}
Else if (tmpString [end] = ':'){
String tmpName = tmpString. substr (begin, end-begin );
TmpSci. push_back (tmpName );
Break;
}
Else ++ end;
 
}
 
Map_name [index] = tmpSci;
++ Index;
}
 
FindScientists (map_name );
}
 
Int main (){
Int n = 0;
Cin> n;
 
For (int I = 1; I <= n; ++ I ){
Int P = 0;
Int N = 0;
Cin> P> N;
If (P <1 | N <1) continue;
 
Getchar ();
Vector <string> paper;
For (int x = 0; x <P; ++ x ){
String tmpPaper;
Getline (cin, tmpPaper );
Paper. push_back (tmpPaper );
}
 
Vector <string> names;
For (int y = 0; y <N; ++ y ){
String tmpName;
Getline (cin, tmpName );
Names. push_back (tmpName );
}
 
Scientists. clear ();
GetErdosNumber (paper, names );
 
Cout <Scenario <I <endl;
Map <string, int >:: iterator it;

For (vector <string >:: iterator ite = names. begin (); ite! = Names. end (); ++ ite ){
It = Scientists. find (* ite );
If (it! = Scientists. end ()){
Cout <it-> first <"" <it-> second <endl;
}
Else {
Cout <* ite <"<Infinity <endl;
}
}
}
Return 0;
}

# Include <iostream>
# Include <string>
# Include <vector>
# Include <map>
# Include <stdio. h>
# Include <algorithm>

Using namespace std;

String Erdos = "Erdos, P .";
String Scenario = "Scenario ";
String Infinity = "infinity ";
Map <string, int> Scientists;

Void loopFind (map <int, vector <string> map_name, vector <string> parters, vector <int> lines, int value ){
If (parters. size () <= 0) return;

For (int I = 0; I <parters. size (); ++ I ){
Vector <string> parter1;
Vector <int> lines1;
String tmp = parters. at (I );
For (map <int, vector <string >:: iterator it = map_name.begin (); it! = Map_name.end (); ++ it ){
Vector <string> tmpNames = it-> second;
Vector <int>: iterator tmpIndex = find (lines. begin (), lines. end (), it-> first );
Vector <string >:: iterator it1 = find (tmpNames. begin (), tmpNames. end (), tmp );
If (tmpIndex! = Lines. end () break;
Else if (it1! = TmpNames. end ()){
For (int j = 0; j <tmpNames. size (); ++ j ){
String tmp1 = tmpNames. at (j );
If (tmp1! = Tmp ){
Scientists [tmp1] = value;
Parter1.push _ back (tmp1 );
}
}
Lines1.push _ back (it-> first );
}
}
LoopFind (map_name, parter1, lines1, value + 1 );
}
}

Void findScientists (map <int, vector <string> map_name ){
Scientists [Erdos] = 0;
Vector <string> parters;
Vector <int> lines;
 
For (map <int, vector <string >:: iterator it = map_name.begin (); it! = Map_name.end (); ++ it ){
Vector <string> names = it-> second;
Vector <string >:: iterator tmp = find (names. begin (), names. end (), Erdos );
If (tmp! = Names. end ()){
For (int I = 0; I <names. size (); ++ I ){
String tmp1 = names. at (I );
If (tmp1! = Erdos ){
Scientists [tmp1] = 1;
Parters. push_back (tmp1 );
}
}
Lines. push_back (it-> first );
}
}
LoopFind (map_name, parters, lines, 2 );
}

Void getErdosNumber (vector <string> paper, vector <string> names ){
Int index = 1;
Map <int, vector <string> map_name;
 
For (vector <string >:: iterator it = paper. begin (); it! = Paper. end (); ++ it ){
String tmpString = * it;
Vector <string> tmpSci;
Int begin = 0;
Int end = 0;
If (tmpString. length () <1) continue;
While (end <tmpString. length ()){
If (tmpString [end] = '.' & tmpString [end + 1] = ',' & tmpString [end + 2] = ''){
String tmpName = tmpString. substr (begin, end-begin + 1 );
TmpSci. push_back (tmpName );
Begin = end + 3;
End = begin;
}
Else if (tmpString [end] = ':'){
String tmpName = tmpString. substr (begin, end-begin );
TmpSci. push_back (tmpName );
Break;
}
Else ++ end;

}

Map_name [index] = tmpSci;
++ Index;
}

FindScientists (map_name );
}

Int main (){
Int n = 0;
Cin> n;

For (int I = 1; I <= n; ++ I ){
Int P = 0;
Int N = 0;
Cin> P> N;
If (P <1 | N <1) continue;

Getchar ();
Vector <string> paper;
For (int x = 0; x <P; ++ x ){
String tmpPaper;
Getline (cin, tmpPaper );
Paper. push_back (tmpPaper );
}

Vector <string> names;
For (int y = 0; y <N; ++ y ){
String tmpName;
Getline (cin, tmpName );
Names. push_back (tmpName );
}

Scientists. clear ();
GetErdosNumber (paper, names );

Cout <Scenario <I <endl;
Map <string, int >:: iterator it;

For (vector <string >:: iterator ite = names. begin (); ite! = Names. end (); ++ ite ){
It = Scientists. find (* ite );
If (it! = Scientists. end ()){
Cout <it-> first <"" <it-> second <endl;
}
Else {
Cout <* ite <"<Infinity <endl;
}
}
}
Return 0;
}
If someone knows why it is time limit exceeded, please let me know. Thank you.

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.