cswork / OA打开练习工作台

AmazonOA #329

Product Category Group Sizes

估计中等可在线判题

考点(AI 标注)并查集哈希计数

You are given all product identifiers, which are distinct, and several product pairs, each meaning the two products belong to the same category. The same-category relation is transitive. Find the final number of products in each category and output the group sizes in ascending order; a product that appears in no pair forms a group by itself. Self-pairs and repeated relations do not create new products.

输入格式

The input is compact JSON: [products,pairs], where each item of pairs is two existing product identifiers. The original statement only guarantees that products are distinct and that the endpoints are valid, with no bounds on values or characters; on this site 0≤number of products, number of relations≤100000, and identifiers have 0..8 Unicode characters. Whitespace and non-ASCII characters in JSON strings use JSON escapes, with a space written as \u0020. When the product list is empty, the relations are empty too.

输出格式

If there are products, output the category sizes in ascending order, separated by spaces; if there are no products, output 0.

样例 1

输入:

[["A","B","C","D","E"],[["A","B"],["B","C"],["D","E"]]]

输出:

2 3

样例 2

输入:

[["","a\u0020\u0020b","\ud83d\ude00"],[["","a\u0020\u0020b"],["",""]]]

输出:

1 2

样例 3

输入:

[[],[]]

输出:

0

样例说明

The outputs are 2 3; 1 2; and 0. This site's output protocol for the empty set is a single 0; otherwise only the positive group sizes are output, without the number of groups.