📌  相关文章
📜  使两个数组相同所需的最小更改

📅  最后修改于: 2021-04-17 10:47:34             🧑  作者: Mango

给定两个数组AB每个元素有n个。任务是使这两个数组相同,即,对于每个1\leq i \leq N ,我们要A_{i} = B_{i} 。在一个单一的操作,你可以选择两个整数xy,并替换x的所有出现在这两个与y中的数组。请注意,无论替换的出现次数如何,仍将其计为一次操作。您必须输出所需的最少操作数。

例子:

Input : 1 2 2
        1 2 5
Output: 1
Here, (x, y) = (5, 2) hence ans = 1.

Input : 2 1 1 3 5
        1 2 2 4 5
Output: 2
Here, (x, y) = (1, 2) and (3, 4) thus ans = 2.
Other pairs are also possible.

可以在不交集联合的帮助下解决此问题。
我们将检查两个数组的所有元素,即每个1\leq i \leq N 。如果元素属于相同的ID,则我们将其跳过。否则,我们对这两个元素都执行Union操作。最后,答案将是形成的所有不同不相交集的大小之和: ans = \sum_{i=1}^{N} (sz[i]-1) 。我们减去1是因为最初我们将每个集合的大小设为1。
下面是上述方法的实现:

C++
// C++ program to find minimum changes 
// required to make two arrays identical
#include 
using namespace std;
  
#define N 100010
  
/*  'id': stores parent of a node.
    'sz': stores size of a DSU tree. */
int id[N], sz[N];
  
// Function to assign root
int Root(int idx)
{
    int i = idx;
    while (i != id[i])
        id[i] = id[id[i]], i = id[i];
  
    return i;
}
  
// Function to find Union
void Union(int a, int b)
{
    int i = Root(a), j = Root(b);
  
    if (i != j) {
        if (sz[i] >= sz[j]) {
            id[j] = i, sz[i] += sz[j];
            sz[j] = 0;
        }
        else {
            id[i] = j, sz[j] += sz[i];
            sz[i] = 0;
        }
    }
}
  
// function to find minimum changes required
// to make both array equal.
int minChange(int n, int a[], int b[])
{
  
    // Sets as single elements
    for (int i = 0; i < N; i++)
        id[i] = i, sz[i] = 1;
  
    // Combine items if they belong to different
    // sets.
    for (int i = 0; i < n; ++i)
  
        // true if both elements have different root
        if (Root(a[i]) != Root(b[i]))
            Union(a[i], b[i]); // make root equal
  
    // Find sum sizes of all sets formed.
    int ans = 0;
    for (int i = 0; i < n; ++i)
        if (id[i] == i)
            ans += (sz[i] - 1);
  
    return ans;
}
  
