• Asked to answer
    + 1 comment

    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