B. Matchmaker

来源:互联网 发布:菜鸟网络借壳三泰控股 编辑:程序博客网 时间:2024/06/06 01:10

time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarpus has n markers and m marker caps. Each marker is described by two numbers: xi is the color and yi is the diameter. Correspondingly, each cap is described by two numbers: aj is the color and bj is the diameter. Cap (aj, bj) can close marker (xi, yi) only if their diameters match, that is, bj = yi. Besides, a marker is considered to be beautifully closed, if the cap color and the marker color match, that is, aj = xi.

Find the way to close the maximum number of markers. If there are several such ways, then choose the one that has the maximum number of beautifully closed markers.

Input

The first input line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the number of markers and the number of caps, correspondingly.

Next n lines describe the markers. The i-th line contains two space-separated integers xiyi (1 ≤ xi, yi ≤ 1000) — the i-th marker's color and diameter, correspondingly.

Next m lines describe the caps. The j-th line contains two space-separated integers ajbj (1 ≤ aj, bj ≤ 1000) — the color and diameter of the j-th cap, correspondingly.

Output

Print two space-separated integers u, v, where u is the number of closed markers and v is the number of beautifully closedmarkers in the sought optimal way. Remember that you have to find the way to close the maximum number of markers, and if there are several such ways, you should choose the one where the number of beautifully closed markers is maximum.

Sample test(s)
input
3 41 23 42 45 42 41 11 2
output
3 2
input
2 21 22 13 45 1
output
1 0
Note

In the first test sample the first marker should be closed by the fourth cap, the second marker should be closed by the first cap and the third marker should be closed by the second cap. Thus, three markers will be closed, and two of them will bebeautifully closed — the first and the third markers.


解题说明:题目中提到了maker和maker cap两种东西,应该是用来匹配用的,此题要求的是closed makers和beautifully closed makers,其中beautiful的要求是除了半径要符合要求外,颜色也要符合要求。由于范围不是很大,可以用数组来进行标记,先读取maker信息在数组对应位置进行标记,再读入caps信息的时查看当前位置是否有maker已经标记。先判断半径,再判断颜色。


#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include <algorithm>using namespace std;int main(){int v[1020][1020];int c[1020],n,m,x,y,a,b,i;    scanf("%d %d",&n,&m);    for(i=0;i<n;i++)    { scanf("%d %d",&x,&y);c[y]++;v[x][y]++;}    for(i=0;i<m;i++)    {        scanf("%d %d",&x,&y);        if(v[x][y])        {b++;v[x][y]--;}        if(c[y])         { a++; c[y]--;}    }    printf("%d %d\n",a,b);    return 0;}


原创粉丝点击