Advertisement
Guest User

A

a guest
Mar 31st, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include<iostream>
  2. #include<map>
  3. #include<vector>
  4. using namespace std;
  5.  
  6. int c, r;
  7. string s1, s2;
  8. map<string, int> ans;
  9. map<string, vector<string> > graph;
  10. map<string, bool> viz;
  11. vector<string> animals;
  12.  
  13.  
  14. int solve(string node){
  15.     viz[node] = true;
  16.     if(graph[node].size() == 0) return ans[node] = 1;
  17.  
  18.     for(int i=0; i < graph[node].size(); i++){
  19.         string aux = graph[node][i];
  20.         if(viz[aux]) ans[node] = max(ans[node], 1 + ans[aux]);
  21.         else ans[node] = max(ans[node], 1+solve(aux));
  22.     }
  23.  
  24.     return ans[node];
  25. }
  26.  
  27. void build(){
  28.     for(int i=0; i < c; i++){
  29.         string aux = animals[i];
  30.         if(!viz[aux]) solve(aux);
  31.     }
  32. }
  33.  
  34. int search(){
  35.     int tmp = ans[animals[0]];
  36.     for(int i=0; i < c; i++){
  37.         tmp = max(tmp, ans[animals[i]]);
  38.     }
  39.     return tmp;
  40. }
  41.  
  42. int main(){
  43.     ans.clear(), viz.clear(), animals.clear(), graph.clear();
  44.     cin >> c >> r;
  45.     if(c == 0 and r == 0) return 0;
  46.     for(int i=0; i < c; i++){
  47.         cin >> s1;
  48.         animals.push_back(s1);
  49.     }
  50.    
  51.     for(int i=0; i < r; i++){
  52.         cin >> s1 >> s2;
  53.         graph[s2].push_back(s1);
  54.     }
  55.     build();
  56.     cout << search() << endl;
  57.     main();
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement