Saleh127

UVA 11060

Mar 10th, 2021 (edited)
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4.  
  5. priority_queue<ll>q;
  6. map<string,ll>mp;
  7. map<ll,string>mp1;
  8. vector<ll>g[100000];
  9. ll a[100000];
  10. vector<string>ans;
  11. bool vv[100000];
  12.  
  13. void solve()
  14. {
  15. while(!q.empty())
  16. {
  17. ll u=q.top();
  18.  
  19. u*=-1;
  20.  
  21. q.pop();
  22.  
  23.  
  24.  
  25. string hh=mp1[u];
  26.  
  27. ans.push_back(hh);
  28.  
  29. for(ll i=0; i<g[u].size(); i++)
  30. {
  31. ll dd=g[u][i];
  32. a[dd]--;
  33. if(a[dd]==0)
  34. {
  35. q.push(-dd);
  36. }
  37. }
  38.  
  39. }
  40. }
  41.  
  42.  
  43. int main()
  44. {
  45. ios_base::sync_with_stdio(0);
  46. cin.tie(0);
  47. cout.tie(0);
  48.  
  49. ll n,m,cs=1;
  50.  
  51. while(cin>>n)
  52. {
  53.  
  54.  
  55. ll i,j=0,k=0;
  56.  
  57. for(i=0; i<50000; i++)
  58. {
  59. a[i]=0;
  60. vv[i]=0;
  61. g[i].clear();
  62. }
  63.  
  64.  
  65. ans.clear();
  66.  
  67. for(i=1; i<=n; i++)
  68. {
  69. string uu;
  70. cin>>uu;
  71.  
  72. mp[uu]=i;
  73. mp1[i]=uu;
  74. }
  75.  
  76. cin>>m;
  77.  
  78. for(i=0; i<m; i++)
  79. {
  80. string u,v;
  81.  
  82. cin>>u>>v;
  83.  
  84. ll x,y;
  85.  
  86. x=mp[u];
  87. y=mp[v];
  88.  
  89. g[x].push_back(y);
  90. a[y]++;
  91.  
  92. }
  93.  
  94. for(i=1; i<=n; i++)
  95. {
  96. if(a[i]==0)
  97. {
  98. q.push(-i);
  99. }
  100. }
  101.  
  102. solve();
  103.  
  104. cout<<"Case #"<<cs++<<": Dilbert should drink beverages in this order: ";
  105.  
  106. for(i=0;i<ans.size()-1;i++)
  107. {
  108. cout<<ans[i]<<" ";
  109. }
  110. cout<<ans[i]<<"."<<endl<<endl;
  111. }
  112.  
  113.  
  114. return 0;
  115. }
  116.  
  117.  
  118. ///priority queue with min to max
  119.  
  120. /*
  121.  
  122. #include <bits/stdc++.h>
  123. using namespace std;
  124. #define ll long long
  125.  
  126. priority_queue<ll,vector<ll>,greater<ll>>q;
  127. map<string,ll>mp;
  128. map<ll,string>mp1;
  129. vector<ll>g[100000];
  130. ll a[100000];
  131. vector<string>ans;
  132. bool vv[100000];
  133.  
  134. void solve()
  135. {
  136. while(!q.empty())
  137. {
  138. ll u=q.top();
  139.  
  140. q.pop();
  141.  
  142. if(vv[u]) continue;
  143.  
  144. string hh=mp1[u];
  145.  
  146. ans.push_back(hh);
  147.  
  148. vv[u]=1;
  149. for(ll i=0; i<g[u].size(); i++)
  150. {
  151. a[g[u][i]]--;
  152. if(a[g[u][i]]==0)
  153. {
  154. q.push(g[u][i]);
  155. }
  156. }
  157.  
  158. }
  159. }
  160.  
  161.  
  162. int main()
  163. {
  164. ios_base::sync_with_stdio(0);
  165. cin.tie(0);
  166. cout.tie(0);
  167.  
  168. ll n,m,cs=1;
  169.  
  170. while(cin>>n)
  171. {
  172.  
  173.  
  174. ll i,j=0,k=0;
  175.  
  176. for(i=0; i<50000; i++)
  177. {
  178. a[i]=0;
  179. vv[i]=0;
  180. g[i].clear();
  181. }
  182.  
  183.  
  184. ans.clear();
  185.  
  186. for(i=1; i<=n; i++)
  187. {
  188. string uu;
  189. cin>>uu;
  190.  
  191. mp[uu]=i;
  192. mp1[i]=uu;
  193. }
  194.  
  195. cin>>m;
  196.  
  197. for(i=0; i<m; i++)
  198. {
  199. string u,v;
  200.  
  201. cin>>u>>v;
  202.  
  203. ll x,y;
  204.  
  205. x=mp[u];
  206. y=mp[v];
  207.  
  208. g[x].push_back(y);
  209. a[y]++;
  210.  
  211. }
  212.  
  213. for(i=1; i<=n; i++)
  214. {
  215. if(a[i]==0)
  216. {
  217. q.push(i);
  218. }
  219. }
  220.  
  221. solve();
  222.  
  223. cout<<"Case #"<<cs++<<": Dilbert should drink beverages in this order: ";
  224.  
  225. for(i=0;i<ans.size()-1;i++)
  226. {
  227. cout<<ans[i]<<" ";
  228. }
  229. cout<<ans[i]<<"."<<endl<<endl;
  230. }
  231.  
  232.  
  233. return 0;
  234. }
  235. */
  236.  
Add Comment
Please, Sign In to add comment