banana

本文介绍了一个关于猴子与香蕉的趣味算法问题。通过两份清单——猴子偏好香蕉类型及不同地点提供的香蕉类型,该算法旨在找出每只猴子是否能在某个地点找到其偏好的香蕉种类。文章提供了一段C++代码实现,包括输入输出格式和具体解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 Format

The first line contains an integer TTT, indicating that there are TTT test cases.

For each test case, the first line contains two integers NNN and MMM, representing the length of the first and the second lists respectively.

In the each line of following NNN lines, two positive integers i,ji, ji,j indicate that the iii-th monkey favours the jjj-th type of banana.

In the each line of following MMM lines, two positive integers j,kj, kj,k indicate that the jjj-th type of banana could be find in the kkk-th place.

All integers of the input are less than or equal to 505050.

Output Format

For each test case, output all the pairs x,yx, yx,y that the xxx-the monkey can accept at least one type of bananas from the yyy-th place.

These pairs should be outputted as ascending order. That is say that a pair of x,yx, yx,y which owns a smaller xxx should be output first.

If two pairs own the same xxx, output the one who has a smaller yyy first.

And there should be an empty line after each test case.

样例输入
1
6 4
1 1
1 2
2 1
2 3
3 3
4 1
1 1
1 3
2 2
3 3
样例输出
1 1
1 2
1 3
2 1
2 3
3 3
4 1
4 3
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct mtb
{
	int mnum;
	int bnum;
}mtb;
mtb a[55];
typedef struct btr{
	int bnum;
	int rnum;
}btr;
btr b[55];
typedef struct M{
	int mnum;
	int rnum;
}M;
M F[1100];
int cmp(M p1, M p2)
{
    if (p1.mnum < p2.mnum)
    {
        return 1;
    }
    if (p1.mnum == p2.mnum && p1.rnum < p2.rnum)
    {
        return 1;
    }
    return 0;
}
int  x=0;
int main()
{
	int T;
	int list1,list2;
	cin>>T;
	while(T--)
	{
		cin>>list1>>list2;
		for(int i=0;i<list1;i++)
		{
			cin>>a[i].mnum;
			cin>>a[i].bnum;
		}
		for(int j=0;j<list2;j++)
		{
			cin>>b[j].bnum;
			cin>>b[j].rnum;
		} 	
		 for(int k=0;k<list1;k++)
		 {
		        for(int q=0;q<list2;q++)
				{
					if(a[k].bnum == b[q].bnum)
					{
						F[x].mnum=a[k].mnum;
						F[x].rnum=b[q].rnum;
						x++;
					}		
				} 		
		 }	
		 sort(F,F+x,cmp); 
		 for(int t=1;t<=x;t++){
		 	    if(!(F[t].mnum==F[t-1].mnum && F[t].rnum==F[t-1].rnum))
		 		cout<<F[t-1].mnum<<" "<<F[t-1].rnum<<endl;
		 }
		 cout<<endl;
		 x=0;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值