This page is still under construction.

Parts of this page are still being built. What you see may change.

Folder Cleanup (large)

Time limit1.5sMemory limit1024 MB

Summary
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 NN and the total number of files MM inside the main folder, separated by a space.

From the second line to the N+M+1N + M + 1-th line, the name of the parent folder PP, the name of the folder or file FF, and CC, which tells whether it is a folder, are given separated by spaces.

The value of CC is 1 if FF is a folder and 0 if it is a file.

The N+M+2N + M + 2-th line gives the number of moves KK.

Over the next KK lines, a folder path AA and a folder path BB are given separated by a space.

The moves must be performed in the order they are given.

The files and folders under AA are moved under BB. It is guaranteed that AA is not an ancestor of BB.

The next line gives the number of queries QQ.

The next QQ 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

  • 1≤N≤1,0001 \le N \le 1,000
  • 1≤M≤1,0001 \le M \le 1,000
  • 0≤K≤1,0000 \le K \le 1,000
  • 1≤∣P∣≤101 \le |P| \le 10
  • 1≤∣F∣≤101 \le |F| \le 10
  • 0≤C≤10 \le C \le 1
  • 1≤Q≤1,0001 \le Q \le 1,000
  • PP and FF consist only of English uppercase and lowercase letters and digits.

Examples2

  1. Example 1

    Input
    3 6
    main FolderA 1
    main FolderB 1
    FolderA File1 0
    FolderA File2 0
    FolderB FolderC 1
    FolderC File4 0
    FolderC File5 0
    FolderB File1 0
    FolderB File3 0
    1
    main/FolderB main/FolderA
    3
    main
    main/FolderA
    main/FolderA/FolderC
    
    Expected output
    5 5
    5 5
    2 2
    
  2. Example 2

    Input
    3 4
    main FolderA 1
    main FolderB 1
    FolderB File2 0
    FolderA FolderC 1
    FolderA File1 0
    FolderC File2 0
    FolderC File3 0
    2
    main/FolderA main/FolderB
    main/FolderB/FolderC main/FolderB
    2
    main
    main/FolderB
    
    Expected output
    3 3
    3 3