ِAtCoder DP contest ("V — Subtree" Iterative)
Difference between en2 and en3, changed 0 character(s)
I just solved the [V-Subtree](https://atcoder.jp/contests/dp/tasks/dp_v) problem on AtCoder DP Contest, and after solving it, I couldn't find anyone who solved it iteratively, so I decided to share my code with you :)↵

[My code (without my template)](https://ideone.com/ldzCcS)↵

My idea was that, to do a topological sort iteratively, I thought I had to use Kahn's algorithm. When I found that I was getting a wrong answer, I tried to find the problem, and it turned out that the tree was undirected. After some deep thinking, I realized that I could treat the tree as directed, so I sorted the elements by their distance from the root (any root can be taken since it's undirected). Then, I went on to write the answer code :)

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English darkahmed 2025-03-20 14:31:40 1
en3 English darkahmed 2025-03-20 14:29:55 0 (published)
en2 English darkahmed 2025-03-20 14:29:12 6
en1 English darkahmed 2025-03-20 14:28:55 771 Initial revision (saved to drafts)