522A - Reposts - CodeForces Solution


*special problem dfs and similar dp graphs trees *1200

Please click on ads to support us..

Python Code:

class Solution:

	def solve(self, reposts, n):
		lookup = {'polycarp': 1}

		for repost in reposts:

			name1, _, name2 = repost.split(" ")

			lookup[name1] = 1 + lookup[name2]

		print(max(lookup.values()))
		return

def main():
	
		t = 1

	sol = Solution()

	while t:
		n = int(input())
		
		reposts = []
		for _ in range(n):
			reposts.append(input().lower())

		sol.solve(reposts, n)

		t -= 1

if __name__ == '__main__':

	main()

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define fo(i, n) for (int i = 0; i < n; i++)
#define ll long long
#define pb push_back
#define F 1000000007
#define S second
#define all(x) x.begin(), x.end()
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<string> vs;

void dfs(map<string,vector<string>> &adj,string &node,unordered_map<string,bool>&visited, unordered_map<string,int> &height){
    /*
       Take action on vertex after entering
        the vertex 
        */   
  
  

    for(auto i:adj[node]){
       /*
       Take action on child before entering
        the child 
        */

        //  cout<<i<<endl;
    //    cout<<"par:"<<node<<" child:"<<i<<endl;
  
         dfs(adj,i,visited,height);
         
          /*
       Take action on child after exiting
        the child
        */ 
       
        height[node] = max(height[i]+1,height[node]);
       
    }
      /*
       Take action on vertex before exiting
        the vertex 
        */ 
    //    return;
}
int main(){
    int n,m;
    cin>>n;
    map<string,vector<string>> adj;
    string root;
    for (int i = 1; i <=n ; i++)
    {
        string s; string u,v;
       cin>>u;
      
        cin>>s;
        cin>>v;
        transform(v.begin(), v.end(), v.begin(), ::tolower);
        transform(u.begin(), u.end(), u.begin(), ::tolower);
        if(i==1){
            root = v;
        }

    //   int k=0;
    //   while(s[k]!=' '){
    //     u.push_back(s[k]);
    //     k++;
    //   }
    //   k++;
    //   while(s[k]!=' '){
    //     k++;
    //   }
    //   while(k<s.size()){
    //     v.push_back(s[k]);
    //     k++;
    //   }
    
    //    cout<<u<<" "<<v<<endl;
      adj[v].push_back(u);

    
    }
   
    
//  for(auto i:adj){
//     cout<<i.first<<" -> ";
//    for(auto j:i.second){
//     cout<<j<<" ";
//    }
//    cout<<endl;
    
//  }
    
    unordered_map<string,bool> visited;
   unordered_map<string,int> height;
      for(auto i:adj){
    visited[i.first]=0;
    height[i.first]=0;
   }
   
     int tree = 0;
    dfs(adj,root,visited,height);
    cout<<height[root]+1<<endl;

  
    
  
    
    

    
}


Comments

Submit
0 Comments
More Questions

733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences