fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long int
  4. #define pb push_back
  5. #define translow transform(s.begin(),s.end(),s.begin(),::tolower);
  6. #define transup transform(s.begin(),s.end(),s.begin(),::toupper);
  7. #define endl "\n"
  8. #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  9. ll mod=1e9+7;
  10. vector<ll>adj[100000];
  11. vector<ll>ans,parent;
  12. map<string,ll>id;
  13. map<ll,string>id2;
  14. bool visited[100000];
  15.  
  16.  
  17.  
  18. void dfs(ll source)
  19. {
  20. visited[source]=true;
  21. for(auto it:adj[source])
  22. {
  23. if(visited[it]==false)
  24. dfs(it);
  25. }
  26.  
  27. ans.pb(source);
  28. }
  29.  
  30.  
  31.  
  32. int main() {
  33. fast
  34. #ifndef ONLINE_JUDGE
  35. freopen("input.txt","r",stdin);
  36. freopen("output.txt","w",stdout);
  37. #endif
  38.  
  39. ll n,xx=1;
  40. while(cin>>n)
  41. {
  42. memset(visited,false,sizeof(visited));
  43.  
  44. for(ll i=0;i<n;i++)
  45. {
  46. string s;
  47. cin>>s;
  48. id[s]=i+1;
  49. id2[i+1]=s;
  50. }
  51. ll m;
  52. cin>>m;
  53. for(ll i=0;i<m;i++)
  54. {
  55. string a,b;
  56. cin>>a>>b;
  57. adj[id[b]].pb(id[a]); //directed reverse graph
  58. //add(id[a],id[b]);
  59. }
  60.  
  61. for(ll i=1;i<=n;i++)
  62. {
  63. if(visited[i]==false)
  64. {
  65. dfs(i);
  66. }
  67. }
  68.  
  69. cout<<"Case #"<<xx<<": Vivek should drink beverages in this order: ";
  70.  
  71. for(ll i=0;i<ans.size()-1;i++)
  72. cout<<id2[ans[i]]<<" ";
  73. cout<<id2[ans[ans.size()-1]]<<"."<<endl;
  74. xx+=1;
  75. ans.clear();
  76. }
  77.  
  78.  
  79. return 0;
  80. }
Success #stdin #stdout 0s 6128KB
stdin
8
a
b
c
d
e
f
s
t
6
a e
a b
c d
e f
s t
a t

8
s
a
b
e
f
c
d
t
6
a e
a b
c d
e f
s t
a t
stdout
Case #1: Vivek should drink beverages in this order: a b c d e f s t.
Case #2: Vivek should drink beverages in this order: s a b e f c d t.