Given an array of pairs arr[][] of length N, and an array queries[] of length M, and an integer R, where queries[i] contain an integer from 1 to R, the task for every queries[i] is to find the maximum element of the connected components of the node with value queries[i].
Note: Initially every integer from 1 to R belongs to the distinct set.
Examples:
Input: R = 5, arr = {{1, 2}, {2, 3}, {4, 5}}, queries[] = {2, 4, 1, 3} Output: 3 5 3 3 Explanation: After making the sets from the arr[] pairs, {1, 2, 3}, {4, 5} For the first query: 2 belongs to the set {1, 2, 3} and the maximum element is 3 For the second query: 4 belongs to the set {4, 5} and the maximum element is 5 For the third query: 1 belongs to the set {1, 2, 3} and the maximum element is 3 For the fourth query: 3 belongs to the set {1, 2, 3} and the maximum element is 3
Input: R = 6, arr = {{1, 3}, {2, 4}}, queries = {2, 5, 6, 1} Output: 4 5 6 3
Approach: The given problem can be solved using the Disjoint Set Union. Initially, all the elements are in different sets, process the arr[] and do union operation on the given pairs and in union update, the maximum value for each set in the array, say maxValue[] value for the parent element. For each query perform the find operation and for the returned parent element find the maxParent[parent]. Follow the steps below to solve the problem:
- Initialize a vector maxValue[] to find the maximum element of every set.
- Initialize the vectors parent(R+1), rank(R+1, 0), maxValue(R+1).
- Iterate over the range [1, R+1) using the variable i and set the value of parent[i] and maxValue[i] as i.
- Iterate over the range [1, N-1] using the variable i and call for function operation Union(parent, rank, maxValue, arr[i].first, arr[i].second).
- Iterate over the range [1, M-1] using the variable i and perform the following steps:
- call for function operation Find(parent, queries[i]).
- Print the value of maxValue[i] as the resultant maximum element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Find(vector< int >& parent, int a)
{
return parent[a] = (parent[a] == a)
? a
: (Find(parent, parent[a]));
}
void Union(vector< int >& parent,
vector< int >& rank,
vector< int >& maxValue,
int a, int b)
{
a = Find(parent, a);
b = Find(parent, b);
if (a == b)
return ;
if (rank[a] == rank[b]) {
rank[a]++;
}
if (rank[a] < rank[b]) {
int temp = a;
a = b;
b = temp;
}
parent[b] = a;
maxValue[a] = max(maxValue[a],
maxValue[b]);
}
void findMaxValueOfSet(
vector<pair< int , int > >& arr,
vector< int >& queries, int R, int N,
int M)
{
vector< int > parent(R + 1);
vector< int > rank(R + 1, 0);
vector< int > maxValue(R + 1);
for ( int i = 1; i < R + 1; i++) {
parent[i] = maxValue[i] = i;
}
for ( int i = 0; i < N; i++) {
Union(parent, rank, maxValue,
arr[i].first,
arr[i].second);
}
for ( int i = 0; i < M; i++) {
int P = Find(parent, queries[i]);
cout << maxValue[P] << " " ;
}
}
int main()
{
int R = 5;
vector<pair< int , int > > arr{ { 1, 2 },
{ 2, 3 },
{ 4, 5 } };
vector< int > queries{ 2, 4, 1, 3 };
int N = arr.size();
int M = queries.size();
findMaxValueOfSet(arr, queries, R, N, M);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int Find( int [] parent, int a)
{
return parent[a] = (parent[a] == a)
? a
: (Find(parent, parent[a]));
}
static void Union( int [] parent,
int [] rank,
int [] maxValue,
int a, int b)
{
a = Find(parent, a);
b = Find(parent, b);
if (a == b)
return ;
if (rank[a] == rank[b]) {
rank[a]++;
}
if (rank[a] < rank[b]) {
int temp = a;
a = b;
b = temp;
}
parent[b] = a;
maxValue[a] = Math.max(maxValue[a],
maxValue[b]);
}
static void findMaxValueOfSet(
int [][] arr,
int [] queries, int R, int N,
int M)
{
int [] parent = new int [R + 1 ];
int [] rank = new int [R + 1 ];
int [] maxValue = new int [R + 1 ];
for ( int i = 1 ; i < R + 1 ; i++) {
parent[i] = maxValue[i] = i;
}
for ( int i = 0 ; i < N; i++) {
Union(parent, rank, maxValue,
arr[i][ 0 ],
arr[i][ 1 ]);
}
for ( int i = 0 ; i < M; i++) {
int P = Find(parent, queries[i]);
System.out.print(maxValue[P]+ " " );
}
}
public static void main(String[] args)
{
int R = 5 ;
int [][] arr ={ { 1 , 2 },
{ 2 , 3 },
{ 4 , 5 } };
int [] queries = { 2 , 4 , 1 , 3 };
int N = arr.length;
int M = queries.length;
findMaxValueOfSet(arr, queries, R, N, M);
}
}
|
Python3
def Find(parent, a):
if (parent[parent[a]]! = parent[a]):
parent[a] = findParent(parent,parent[a])
return parent[a]
def Union(parent, rank, maxValue, a, b):
a = Find(parent, a)
b = Find(parent, b)
if (a = = b):
return
if (rank[a] = = rank[b]):
rank[a] + = 1
if (rank[a] < rank[b]):
temp = a
a = b
b = temp
parent[b] = a
maxValue[a] = max (maxValue[a],maxValue[b])
def findMaxValueOfSet(arr,queries, R, N, M):
parent = [ 1 for i in range (R + 1 )]
rank = [ 0 for i in range (R + 1 )]
maxValue = [ 0 for i in range (R + 1 )]
for i in range ( 1 ,R + 1 , 1 ):
parent[i] = maxValue[i] = i
for i in range (N):
Union(parent, rank, maxValue, arr[i][ 0 ],arr[i][ 1 ])
for i in range (M):
P = Find(parent, queries[i])
print (maxValue[P],end = " " )
if __name__ = = '__main__' :
R = 5
arr = [[ 1 , 2 ],
[ 2 , 3 ],
[ 4 , 5 ]];
queries = [ 2 , 4 , 1 , 3 ]
N = len (arr)
M = len (queries)
findMaxValueOfSet(arr, queries, R, N, M)
|
C#
using System;
public class GFG{
static int Find( int [] parent, int a)
{
return parent[a] = (parent[a] == a)
? a
: (Find(parent, parent[a]));
}
static void Union( int [] parent,
int [] rank,
int [] maxValue,
int a, int b)
{
a = Find(parent, a);
b = Find(parent, b);
if (a == b)
return ;
if (rank[a] == rank[b]) {
rank[a]++;
}
if (rank[a] < rank[b]) {
int temp = a;
a = b;
b = temp;
}
parent[b] = a;
maxValue[a] = Math.Max(maxValue[a],
maxValue[b]);
}
static void findMaxValueOfSet(
int [,] arr,
int [] queries, int R, int N,
int M)
{
int [] parent = new int [R + 1];
int [] rank = new int [R + 1];
int [] maxValue = new int [R + 1];
for ( int i = 1; i < R + 1; i++) {
parent[i] = maxValue[i] = i;
}
for ( int i = 0; i < N; i++) {
Union(parent, rank, maxValue,
arr[i,0],
arr[i,1]);
}
for ( int i = 0; i < M; i++) {
int P = Find(parent, queries[i]);
Console.Write(maxValue[P]+ " " );
}
}
public static void Main(String[] args)
{
int R = 5;
int [,] arr ={ { 1, 2 },
{ 2, 3 },
{ 4, 5 } };
int [] queries = { 2, 4, 1, 3 };
int N = arr.GetLength(0);
int M = queries.Length;
findMaxValueOfSet(arr, queries, R, N, M);
}
}
|
Javascript
<script>
function Find(parent, a) {
return (parent[a] = parent[a] == a ? a : Find(parent, parent[a]));
}
function Union(parent, rank, maxValue, a, b) {
a = Find(parent, a);
b = Find(parent, b);
if (a == b) return ;
if (rank[a] == rank[b]) {
rank[a]++;
}
if (rank[a] < rank[b]) {
let temp = a;
a = b;
b = temp;
}
parent[b] = a;
maxValue[a] = Math.max(maxValue[a], maxValue[b]);
}
function findMaxValueOfSet(arr, queries, R, N, M) {
let parent = new Array(R + 1);
let rank = new Array(R + 1).fill(0);
let maxValue = new Array(R + 1);
for (let i = 1; i < R + 1; i++) {
parent[i] = maxValue[i] = i;
}
for (let i = 0; i < N; i++) {
Union(parent, rank, maxValue, arr[i][0], arr[i][1]);
}
for (let i = 0; i < M; i++) {
let P = Find(parent, queries[i]);
document.write(maxValue[P] + " " );
}
}
let R = 5;
let arr = [
[1, 2],
[2, 3],
[4, 5],
];
let queries = [2, 4, 1, 3];
let N = arr.length;
let M = queries.length;
findMaxValueOfSet(arr, queries, R, N, M);
</script>
|
Time Complexity: O(N*log M) Auxiliary Space: O(N)
|