#C3618. Gotham Security Monitor
Gotham Security Monitor
Gotham Security Monitor
You are tasked with monitoring the security of various zones in Gotham City. The city comprises N zones. Some pairs of zones are connected by bidirectional roads. Over time, you will receive queries that either check if two zones are currently connected (and thus secure) or update the security level of a particular zone (which does not affect connectivity).
Connectivity Query: This query checks if zone u and zone v belong to the same connected component. Two zones are in the same connected component if there is a path between them using the available roads. The answer is reported as YES
if they are connected or NO
otherwise.
Update Query: Although provided, the update query does not affect connectivity and can be safely ignored.
The problem can be formally described using the union-find (disjoint set union, DSU) data structure. Specifically, if we denote the parent of element x by \(parent[x]\) then the find operation is defined by:
[ find(x) = \begin{cases} x, & \text{if } parent[x] = x \ find(parent[x]), & \text{otherwise} \end{cases} ]
Your task is to process all queries in order and output the result for the connectivity queries.
inputFormat
The input is read from standard input (stdin) and has the following format:
N M u1 v1 u2 v2 ... (M lines, each contains two integers describing a road between zones ui and vi) Q type1 u1 v1 type2 u2 v2 ... (Q lines, each describing a query with three integers)
Where:
N
is the number of zones.M
is the number of roads (edges).- Each of the next M lines contains two integers
u
andv
indicating that there is a road connecting zonesu
andv
. Q
is the number of queries.- Each query is given by three integers:
type
,u
, andv
. - For a query of type 0, output whether zone
u
and zonev
are connected, outputtingYES
orNO
. - For queries of type 1, update commands (e.g. updating the security level) that do not affect connectivity. You can ignore these queries.
outputFormat
For each query of type 0, print the result to standard output (stdout) on a separate line. The output should be YES
if the two zones are connected, and NO
otherwise.
5
4
1 2
2 3
3 4
4 5
4
0 1 5
1 3 10
0 3 2
0 5 1
YES
YES
YES
</p>