Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<map>
- #include<vector>
- using namespace std;
- int c, r;
- string s1, s2;
- map<string, int> ans;
- map<string, vector<string> > graph;
- map<string, bool> viz;
- vector<string> animals;
- int solve(string node){
- viz[node] = true;
- if(graph[node].size() == 0) return ans[node] = 1;
- for(int i=0; i < graph[node].size(); i++){
- string aux = graph[node][i];
- if(viz[aux]) ans[node] = max(ans[node], 1 + ans[aux]);
- else ans[node] = max(ans[node], 1+solve(aux));
- }
- return ans[node];
- }
- void build(){
- for(int i=0; i < c; i++){
- string aux = animals[i];
- if(!viz[aux]) solve(aux);
- }
- }
- int search(){
- int tmp = ans[animals[0]];
- for(int i=0; i < c; i++){
- tmp = max(tmp, ans[animals[i]]);
- }
- return tmp;
- }
- int main(){
- ans.clear(), viz.clear(), animals.clear(), graph.clear();
- cin >> c >> r;
- if(c == 0 and r == 0) return 0;
- for(int i=0; i < c; i++){
- cin >> s1;
- animals.push_back(s1);
- }
- for(int i=0; i < r; i++){
- cin >> s1 >> s2;
- graph[s2].push_back(s1);
- }
- build();
- cout << search() << endl;
- main();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement