Nothing in Common Solution

Problem

Alice has quarreled with Berta recently. Now she doesn't want to have anything in common with her!

Recently, they've received two collections of positive integers. The Alice's integers were A1A2, ..., AN, while Berta's were B1B2, ..., BM.

Now Alice wants to throw away the minimal amount of number from her collection so that their collections would have no common numbers, i.e. there won't be any number which is present in both collections. Please help Alice to find the minimal amount of numbers she would need to throw away.

Input

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.

The first line of each test case contains two space-separated integer numbers N and M denoting the amount of numbers in Alice's and Berta's collections respectively.

The second line contains N space-separated integers A1A2, ..., AN denoting the numbers of Alice.

The third line contains M space-separated integers B1B2, ..., BM denoting the numbers of Berta.

Output

For each test case, output a single line containing the minimal amount of numbers Alice needs to throw away from her collection so that she wouldn't have any numbers in common with Berta.

Constraints

  • 1 ≤ AiBi ≤ 106
  • All numbers are distinct within a single girl's collection.

Subtasks

  • Subtask #1 (47 points)T = 251 ≤ N, M ≤ 1000
  • Subtask #2 (53 points)T = 51 ≤ N, M ≤ 100000

Sample 1:

Input
Output
2
3 4
1 2 3
3 4 5 6
3 3
1 2 3
4 5 6
1
0

Explanation:

Example case 1. If Alice throws away the number 3 from her collection, she would obtain {1, 2} which is disjoint with {3, 4, 5, 6}.

Example case 2. Girls don't have any number in common initially. So there is no need to throw away any number.






Program :


 #include <stdio.h>

#include <string.h>

int main(void) 

{

unsigned short t1;

scanf("%hu",&t1);

while(t1--)

{

    unsigned register int num=0;

    unsigned int p,q,a[1000001];

    scanf("%u%u",&p,&q);

    memset(a,0,sizeof(int)*1000001);

    p+=q;

    for(unsigned register int i=0; i<p; i++)

    {

        scanf("%u",&q);

        ++a[q];

        num+=a[q]>1;

    }

    printf("%u\n",num);

}

return 0;

}


Post a Comment

0 Comments