fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define pb push_back
  5. #define fi first
  6. #define se second
  7. #define mp make_pair
  8. #define all(cont) cont.begin(), cont.end()
  9.  
  10. int main()
  11. {
  12. ios::sync_with_stdio(0);
  13. cin.tie(0);
  14. #ifndef ONLINE_JUDGE
  15. freopen("input.txt", "r", stdin);
  16. freopen("output.txt", "w", stdout);
  17. #endif
  18. ll tt = 1;
  19. ll N;
  20. string str;
  21. while(getline(cin,str))
  22. {
  23. N = stoll(str);
  24. ll M;
  25. string val,a,b;
  26. vector<string>arr(N);
  27. unordered_map<string,bool>visited;
  28. for(ll i=0;i<N;i++)
  29. {
  30.  
  31. cin>>val;
  32. arr[i] = val;
  33. visited[val] = false;
  34. }
  35. unordered_map<string,ll>indegree;
  36. unordered_map<string,vector<string>>adj;
  37. cin>>M;
  38. for(ll i=0;i<M;i++)
  39. {
  40. cin>>a>>b;
  41. adj[a].pb(b);
  42. indegree[b]++;
  43. }
  44. string ans;
  45. while(true)
  46. {
  47. bool flag = false;
  48. for(int i=0;i<N;i++)
  49. {
  50. if(indegree[arr[i]]==0 && !visited[arr[i]])
  51. {
  52. ans += arr[i]+" ";
  53. for(auto it:adj[arr[i]])
  54. indegree[it]--;
  55. flag = true;
  56. visited[arr[i]] = true;
  57. }
  58. }
  59. if(flag==false)
  60. break;
  61. }
  62. cout<<"Case #"<<tt<<": Vivek should drink beverages in this order: "<<ans<<endl;
  63. cout<<endl;
  64. tt++;
  65. getline(cin,str);
  66. getline(cin,str);
  67. }
  68.  
  69. return 0;
  70.  
  71.  
  72. }
  73.  
Success #stdin #stdout 0s 4296KB
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

5
A
B
C
D
E
5
C A
D A
B D
E B
E C
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 

Case #3: Vivek should drink beverages in this order: E B C D A