📌  相关文章
📜  执行 Q 次查询后,在给定的未连接图中查找从 K 到 N 的第一个未删除整数

📅  最后修改于: 2022-05-13 01:56:06.359000             🧑  作者: Mango

执行 Q 次查询后,在给定的未连接图中查找从 K 到 N 的第一个未删除整数

给定一个表示整数集合[1, N]的正整数N和一个长度为Q的类型为{L, K}的数组query[] ,任务是根据以下规则执行给定的查询并打印结果:

  • 如果 L 的值为 1 ,则删除给定的整数K
  • 如果 L 的值为 2 ,则打印未删除的从KN的第一个整数。

注意:当右边的所有元素都被删除时,答案将为-1

例子:

方法:给定的问题可以通过使用不相交集联合数据结构来解决。最初,所有元素都在不同的集合中,对于第一种类型的查询,对给定的整数执行联合运算。对于第二种查询,获取给定顶点的父节点并打印存储在数组graph[]中的值。请按照以下步骤解决问题:

  • 初始化矢量图(N + 2)
  • 使用变量i迭代范围[1, N+1]并将graph[i]的值设置为i
  • 使用变量query遍历query[]并执行以下步骤:
    • 如果query.first1 ,则调用函数操作Union(graph, query.second, query.second + 1)
    • 否则,调用函数操作Get(graph, query.second)并将其分配给变量a 。现在,如果a的值为(N + 1) ,则打印-1 。否则,打印graph[a]的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to perform the Get operation
// of disjoint set union
int Get(vector& graph, int a)
{
    return graph[a]
           = (graph[a] == a ? a
                            : Get(graph, graph[a]));
}
 
// Function to perform the union
// operation of disjoint set union
void Union(vector& graph,
           int a, int b)
{
 
    a = Get(graph, a);
    b = Get(graph, b);
 
    // Update the graph[a] as b
    graph[a] = b;
}
 
// Function to perform given queries on
// set of vertices initially not connected
void Queries(vector >& queries,
             int N, int M)
{
 
    // Stores the vertices
    vector graph(N + 2);
 
    // Mark every vertices rightmost
    // vertex as i
    for (int i = 1; i <= N + 1; i++) {
 
        graph[i] = i;
    }
 
    // Traverse the queries array
    for (auto query : queries) {
 
        // Check if it is first type
        // of the given query
        if (query.first == 1) {
 
            Union(graph, query.second,
                  query.second + 1);
        }
        else {
 
            // Get the parent of a
            int a = Get(graph, query.second);
 
            // Print the answer for
            // the second query
            if (a == N + 1)
                cout << -1 << " ";
            else
                cout << graph[a] << " ";
        }
    }
}
 
// Driver Code
int main()
{
    int N = 5;
    vector > queries{
        { 2, 1 }, { 1, 1 }, { 2, 1 }, { 2, 3 }
    };
    int Q = queries.size();
 
    Queries(queries, N, Q);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to perform the Get operation
// of disjoint set union
public static int Get(int[] graph, int a)
{
    return graph[a]
           = (graph[a] == a ? a
                            : Get(graph, graph[a]));
}
 
// Function to perform the union
// operation of disjoint set union
public static void Union(int[] graph,
           int a, int b)
{
 
    a = Get(graph, a);
    b = Get(graph, b);
 
    // Update the graph[a] as b
    graph[a] = b;
}
 
// Function to perform given queries on
// set of vertices initially not connected
public static void Queries(int[][] queries,
             int N, int M)
{
 
    // Stores the vertices
    int[] graph = new int[N + 2];
 
    // Mark every vertices rightmost
    // vertex as i
    for (int i = 1; i <= N + 1; i++) {
 
        graph[i] = i;
    }
 
    // Traverse the queries array
    for (int[] query : queries) {
 
        // Check if it is first type
        // of the given query
        if (query[0] == 1) {
 
            Union(graph, query[1],
                  query[1] + 1);
        }
        else {
 
            // Get the parent of a
            int a = Get(graph, query[1]);
 
            // Print the answer for
            // the second query
            if (a == N + 1)
                System.out.print(-1);
            else
                System.out.print(graph[a] + " ");
        }
    }
}
 
// Driver Code
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);
 
}
}
 
// This code is contributed by gfgking.


Python3
# Python 3 program for the above approach
 
# Function to perform the Get operation
# of disjoint set union
def Get(graph, a):
    if graph[graph[a]]!= graph[a]:
        graph[a] = Get(graph, graph[a])
    else:
        return graph[a]
 
# Function to perform the union
# operation of disjoint set union
def Union(graph, a, b):
    a = Get(graph, a)
    b = Get(graph, b)
 
    # Update the graph[a] as b
    graph[a] = b
 
# Function to perform given queries on
# set of vertices initially not connected
def Queries(queries, N, M):
   
    # Stores the vertices
    graph = [0 for i in range(N + 2)]
 
    # Mark every vertices rightmost
    # vertex as i
    for i in range(1,N + 2,1):
        graph[i] = i
 
    # Traverse the queries array
    for query in queries:
       
        # Check if it is first type
        # of the given query
        if (query[0] == 1):
            Union(graph, query[1], query[1] + 1)
 
        else:
 
            # Get the parent of a
            a = Get(graph, query[1])
 
            # Print the answer for
            # the second query
            if (a == N + 1):
                print(-1)
            else:
                print(graph[a],end = " ")
 
# Driver Code
if __name__ == '__main__':
    N = 5
    queries = [[2, 1],[1, 1],[2, 1],[2, 3]]
    Q = len(queries)
 
    Queries(queries, N, Q)
     
    # This code is contributed by SURENDRA_GANGWAR.


Javascript


输出:
1 2 3

时间复杂度: O(M*log N)
辅助空间: O(N)