[C++/Python] Concise DFS
Python\n\nclass Solution(object):\n    def arrayNesting(self, nums):\n        vis = set()\n\n        def dfs(i):\n            if i in vis: return 0;\n          
# Python\n```\nclass Solution(object):\n    def arrayNesting(self, nums):\n        vis = set()\n\n        def dfs(i):\n            if i in vis: return 0;\n            vis.add(i)\n            return 1 + dfs(nums[i])\n        \n        ans = 0\n        for i in range(len(nums)):\n            if i not in vis: ans = max(ans, dfs(i))\n        \n        return ans\n```\n\n# C++\n```\nclass Solution {\n    int dfs(int i, vector<int>& nums, unordered_set<int> &vis) {\n        if(vis.find(i) != vis.end()) return 0;\n        vis.insert(i);\n        return 1 + dfs(nums[i], nums, vis);\n    }\n\npublic:\n    int arrayNesting(vector<int>& nums) {\n        int n = nums.size();\n        unordered_set<int> vis;\n\n        int ans = 0;\n        for(int i=0; i<n; i++) {\n            if(vis.find(i) == vis.end()) ans = max(ans, dfs(i, nums, vis));\n        }\n        return ans;\n    }\n};\n```\n
['Depth-First Search', 'Graph', 'Python', 'C++']