Q 查询的给定节点的连通分量中的最大元素
给定一个长度为N的数组对arr[][]和一个长度为M的数组queries[]和一个整数R ,其中queries[i]包含一个从1到R的整数,每个queries[i]的任务是找到具有值queries[i]的节点的连通分量的最大元素。
Note: Initially every integer from 1 to R belongs to the distinct set.
例子:
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
方法:给定的问题可以使用不相交集并集来解决。最初,所有元素都在不同的集合中,处理arr[]并对给定的对进行联合操作,并在联合更新中,数组中每个集合的最大值,例如父元素的maxValue[]值。对于每个查询执行查找操作,并为返回的父元素查找maxParent[parent] 。请按照以下步骤解决问题:
- 初始化一个向量maxValue[]以找到每个集合的最大元素。
- 初始化向量parent(R+1), rank(R+1, 0), maxValue(R+1) 。
- 使用变量i遍历范围[1, R+1)并将parent[i]和maxValue[i]的值设置为i 。
- 使用变量i遍历范围[1, N-1]并调用函数操作Union(parent, rank, maxValue, arr[i].first, arr[i].second) 。
- 使用变量i遍历范围[1, M-1]并执行以下步骤:
- 调用函数操作Find(parent, queries[i]) 。
- 打印maxValue[i]的值作为结果最大元素。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to perform the find operation
// to find the parent of a disjoint set
int Find(vector& parent, int a)
{
return parent[a] = (parent[a] == a)
? a
: (Find(parent, parent[a]));
}
// FUnction to perform union operation
// of disjoint set union
void Union(vector& parent,
vector& rank,
vector& maxValue,
int a, int b)
{
a = Find(parent, a);
b = Find(parent, b);
if (a == b)
return;
// If the rank are the same
if (rank[a] == rank[b]) {
rank[a]++;
}
if (rank[a] < rank[b]) {
int temp = a;
a = b;
b = temp;
}
parent[b] = a;
// Update the maximum value
maxValue[a] = max(maxValue[a],
maxValue[b]);
}
// Function to find the maximum element
// of the set which belongs to the
// element queries[i]
void findMaxValueOfSet(
vector >& arr,
vector& queries, int R, int N,
int M)
{
// Stores the parent elements
// of the sets
vector parent(R + 1);
// Stores the rank of the sets
vector rank(R + 1, 0);
// Stores the maxValue of the sets
vector maxValue(R + 1);
for (int i = 1; i < R + 1; i++) {
// Update parent[i] and
// maxValue[i] to i
parent[i] = maxValue[i] = i;
}
for (int i = 0; i < N; i++) {
// Add arr[i].first and
// arr[i].second elements to
// the same set
Union(parent, rank, maxValue,
arr[i].first,
arr[i].second);
}
for (int i = 0; i < M; i++) {
// Find the parent element of
// the element queries[i]
int P = Find(parent, queries[i]);
// Print the maximum value
// of the set which belongs
// to the element P
cout << maxValue[P] << " ";
}
}
// Driver Code
int main()
{
int R = 5;
vector > arr{ { 1, 2 },
{ 2, 3 },
{ 4, 5 } };
vector queries{ 2, 4, 1, 3 };
int N = arr.size();
int M = queries.size();
findMaxValueOfSet(arr, queries, R, N, M);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to perform the find operation
// to find the parent of a disjoint set
static int Find(int [] parent, int a)
{
return parent[a] = (parent[a] == a)
? a
: (Find(parent, parent[a]));
}
// FUnction to perform union operation
// of disjoint set union
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 the rank are the same
if (rank[a] == rank[b]) {
rank[a]++;
}
if (rank[a] < rank[b]) {
int temp = a;
a = b;
b = temp;
}
parent[b] = a;
// Update the maximum value
maxValue[a] = Math.max(maxValue[a],
maxValue[b]);
}
// Function to find the maximum element
// of the set which belongs to the
// element queries[i]
static void findMaxValueOfSet(
int[][] arr,
int [] queries, int R, int N,
int M)
{
// Stores the parent elements
// of the sets
int [] parent = new int[R + 1];
// Stores the rank of the sets
int [] rank = new int[R + 1];
// Stores the maxValue of the sets
int [] maxValue = new int[R + 1];
for (int i = 1; i < R + 1; i++) {
// Update parent[i] and
// maxValue[i] to i
parent[i] = maxValue[i] = i;
}
for (int i = 0; i < N; i++) {
// Add arr[i][0] and
// arr[i][1] elements to
// the same set
Union(parent, rank, maxValue,
arr[i][0],
arr[i][1]);
}
for (int i = 0; i < M; i++) {
// Find the parent element of
// the element queries[i]
int P = Find(parent, queries[i]);
// Print the maximum value
// of the set which belongs
// to the element P
System.out.print(maxValue[P]+ " ");
}
}
// Driver Code
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);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python 3 program for the above approach
# Function to perform the find operation
# to find the parent of a disjoint set
def Find(parent, a):
if(parent[parent[a]]!=parent[a]):
parent[a]=findParent(parent,parent[a])
return parent[a]
#return parent[a] = a if (parent[a] == a) else Find(parent, parent[a])
# FUnction to perform union operation
# of disjoint set union
def Union(parent, rank, maxValue, a, b):
a = Find(parent, a)
b = Find(parent, b)
if (a == b):
return
# If the rank are the same
if (rank[a] == rank[b]):
rank[a] += 1
if (rank[a] < rank[b]):
temp = a
a = b
b = temp
parent[b] = a
# Update the maximum value
maxValue[a] = max(maxValue[a],maxValue[b])
# Function to find the maximum element
# of the set which belongs to the
# element queries[i]
def findMaxValueOfSet(arr,queries, R, N, M):
# Stores the parent elements
# of the sets
parent = [1 for i in range(R+1)]
# Stores the rank of the sets
rank = [0 for i in range(R+1)]
# Stores the maxValue of the sets
maxValue = [0 for i in range(R + 1)]
for i in range(1,R + 1,1):
# Update parent[i] and
# maxValue[i] to i
parent[i] = maxValue[i] = i
for i in range(N):
# Add arr[i].first and
# arr[i].second elements to
# the same set
Union(parent, rank, maxValue, arr[i][0],arr[i][1])
for i in range(M):
# Find the parent element of
# the element queries[i]
P = Find(parent, queries[i])
# Print the maximum value
# of the set which belongs
# to the element P
print(maxValue[P],end = " ")
# Driver Code
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)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
public class GFG{
// Function to perform the find operation
// to find the parent of a disjoint set
static int Find(int [] parent, int a)
{
return parent[a] = (parent[a] == a)
? a
: (Find(parent, parent[a]));
}
// FUnction to perform union operation
// of disjoint set union
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 the rank are the same
if (rank[a] == rank[b]) {
rank[a]++;
}
if (rank[a] < rank[b]) {
int temp = a;
a = b;
b = temp;
}
parent[b] = a;
// Update the maximum value
maxValue[a] = Math.Max(maxValue[a],
maxValue[b]);
}
// Function to find the maximum element
// of the set which belongs to the
// element queries[i]
static void findMaxValueOfSet(
int[,] arr,
int [] queries, int R, int N,
int M)
{
// Stores the parent elements
// of the sets
int [] parent = new int[R + 1];
// Stores the rank of the sets
int [] rank = new int[R + 1];
// Stores the maxValue of the sets
int [] maxValue = new int[R + 1];
for (int i = 1; i < R + 1; i++) {
// Update parent[i] and
// maxValue[i] to i
parent[i] = maxValue[i] = i;
}
for (int i = 0; i < N; i++) {
// Add arr[i,0] and
// arr[i,1] elements to
// the same set
Union(parent, rank, maxValue,
arr[i,0],
arr[i,1]);
}
for (int i = 0; i < M; i++) {
// Find the parent element of
// the element queries[i]
int P = Find(parent, queries[i]);
// Print the maximum value
// of the set which belongs
// to the element P
Console.Write(maxValue[P]+ " ");
}
}
// Driver Code
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);
}
}
// This code is contributed by shikhasingrajput
Javascript
3 5 3 3
时间复杂度: O(N*log M)
辅助空间: O(N)