this gave me AC:-
Found all connected components, added the weights of their spanning trees, finally I connected all those components using red edges.
AC code:-Attempt 2 - Google Docs
bro, i also thought to use a graph and connected components, but can u analyse my solution, just take a glimpse, and sort out anything from it it will be helpful
Answer is not very correct because your code didn’t count the number of connected components. Thats my conclusion. Only Graph/DSU can solve this problem.
I figured dfs would time out looking at the constraints since dfs takes {O(|E|+|V|)} and in worst case may take {O(|V|^2)}. Hence, I used DSU. How is that your solution was able to pass the given constraints?
Because my solution’s time complexity is :- Time Complexity of DFS.(I didn’t actually calculate spanning trees as there was no need, only observation was enough for me to calculate each spanning tree, my bad, I never studied DSU.)
Well, you can do dfs on any graph which has (10^5) number of nodes, and (10^5) edges…I don’t think there is anything new in it . No one gets TLE for dfs for sparse graphs. And worst case for DFS is :- O(V+E).
Now, its frustrating for me. Who said the number of edges is always ‘v*(v-1)/2’??? This is the condition only for a complete graph. My graph has only ‘M’ edges , mentioned in the problem statement itself. In short, my graph has 10^5 nodes and 10^5 edges.(All edges in my graph are BLACK.) . I won’t give further clarifications.
Damn. I saw only {M \leq N*(N-1)/2} and didn’t see {0 \leq M \leq 10^5} which is given at the end. Did I just get 3 penalties for not seeing this. My first attempt with dfs passed all testcases. And to optimize it I used DSU.
Kickstart considers last successfully submitted solution as final solution?