#K85887. E-Commerce Catalog Management
E-Commerce Catalog Management
E-Commerce Catalog Management
You are given a system to manage an e-commerce catalog organized as a hierarchical tree of categories. Each category can have products and subcategories. Your task is to implement the catalog system with three operations:
- add <category_id> <product_id>: Add a product with id product_id to the category category_id.
- query <category_id>: Query the total number of unique products in the category category_id and all of its descendant subcategories.
- <parent_id> <child_id>: Add a subcategory relationship by making child_id a direct child of parent_id.
Input consists of a series of operations, one per line. The sequence terminates with a single line containing a "0". For every query
operation, print the answer on a new line.
Note: If a product is added to multiple categories within the same hierarchy, it should only be counted once in the query result.
inputFormat
The input is given from standard input (stdin
) as multiple lines. Each line (except the last) represents an operation in one of the following formats:
add <category_id> <product_id>
query <category_id>
<parent_id> <child_id>
— to add a subcategory.
The last line of input is a single 0 which indicates the end of operations.
outputFormat
For each query
operation encountered in the input, output one line containing a single integer representing the count of unique products in the queried category (including all its subcategories).
add 1 101
add 2 102
add 2 103
add 3 104
1 2
1 3
query 1
query 2
query 3
0
4
2
1
</p>