2017 ACM-ICPC 亚洲区-banana

Discussion


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 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 i-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 j-th 
type of banana could be find in the k-th place. 
All integers of the input are less than 50.

Output Format

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


6 4 
1 1 
1 2 
2 1 
2 3 
3 3 
4 1 
1 1 
1 3 
2 2 
3 3

Sample Output

1 1 
1 2 
1 3 
2 1 
2 2 
2 3 
3 3 
4 1 
4 3

题意:

输入n和m,n行每行表示第i只猴子喜欢第j种香蕉,m行每行表示第i种香蕉来自第j个地区;
问哪些猴子在哪些地区有喜欢的香蕉,按猴子序号(若同则按地区)升序输出。

思路:

通过观察,可把每对数据当作一个二元关系,由n行部分和m行部分的二元关系通过关系复合(离散数学)得到结果。这里可通过 vector 和 map 的实现。

code:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

const int maxn = 55;
vector<int >nm[maxn];
int map[maxn][maxn];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n, m, moky, bana, pls;
        for(int i=0;i<=50;i++)
            nm[i].clear();
        memset(map,0,sizeof(map));
        scanf("%d %d",&n, &m);
        for(int i = 0; i < n; i ++)
        {
            scanf("%d %d",&moky, &bana);
            nm[bana].push_back(moky);
        }
        for(int i = 0; i < m; i ++)
        {
            scanf("%d %d",&bana, &pls);
            for(unsigned int j = 0; j < nm[bana].size(); j ++)
                map[nm[bana][j]][pls] = 1;
        }
        for(int i = 1; i <= 50; i ++)
        {
            for(int j = 1; j <= 50; j ++)
                if(map[i][j])
                    printf("%d %d\n",i,j);
        }
        printf("\n");
    }
}


ACM-ICPC(国际大学生程序设计竞赛)是一项面向大学生的计算机编程竞赛,涉及算法和数据结构等领域。在比赛中,选手需要解决一系列编程问题,使用合适的算法和数据结构来实现正确和高效的解决方案。 对于整理ACM-ICPC模板,以下是一些建议: 1. 了解比赛要求:首先,你需要了解ACM-ICPC比赛的具体要求和规则。这包括了解比赛所涉及的算法和数据结构,以及题目的类型和难度等。 2. 收集资料:收集与ACM-ICPC相关的资料,包括经典算法和数据结构的实现代码、常见问题的解题思路等。可以参考教材、博客、论文等资源。 3. 整理模板:将收集到的资料整理成模板。可以按照算法和数据结构的分类进行整理,例如排序算法、图算法、字符串算法等。对每个模板,添加必要的注释和示例代码,以便理解和使用。 4. 测试代码:对每个模板编写测试代码,确保它们的正确性和可靠性。可以使用已知的测试用例或自行设计测试用例。 5. 更新与扩充:定期更新和扩充模板,以适应ACM-ICPC比赛中新出现的算法和数据结构。同时,根据自己的经验和理解,对模板进行优化和改进。 6. 练习和复习:在比赛之前,利用整理好的模板进行练习和复习。尝试解决一些经典问题,使用模板中的算法和数据结构进行实现,并进行优化。 希望这些建议对你整理ACM-ICPC模板有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值