Kingdom unity, time limit gets exceeded??

algorithm
articulation
connected
data-structure
dfs
graph
tle

#1

I tried to solve this problem using the technique mentioned in the editorial i.e, I found all the articulation points in the graph using dfs algorithm and multiplied it buy the cost k to get the final answer. But I am getting the error “Time Limit Exceeded”.

This is my solution.

Anybody help me out resolving this.