Directories required for creating a folder tree
Python
 
In Python, you would just use os.makedirs(). See the docs at the bottom of this post
import sys

num_existing, num_required = map(int, next(sys.stdin).split())

sep = &quot;/&quot;
directory = {}

for i,path in enumerate(s.rstrip(&quot;\n&quot;) for s in sys.stdin):
    d = directory    
    base = &quot;&quot;
    for p in path.split(sep)[1:]:
        base += sep+p
        if p not in d:
            if i &gt;= num_existing:
                print base
            d[p]={}
        d = d[p]

Python docs for os.makedirs()
&gt;&gt;&gt; import os
&gt;&gt;&gt; help(os.makedirs)

Help on function makedirs in module os:

makedirs(name, mode=511)
    makedirs(path [, mode=0777])
    
    Super-mkdir; create a leaf directory and all intermediate ones.
    Works like mkdir, except that any intermediate path segment (not
    just the rightmost) will be created if it does not exist.  This is
    recursive.