#P9216. Document Combination and Similarity Query
Document Combination and Similarity Query
Document Combination and Similarity Query
Fusu has not slept for 36 hours while working on her theoretical computer science assignment. To catch some sleep, she discovered n documents. The i-th document is represented by a sequence \(a_i\). Fusu plans to combine these documents using two types of operations:
1 x y
: Concatenate document \(a_x\) to the end of document \(a_y\), then delete document \(a_x\). It is guaranteed that after a1
operation, the deleted document will not appear in any subsequent operation.2 x y
: Query whether documents \(a_x\) and \(a_y\) are similar.
Two documents \(a_x\) and \(a_y\) are defined as similar if it is possible to rearrange the elements of \(a_x\) in some order so that it becomes equal to \(a_y\). In other words, they are similar if they have the same multiset of elements.
Example:
Suppose \(a_1 = [1,2]\), \(a_2 = [3,4]\), \(a_3 = [1,2,3,4]\). After performing the operation 1 1 2
, document \(a_1\) is appended to \(a_2\) (so \(a_2\) becomes \([3,4,1,2]\)) and \(a_1\) is deleted. Then, executing 2 2 3
will return YES
since \(a_2\) can be rearranged as \([1,2,3,4]\), which is equal to \(a_3\).
inputFormat
The first line contains two integers \(n\) and \(q\) --- the number of documents and the number of operations.
Then, \(n\) lines follow. The i-th of these lines begins with an integer \(k_i\) (the length of the \(i\)-th document), followed by \(k_i\) integers representing the elements of \(a_i\). It is recommended to sort each sequence internally (or maintain a count) since similarity only depends on the multiset of elements.
After that, \(q\) lines follow, each describing an operation in one of the following two formats:
1 x y
2 x y
All indices are 1-indexed.
outputFormat
For each operation of the form 2 x y
, output a single line containing YES
if document \(a_x\) and \(a_y\) are similar, and NO
otherwise.
sample
3 2
2 1 2
2 3 4
4 1 2 3 4
1 1 2
2 2 3
YES