Find first undeleted integer from K to N in given unconnected Graph after performing Q queries
Given a positive integer N representing the set of integers [1, N] and an array queries[] of length Q of type {L, K}, the task is to perform the given queries according to the following rules and print the result:
- If the value of L is 1, then delete the given integer K.
- If the value of L is 2, then print the first integer from K to N which is not deleted.
Note: When all the elements to the right are deleted, the answer will be -1.
Examples:
Input: N = 3, queries[] = {{2, 1}, {1, 1}, {2, 1}, {2, 3}}
Output: 1 2 3
Explanation:
For the first query: the rightmost element for 1 is 1 only.
After the second query: 1 is deleted.
For the third query: the rightmost element for 1 is 2.
For the fourth query: the rightmost element for 3 is 3.
Input: N = 5, queries = {{2, 1}, {1, 3}, {2, 3}, {1, 2}, {2, 2}, {1, 5}, {2, 5}}
Output: 1 4 4 -1
Approach: The given problem can be solved by using Disjoint Set Union Data Structure. Initially, all the elements are in different sets, for the first type query perform the Union Operation on the given integer. For the second type of query, get the parent of the given vertex and print the value stored in the array graph[]. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Get(vector< int >& graph, int a)
{
return graph[a]
= (graph[a] == a ? a
: Get(graph, graph[a]));
}
void Union(vector< int >& graph,
int a, int b)
{
a = Get(graph, a);
b = Get(graph, b);
graph[a] = b;
}
void Queries(vector<pair< int , int > >& queries,
int N, int M)
{
vector< int > graph(N + 2);
for ( int i = 1; i <= N + 1; i++) {
graph[i] = i;
}
for ( auto query : queries) {
if (query.first == 1) {
Union(graph, query.second,
query.second + 1);
}
else {
int a = Get(graph, query.second);
if (a == N + 1)
cout << -1 << " " ;
else
cout << graph[a] << " " ;
}
}
}
int main()
{
int N = 5;
vector<pair< int , int > > queries{
{ 2, 1 }, { 1, 1 }, { 2, 1 }, { 2, 3 }
};
int Q = queries.size();
Queries(queries, N, Q);
return 0;
}
|
Java
class GFG{
public static int Get( int [] graph, int a)
{
return graph[a]
= (graph[a] == a ? a
: Get(graph, graph[a]));
}
public static void Union( int [] graph,
int a, int b)
{
a = Get(graph, a);
b = Get(graph, b);
graph[a] = b;
}
public static void Queries( int [][] queries,
int N, int M)
{
int [] graph = new int [N + 2 ];
for ( int i = 1 ; i <= N + 1 ; i++) {
graph[i] = i;
}
for ( int [] query : queries) {
if (query[ 0 ] == 1 ) {
Union(graph, query[ 1 ],
query[ 1 ] + 1 );
}
else {
int a = Get(graph, query[ 1 ]);
if (a == N + 1 )
System.out.print(- 1 );
else
System.out.print(graph[a] + " " );
}
}
}
public static void main(String args[])
{
int N = 5 ;
int [][] queries = { { 2 , 1 }, { 1 , 1 }, { 2 , 1 }, { 2 , 3 }};
int Q = queries.length;
Queries(queries, N, Q);
}
}
|
Python3
def Get(graph, a):
if graph[graph[a]]! = graph[a]:
graph[a] = Get(graph, graph[a])
else :
return graph[a]
def Union(graph, a, b):
a = Get(graph, a)
b = Get(graph, b)
graph[a] = b
def Queries(queries, N, M):
graph = [ 0 for i in range (N + 2 )]
for i in range ( 1 ,N + 2 , 1 ):
graph[i] = i
for query in queries:
if (query[ 0 ] = = 1 ):
Union(graph, query[ 1 ], query[ 1 ] + 1 )
else :
a = Get(graph, query[ 1 ])
if (a = = N + 1 ):
print ( - 1 )
else :
print (graph[a],end = " " )
if __name__ = = '__main__' :
N = 5
queries = [[ 2 , 1 ],[ 1 , 1 ],[ 2 , 1 ],[ 2 , 3 ]]
Q = len (queries)
Queries(queries, N, Q)
|
C#
using System;
class GFG
{
public static int Get( int [] graph, int a)
{
return graph[a] = (graph[a] == a ? a : Get(graph, graph[a]));
}
public static void Union( int [] graph, int a, int b)
{
a = Get(graph, a);
b = Get(graph, b);
graph[a] = b;
}
public static void Queries( int [][] queries, int N, int M)
{
int [] graph = new int [N + 2];
for ( int i = 1; i <= N + 1; i++)
{
graph[i] = i;
}
foreach ( int [] query in queries)
{
if (query[0] == 1)
{
Union(graph, query[1], query[1] + 1);
}
else
{
int a = Get(graph, query[1]);
if (a == N + 1)
Console.Write(-1);
else
Console.Write(graph[a] + " " );
}
}
}
static void Main( string [] args)
{
int N = 5;
int [][] queries = {
new int [] { 2, 1 },
new int [] { 1, 1 },
new int [] { 2, 1 },
new int [] { 2, 3 }
};
int Q = queries.Length;
Queries(queries, N, Q);
}
}
|
Javascript
<script>
function Get(graph, a) {
return (graph[a] = graph[a] == a ? a : Get(graph, graph[a]));
}
function Union(graph, a, b) {
a = Get(graph, a);
b = Get(graph, b);
graph[a] = b;
}
function Queries(queries, N, M) {
let graph = new Array(N).fill(2);
for (let i = 1; i <= N + 1; i++) {
graph[i] = i;
}
for (let query of queries) {
if (query[0] == 1) {
Union(graph, query[1], query[1] + 1);
} else {
let a = Get(graph, query[1]);
if (a == N + 1) document.write(-1 + " " );
else document.write(graph[a] + " " );
}
}
}
let N = 5;
let queries = [
[2, 1],
[1, 1],
[2, 1],
[2, 3],
];
let Q = queries.length;
Queries(queries, N, Q);
</script>
|
Time Complexity: O(M*log N)
Auxiliary Space: O(N)
Last Updated :
09 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...