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 :)
↵
[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 :)