• Source
    1. #include<stdio.h>
    2. #include<string.h>
    3. #include<vector>
    4. #include<map>
    5. #include<queue>
    6. #include<iostream>
    7. #define sz 3300
    8.  
    9. using namespace std;
    10.  
    11. map<string,int>mp;
    12.  
    13. map<int,string>mp1;
    14.  
    15. int par[sz];
    16.  
    17. bool color[sz];
    18.  
    19. vector<int>adj[sz];
    20.  
    21. int tag,flag;
    22.  
    23. string source,dest;
    24.  
    25. vector<int>vec;
    26.  
    27. void bfs(int s)
    28. {
    29. memset(color,false,sizeof(color));
    30.  
    31. queue<int>Q;
    32.  
    33. color[s]=1;
    34.  
    35. Q.push(s);
    36.  
    37. int a;
    38.  
    39. while (!Q.empty())
    40. {
    41. int u=Q.front();
    42. Q.pop();
    43.  
    44. for (int i=0; i<adj[u].size(); i++)
    45. {
    46. a=adj[u][i];
    47. if(color[a]==0)
    48. {
    49. color[a]=1;
    50. Q.push(a);
    51. par[a]=u;
    52. }
    53. }
    54. }
    55.  
    56. return ;
    57. }
    58.  
    59. void path(int v)
    60. {
    61. if (mp[source]==v)
    62. {
    63. vec.push_back(v);
    64. return;
    65. }
    66. else if (par[v]==0)
    67. {
    68. flag = 1;
    69. return;
    70. }
    71. else
    72. {
    73. path(par[v]);
    74. vec.push_back(v);
    75. }
    76. return ;
    77. }
    78.  
    79. int main()
    80. {
    81. int n,i,j,tc=0;
    82. string s,s1;
    83. while(scanf("%d",&n)!=EOF)
    84. {
    85. tag=0;
    86.  
    87. mp.clear();
    88. mp1.clear();
    89. vec.clear();
    90. memset(par,0,sizeof(par));
    91.  
    92. if(tc)
    93. {
    94. printf("\n");
    95. }
    96.  
    97. for(i=1; i<=n; i++)
    98. {
    99. cin>>s>>s1;
    100.  
    101. if(mp.find(s)==mp.end())
    102. {
    103. mp[s]=++tag;
    104. mp1[tag]=s;
    105. }
    106.  
    107. if(mp.find(s1)==mp.end())
    108. {
    109. mp[s1]=++tag;
    110. mp1[tag]=s1;
    111. }
    112.  
    113. adj[mp[s]].push_back(mp[s1]);
    114.  
    115. adj[mp[s1]].push_back(mp[s]);
    116. }
    117.  
    118. cin>>source>>dest;
    119. if(mp.find(source)==mp.end())
    120. {
    121. mp[source]=++tag;
    122. mp1[tag]=source;
    123. }
    124.  
    125. if(mp.find(dest)==mp.end())
    126. {
    127. mp[dest]=++tag;
    128. mp1[tag]=dest;
    129. }
    130.  
    131.  
    132. bfs(mp[source]);
    133.  
    134. flag=0;
    135.  
    136. path(mp[dest]);
    137.  
    138.  
    139. if(flag)
    140. {
    141. printf("No route\n");
    142.  
    143. }
    144. else
    145. {
    146. if(vec.size()==1)
    147. {
    148. cout<<mp1[vec[0]]<<" "<<mp1[vec[0]]<<"\n";
    149. }
    150. else
    151. {
    152. for(i=0; i<vec.size()-1; i++)
    153. {
    154. cout<<mp1[vec[i]]<<" "<<mp1[vec[i+1]]<<"\n";
    155. }
    156. }
    157. }
    158.  
    159. tc=1;
    160.  
    161. for(i=0;i<=tag;i++)
    162. {
    163. adj[i].clear();
    164. }
    165.  
    166. }
    167. return 0;
    168. }
    169.