#P8123. Data Sharing Among Servers
Data Sharing Among Servers
Data Sharing Among Servers
There are \(N\) servers, where the \(i\)th server initially stores data block \(i\). There are three types of operations:
S a b
: The \(a\)th server and the \(b\)th server share data. After sharing, both servers obtain the union of their current data blocks (duplicates are automatically removed, i.e. a set union).Q a d
: Query whether the \(a\)th server has data block \(d\). Since data block \(d\) initially exists only on server \(d\), after sharing, \(a\) has block \(d\) if and only if server \(a\) and server \(d\) are connected by sharing operations.C a
: Query the number of servers that currently store data block \(a\). Note that since server \(a\) initially holds block \(a\), after sharing the entire connected component will have block \(a\).
There are exactly \(N-1\) operations of type S
. If these operations are viewed as adding edges between servers, they form a tree with \(N\) nodes. In addition, there are \(K\) query operations (of types Q
and C
combined). Output the result for each query in the order they appear.
inputFormat
The first line contains two integers \(N\) and \(K\): the number of servers and the total number of query operations (Q
and C
combined), respectively.
The next \(N-1+K\) lines each contain one operation in one of the following formats:
S a b
Q a d
C a
It is guaranteed that there are exactly \(N-1\) S
operations and that these operations form a tree connecting all servers.
outputFormat
For each query operation (Q
and C
), output one line:
- For a
Q
operation, outputYES
if the \(a\)th server contains data block \(d\), andNO
otherwise. - For a
C
operation, output an integer representing the number of servers that store data block \(a\).
sample
3 3
S 1 2
S 2 3
Q 1 3
C 2
Q 2 1
YES
3
YES
</p>