Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include<cstdio>
  2. #include<map>
  3. #include<vector>
  4. #include<string>
  5. #include<iostream>
  6. using namespace std;
  7.  
  8. void dfs(int a);
  9.  
  10. vector<int> node[110];
  11. int dis[110],fin[110];
  12. string col[100];
  13. int time;
  14.  
  15. int main()
  16. {
  17.  
  18. map<string,int>mymap;
  19. int k,t,num,i,add,temp,c=0;
  20.  
  21. string ch,ch1;
  22. string str[110];
  23.  
  24. //freopen("input.txt","r",stdin);
  25. while(scanf("%d",&num)==1){
  26.  
  27.  
  28. c++;
  29. for(i=1;i<=num;i++){
  30.  
  31. cin>>ch;
  32. str[i]=ch;
  33. mymap[ch]=i;
  34.  
  35.  
  36. col[i]="nil";
  37.  
  38. }
  39.  
  40. for(i=1;i<=num;i++){
  41.  
  42.  
  43. }
  44. scanf("%d",&add);
  45.  
  46. for(i=1;i<=add;i++){
  47. cin>>ch>>ch1;
  48.  
  49. node[mymap[ch]].push_back(mymap[ch1]);
  50. col[mymap[ch1]]="white";
  51.  
  52. }
  53. for(i=1;i<=num;i++){
  54.  
  55. if(col[i]=="nil"){
  56. dfs(i);
  57.  
  58. }
  59. }
  60.  
  61. k=num;
  62. while(k!=0){
  63. t=0;
  64. for(i=1;i<=k-1;i++){
  65.  
  66. if(fin[i]<fin[i+1]){
  67. temp=fin[i];
  68. ch=str[i];
  69. str[i]=str[i+1];
  70. fin[i]=fin[i+1];
  71. fin[i+1]=temp;
  72. str[i+1]=ch;
  73. t=i;
  74. }
  75. }
  76. k=t;
  77. }
  78. printf("Case #%d: Dilbert should drink beverages in this order: ",c);
  79. for(i=1;i<num;i++){
  80.  
  81. cout<<str[i]<<" ";
  82. }
  83. cout<<str[num]<<".\n\n";
  84.  
  85. mymap.clear();
  86.  
  87. for(i=1;i<=num;i++){
  88.  
  89. node[i].clear();
  90. }
  91. }
  92. return 0;
  93. }
  94.  
  95. void dfs(int a){
  96. col[a]="grey";
  97. dis[a]=++time;
  98. for(int x=0;x<node[a].size();x++){
  99.  
  100. if(col[node[a][x]]=="white"){
  101.  
  102.  
  103. dfs(node[a][x]);
  104. }
  105. }
  106. col[a]="black";
  107. fin[a]=++time;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement