Folder Cleanup (large)
Time limit1.5sMemory limit1024 MB
Given a folder tree, move subtrees between folders where identical names merge or overwrite, then answer queries counting distinct file names and total files under a folder.
- Level
Medium6 of 10
- Topics
- Tree, Simulation, Hash map, DFS
- Solved
- No attempts yet
Problem
Inside a folder named main there are various files and folders.
main
├─ FolderA
│ ├─ File1
│ └─ File2
└─ FolderB
├─ FolderC
│ ├─ File4
│ └─ File5
├─ File1
└─ File3
The structure above shows the hierarchy under the main folder. FolderA, FolderB, and FolderC are folders, and File1, File2, and File3 are files. Files with the same name have exactly the same contents.
A folder cannot contain two or more files with the same name.
A directory under main cannot contain two or more folders with the same name.
You want to clean up the main folder by moving folders or files.
For example, suppose you want to move the two files File1 and File3 and the folder FolderC under FolderB into FolderA.
The file File1 overwrites the identical file that already exists in FolderA. File3 has no identical file, so it is moved as is. The folder FolderC also has no identical folder, so it is moved under FolderA. Since every folder and file under FolderB has been moved, FolderB is deleted.
Below is the hierarchy under main after moving FolderB into FolderA.
main
└─ FolderA
├─ FolderC
│ ├─ File4
│ └─ File5
├─ File1
├─ File2
└─ File3
You want to clean up folders through this process. After the cleanup, you want to check file information with queries.
Input
The first line gives the total number of folders and the total number of files inside the main folder, separated by a space.
From the second line to the -th line, the name of the parent folder , the name of the folder or file , and , which tells whether it is a folder, are given separated by spaces.
The value of is 1 if is a folder and 0 if it is a file.
The -th line gives the number of moves .
Over the next lines, a folder path and a folder path are given separated by a space.
The moves must be performed in the order they are given.
The files and folders under are moved under . It is guaranteed that is not an ancestor of .
The next line gives the number of queries .
The next lines contain the queries. Each query gives the path of a folder starting from main. For example, if a query for FolderB inside the main folder is given, it comes as main/FolderB, the path of FolderB. It is guaranteed that the path in a query always points to a folder that exists.
Output
For each query in order, print on one line the number of distinct file kinds under the folder and the total number of files.
The number of distinct file kinds counts identical files as one. The total number of files does not count identical files as one.
For example, if there are 5 files named File1, the number of distinct kinds is 1 and the total number of files is 5.
Constraints
- and consist only of English uppercase and lowercase letters and digits.