Problem Description
Bananas are the favoured food of monkeys.
In the forest, there is a Banana Company that provides bananas from different places.
The company has two lists.
The first list records the types of bananas preferred by different monkeys, and the second one records the types of bananas from different places.
Now, the supplier wants to know, whether a monkey can accept at least one type of bananas from a place.
Remenber that, there could be more than one types of bananas from a place, and there also could be more than one types of bananas of a monkey's preference.
Input
The first line contains an integer T, indicating that there are T test cases.
For each test case, the first line contains two integers N and M, representing the length of the first and the second lists respectively.
In the each line of following N lines, two positive integers i,j indicate that the ii-th monkey favours the j-th type of banana.
In the each line of following M lines, two positive integers j,k indicate that the jj-th type of banana could be find in the k-th place.
All integers of the input are less than or equal to 50.
Output
For each test case, output all the pairs x,y that the x-the monkey can accept at least one type of bananas from the y-th place.
These pairs should be outputted as ascending order. That is say that a pair of x,y which owns a smaller x should be output first.
If two pairs own the same x, output the one who has a smaller y first.
And there should be an empty line after each test case.
Sample Input
1
6 4
1 1
1 2
2 1
2 3
3 3
4 1
1 1
1 3
2 2
3 3Sample Output
1 1
1 2
1 3
2 1
2 3
3 3
4 1
4 3
题意:t 组数据,给出 n 对猴子和香蕉 (a,b) 的关系代表猴子 a 喜欢香蕉 b,然后给出 m 对香蕉与地点的关系 (b,c) 代表香蕉 b 在地点 c 有种植,问每个猴子喜欢的香蕉的对应的生产地址,要求按字典序输出
思路:由于数据量最多到 50,因此直接暴力即可,最后记得按字典序排序去重
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
const double EPS = 1E-10;
const int MOD = 1E9+7;
const int N = 1000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
struct Node {
int a,b;
bool operator < (const Node &rhs)const {
if(a==rhs.a)
return b<rhs.b;
return a<rhs.a;
}
} node[N],res[N];
int main() {
int t;
scanf("%d",&t);
while(t--) {
int n,m;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
scanf("%d%d",&node[i].a,&node[i].b);
int tot=0;
for(int i=1; i<=m; i++) {
int x,y;
scanf("%d%d",&x,&y);
for(int i=1; i<=n; i++) {
if(x==node[i].b) {
res[++tot].a=node[i].a;
res[tot].b=y;
}
}
}
sort(res+1,res+1+tot);
for(int i=1; i<=tot; i++) {
if(res[i].a==res[i+1].a&&res[i].b==res[i+1].b)
continue;
printf("%d %d\n",res[i].a,res[i].b);
}
printf("\n");
}
return 0;
}