// Driver program
int main()
{
  
    int a[] = { 2, 1, 1, 3, 5 }, b[] = { 1, 2, 2, 4, 5 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << minChange(n, a, b);
    return 0;
}


Java
// Java program to find minimum changes 
// required to make two arrays identical
  
class GFG{
static int N=100010;
  
/* 'id': stores parent of a node.
    'sz': stores size of a DSU tree. */
static int[] id=new int[100010];
static int[] sz=new int[100010];
  
// Function to assign root
static int Root(int idx)
{
    int i = idx;
    while (i != id[i])
        {
            id[i] = id[id[i]];
            i = id[i];
        }
  
    return i;
}
  
// Function to find Union
static void Union(int a, int b)
{
    int i = Root(a);
    int j = Root(b);
  
    if (i != j) {
        if (sz[i] >= sz[j]) {
            id[j] = i;
            sz[i] += sz[j];
            sz[j] = 0;
        }
        else {
            id[i] = j;
            sz[j] += sz[i];
            sz[i] = 0;
        }
    }
}
  
// function to find minimum changes required
// to make both array equal.
static int minChange(int n, int a[], int b[])
{
  
    // Sets as single elements
    for (int i = 0; i < N; i++)
        {
            id[i] = i;
            sz[i] = 1;
        }
  
    // Combine items if they belong to different
    // sets.
    for (int i = 0; i < n; ++i)
  
        // true if both elements have different root
        if (Root(a[i]) != Root(b[i]))
            Union(a[i], b[i]); // make root equal
  
    // Find sum sizes of all sets formed.
    int ans = 0;
    for (int i = 0; i < n; ++i)
        if (id[i] == i)
            ans += (sz[i] - 1);
  
    return ans;
}
  
// Driver program
public static void main(String[] args)
{
  
    int a[] = { 2, 1, 1, 3, 5 }, b[] = { 1, 2, 2, 4, 5 };
    int n = a.length;
    System.out.println(minChange(n, a, b));
}
}
// This code is contributed by mits


Python 3
# Python 3 program to find minimum changes
# required to make two arrays identical
  
N = 100010
  
# 'id':stores parent of a node
# 'sz':stores size of a DSU tree
ID = [0 for i in range(N)]
sz = [0 for i in range(N)]
  
# function to assign root
def Root(idx):
    i = idx
    while i != ID[i]:
        ID[i], i = ID[ID[i]], ID[i]
    return i
  
# Function to find Union
def Union(a, b):
    i, j = Root(a), Root(b)
      
    if i != j:
        if sz[i] >= sz[j]:
            ID[j] = i
            sz[i] += sz[j]
            sz[j] = 0
        else:
            ID[i] = j
            sz[j] += sz[i]
            sz[i] = 0
  
# function to find minimum changes
# reqired to make both array equal
def minChange(n, a, b):
      
    # sets as single elements
    for i in range(N):
        ID[i] = i
        sz[i] = 1
          
    # Combine items if they belong 
    # to differnet sets
    for i in range(n):
          
        # true if both elements have
        # different root
        if Root(a[i]) != Root(b[i]):
            Union(a[i], b[i])
      
    # find sum sizes of all sets formed
    ans = 0
    for i in range(n):
        if ID[i] == i:
            ans += (sz[i] - 1)
      
    return ans
      
# Driver Code
a = [2, 1, 1, 3, 5]
b = [1, 2, 2, 4, 5]
n = len(a)
  
print(minChange(n, a, b))
  
# This code is contributed 
# by Mohit kumar 29 (IIIT gwalior)


C#
// C# program to find minimum changes 
// required to make two arrays identical
using System;
  
class GFG{
static int N=100010;
  
/* 'id': stores parent of a node.
    'sz': stores size of a DSU tree. */
static int []id=new int[100010];
static int []sz=new int[100010];
  
// Function to assign root
static int Root(int idx)
{
    int i = idx;
    while (i != id[i])
        {
            id[i] = id[id[i]];
            i = id[i];
        }
  
    return i;
}
  
// Function to find Union
static void Union(int a, int b)
{
    int i = Root(a);
    int j = Root(b);
  
    if (i != j) {
        if (sz[i] >= sz[j]) {
            id[j] = i;
            sz[i] += sz[j];
            sz[j] = 0;
        }
        else {
            id[i] = j;
            sz[j] += sz[i];
            sz[i] = 0;
        }
    }
}
  
// function to find minimum changes required
// to make both array equal.
static int minChange(int n, int []a, int []b)
{
  
    // Sets as single elements
    for (int i = 0; i < N; i++)
        {
            id[i] = i;
            sz[i] = 1;
        }
  
    // Combine items if they belong to different
    // sets.
    for (int i = 0; i < n; ++i)
  
        // true if both elements have different root
        if (Root(a[i]) != Root(b[i]))
            Union(a[i], b[i]); // make root equal
  
    // Find sum sizes of all sets formed.
    int ans = 0;
    for (int i = 0; i < n; ++i)
        if (id[i] == i)
            ans += (sz[i] - 1);
  
    return ans;
}
  
// Driver program
public static void Main()
{
  
    int []a = { 2, 1, 1, 3, 5 };
    int []b = { 1, 2, 2, 4, 5 };
    int n = a.Length;
    Console.WriteLine(minChange(n, a, b));
}
}
// This code is contributed by anuj_67..


PHP
= $sz[$j]) 
        {
            $id[$j] = $i;
            $sz[$i] += $sz[$j];
            $sz[$j] = 0;
        }
        else 
        {
            $id[$i] = $j;
            $sz[$j] += $sz[$i];
            $sz[$i] = 0;
        }
    }
}
  
// function to find minimum changes 
// required to make both array equal.
function minChange($n, &$a, &$b)
{
    global $id, $sz, $N;
  
    // Sets as single elements
    for ($i = 0; $i < $N; $i++)
    {
        $id[$i] = $i;
        $sz[$i] = 1;
    }
  
    // Combine items if they belong to 
    // different sets.
    for ($i = 0; $i < $n; ++$i)
  
        // true if both elements have 
        // different roots
        if (Root($a[$i]) != Root($b[$i]))
            Union($a[$i], $b[$i]); // make root equal
  
    // Find sum sizes of all sets formed.
    $ans = 0;
    for ($i = 0; $i < $n; ++$i)
        if ($id[$i] == $i)
            $ans += ($sz[$i] - 1);
  
    return $ans;
}
  
// Driver Code
$a = array(2, 1, 1, 3, 5);
$b = array(1, 2, 2, 4, 5);
$n = sizeof($a);
echo minChange($n, $a, $b);
  
// This code is contributed by ita_c
?>


输出:
2

时间复杂度: O(N + n),其中N是数组项的最大可能值,n是数组中元素的数量。