3. Cluster Queries
In a Salesforce global infrastructure, there are customer support clusters numbered from 1 to clusters, distributed across multiple regions. These clusters are interconnected by n communication links, represented by the array connections, such that if two clusters are connected directly or indirectly, they belong to the same support network.
Each cluster has a case resolution system to handle customer cases. If a cluster's resolution system goes offline, it forwards the cases to the active cluster in the same support network with the lowest ID, which then resolves the cases. If no active clusters remain in the network, the case is logged as unresolved.
You need to process q queries of two types:
1. A case-assignment query: "1 cluster_id", where a case is assigned to the specified cluster.
2. A system-failure query: "2 cluster_id", where the resolution system of the specified cluster goes offline.
For each case-assignment query, output the ID of the cluster that resolves the case. Specifically:
- When a case-assignment query is made to cluster sender_cluster_id, find the active cluster in the same support network as sender_cluster_id with the lowest ID and output its ID.
- If no active clusters remain in the network, output -1.
Example:
Suppose:
clusters = 3
n = 2
connections = [[1, 2], [2, 3]]
q = 5
queries = [[2, 2], [1, 2], [2, 1], [2, 3], [1, 1]]
Function Description:
Complete the function getAssignedCluster in the editor below.
getAssignedCluster has the following parameters:
- int clusters: number of clusters
- int connections[n][2]: the connections between clusters
- int queries[q][2]: the queries
Returns:
- int: the ID of the cluster that resolves the case, or -1 if no active cluster remains in the network.
Constraints:
- 1 ≤ clusters ≤ 10^5
- 0 ≤ n ≤ 10^5
- 1 ≤ q ≤ 2 × 10^5
- 1 ≤ connections[i][0], connections[i][1] ≤ clusters
- 1 ≤ queries[i][0] ≤ 2
- 1 ≤ queries[i][1] ≤ clusters
- There is at least 1 query of type 1.
Input Format For Custom Testing:
The first line contains an integer, clusters.
The next line contains an integer, n, the number of interconnections.
The next line contains an integer, 2.
Each line i of the subsequent n lines (where 0 ≤ i < n) contains two integers, connections[i][0] and connections[i][1].
The next line contains an integer, q, the number of queries.
The next line contains an integer, 2.
Each line i of the subsequent q lines (where 0 ≤ i < q) contains two integers, queries[i][0] and queries[i][1].
Asked in:
SALESFORCE