We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I can give some tips.for result,Use long long.problem solution :use DFS to get conected component ,use a count variable to count number of vertices in each component.this counts are stored in a vector.then for each number in vector,Result increased by that number* sum of all number before that in vector .you can do that in linear complexity by storing cumulative sum in another array/vector.
I think this can help you
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Journey to the Moon
You are viewing a single comment's thread. Return to all comments →
I can give some tips.for result,Use long long.problem solution :use DFS to get conected component ,use a count variable to count number of vertices in each component.this counts are stored in a vector.then for each number in vector,Result increased by that number* sum of all number before that in vector .you can do that in linear complexity by storing cumulative sum in another array/vector. I think this can help you