Advertisement
hieudoan

UVa 11060 Beverages

Aug 8th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n,m;
  5. vector<string> bev;   //store all bevarages with order input
  6. map<string,int> indegree;  
  7. map<string,vector<string> > adjList;
  8.  
  9.  
  10. int main()
  11. {
  12.     int c =0;
  13.     while(cin>>n){
  14.         bev.clear();
  15.         indegree.clear();
  16.         adjList.clear();
  17.  
  18.         string str;
  19.         for(int i=0;i<n;i++){
  20.             cin>>str;
  21.             bev.push_back(str);
  22.         }
  23.         cin>>m;
  24.         string str1;
  25.         for(int i=0;i<m;i++){
  26.             cin>>str>>str1;
  27.             indegree[str1]++;
  28.             adjList[str].push_back(str1);
  29.         }
  30.    
  31.         queue<string> q;
  32.         int i=0;
  33.         while(indegree[bev[i]]>0){i++;}
  34.         q.push(bev[i]);
  35.         vector<string> u1=adjList[bev[i]];
  36.         for(int j=0;j<u1.size();j++){
  37.             indegree[u1[j]]--;
  38.         }
  39.         bev.erase(bev.begin()+i);
  40.         vector<string> tp;
  41.  
  42.         while(!q.empty()){
  43.             string p=q.front();
  44.             q.pop();
  45.             tp.push_back(p);
  46.             int i=0;
  47.             while(indegree[bev[i]] && i<bev.size()){i++;}
  48.             if(i==bev.size()) break;
  49.             q.push(bev[i]);  //push into queue cái có indegree=0
  50.             vector<string> u=adjList[bev[i]];
  51.             for(int j=0;j<u.size();j++){
  52.                 --indegree[u[j]]; //descrease all nodes connect with node pushed
  53.             }
  54.  
  55.             bev.erase(bev.begin()+i);   //erase node from input
  56.         }
  57.         cout<<"Case #"<<++c<<": Dilbert should drink beverages in this order:";
  58.         for(int j=0;j<tp.size();j++){
  59.             cout<<" "<<tp[j];
  60.         }
  61.         cout<<"."<<endl;
  62.         cout<<endl;
  63.  
  64.     }
  65.     return 0;
  66. }
  67. //hieudoanitus
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement